فهرست منبع

Merge pull request #6 from adadgio/master

Master
Xu Ding 9 سال پیش
والد
کامیت
1688284682
2فایلهای تغییر یافته به همراه108 افزوده شده و 2 حذف شده
  1. 11 1
      readme.md
  2. 97 1
      src/Resumable.php

+ 11 - 1
readme.md

@@ -29,10 +29,20 @@ $resumable->process();
 
 ```
 
+## Options ##
+### Setting custom filename ###
 
+```
+// custom filename (extension from original file will be kept)
+// @todo Add resumable->getExtension() method
+$resumable->setFilename('myfile');
+
+// automatically slugified filename
+// @todo Not yet working, better use as 3d party library https://github.com/cocur/slugify
+$resumable->setFilename(RESUMABLE::SLUGIFY);
+```
 
 ## Testing
 ```
 $ ./vendor/bin/phpunit
 ```
-

+ 97 - 1
src/Resumable.php

@@ -29,6 +29,14 @@ class Resumable
 
     protected $log;
 
+    protected $filename;
+
+    protected $originalFilename;
+
+    protected $filepath;
+
+    const SLUGIFY = true;
+
     public function __construct(Request $request, Response $response)
     {
         $this->request = $request;
@@ -49,6 +57,63 @@ class Resumable
         }
     }
 
+    /**
+     * Set final filename.
+     *
+     * @param string Final filename
+     */
+    public function setFilename($filename)
+    {
+        $this->filename = $filename;
+
+        return $this;
+    }
+
+    /**
+     * Get final filename.
+     *
+     * @return string Final filename
+     */
+    public function getFilename()
+    {
+        return $this->filename;
+    }
+
+    /**
+     * Get final filename.
+     *
+     * @return string Final filename
+     */
+    public function getOriginalFilename()
+    {
+        return $this->originalFilename;
+    }
+
+    /**
+     * Get final filapath.
+     *
+     * @return string Final filename
+     */
+    public function getFilepath()
+    {
+        return $this->filepath;
+    }
+
+    /**
+     * Makes sure the orginal extension never gets overriden by user defined filename.
+     *
+     * @param string User defined filename
+     * @param string Original filename
+     * @return string Filename that always has an extension from the original file
+     */
+    private function createSafeFilename($filename, $originalFilename)
+    {
+        $filename = $this->removeExtension($filename);
+        $extension = $this->findExtension($originalFilename);
+
+        return sprintf('%s.%s', $filename, $extension);
+    }
+
     public function handleTestChunk()
     {
         $identifier = $this->resumableParam('identifier');
@@ -83,12 +148,29 @@ class Resumable
         return $this->response->header(200);
     }
 
+    /**
+     * Create the final file from chunks
+     *
+     */
     private function createFileAndDeleteTmp($identifier, $filename)
     {
         $tmpFolder = new Folder($this->tmpChunkDir($identifier));
         $chunkFiles = $tmpFolder->read(true, true, true)[1];
-        if ($this->createFileFromChunks($chunkFiles, $this->uploadFolder . DIRECTORY_SEPARATOR . $filename) && $this->deleteTmpFolder) {
+
+        // save original filename
+        $this->originalFilename = $filename;
+        
+        // if the user has set a filename (or decided to slugify it), change the final filename
+        if (null !== $this->filename) {
+            $this->filename = $this->createSafeFilename($this->filename, $filename);
+        }
+
+        // replace filename reference by the final file
+        $this->filepath = $this->uploadFolder . DIRECTORY_SEPARATOR . $this->filename;
+
+        if ($this->createFileFromChunks($chunkFiles, $this->filepath) && $this->deleteTmpFolder) {
             $tmpFolder->delete();
+            $this->uploadComplete = true;
         }
     }
 
@@ -189,5 +271,19 @@ class Resumable
         }
     }
 
+    private function findExtension($filename)
+    {
+        $parts = explode('.', basename($filename));
+
+        return end($parts);
+    }
 
+    private function removeExtension($filename)
+    {
+        $parts = explode('.', basename($filename));
+        $ext = end($parts); // get extension
+
+        // remove extension from filename if any
+        return str_replace(sprintf('.%s', $ext), '', $filename);
+    }
 }