Browse Source

Merge branch 'adadgio-master'

xu ding 8 years ago
parent
commit
c3d01a22c1
2 changed files with 66 additions and 19 deletions
  1. 14 8
      readme.md
  2. 52 11
      src/Resumable.php

+ 14 - 8
readme.md

@@ -29,17 +29,23 @@ $resumable->process();
 
 ```
 
-## Options ##
-### Setting custom filename ###
+## More ##
+### Setting custom filename(s) ###
 
 ```
-// custom filename (extension from original file will be kept)
-// @todo Add resumable->getExtension() method
-$resumable->setFilename('myfile');
+// custom filename (extension from original file will be magically removed and re-appended)
+$originalName = $resumable->getOriginalFilename(Resumable::WITHOUT_EXTENSION); // will gove you "original Name" instead of "original Name.png"
+// do some slugification or whatever you need...
+$slugifiedname = my_slugify($originalName); // this is up to you, it as ported out of the library.
+$resumable->setFilename($slugifiedname);
 
-// automatically slugified filename
-// @todo Not yet working, better use as 3d party library https://github.com/cocur/slugify
-$resumable->setFilename(RESUMABLE::SLUGIFY);
+$resumable->process();
+
+// you can also get file information after the upload is complete
+if (true === $resumable->isUploadComplete()) { // true when the final file has been uploaded and chunks reunited.
+    $extension = $resumable->getExtension();
+    $filename = $resumable->getFilename();
+}
 ```
 
 ## Testing

+ 52 - 11
src/Resumable.php

@@ -31,11 +31,15 @@ class Resumable
 
     protected $filename;
 
+    protected $filepath;
+
+    protected $extension;
+
     protected $originalFilename;
 
-    protected $filepath;
+    protected $isUploadComplete = false;
 
-    const SLUGIFY = true;
+    const WITHOUT_EXTENSION = true;
 
     public function __construct(Request $request, Response $response)
     {
@@ -44,6 +48,19 @@ class Resumable
 
         $this->log = new Logger('debug');
         $this->log->pushHandler(new StreamHandler('debug.log', Logger::DEBUG));
+
+        $this->preProcess();
+    }
+
+    // sets original filename and extenstion, blah blah
+    public function preProcess()
+    {
+        if (!empty($this->resumableParams())) {
+            if (!empty($this->request->file())) {
+                $this->extension = $this->findExtension($this->resumableParam('filename'));
+                $this->originalFilename = $this->resumableParam('filename');
+            }
+        }
     }
 
     public function process()
@@ -58,6 +75,16 @@ class Resumable
     }
 
     /**
+     * Get isUploadComplete
+     *
+     * @return boolean
+     */
+    public function isUploadComplete()
+    {
+        return $this->isUploadComplete;
+    }
+
+    /**
      * Set final filename.
      *
      * @param string Final filename
@@ -84,9 +111,13 @@ class Resumable
      *
      * @return string Final filename
      */
-    public function getOriginalFilename()
+    public function getOriginalFilename($withoutExtension = false)
     {
-        return $this->originalFilename;
+        if ($withoutExtension === static::WITHOUT_EXTENSION) {
+            return $this->removeExtension($this->originalFilename);
+        } else {
+            return $this->originalFilename;
+        }
     }
 
     /**
@@ -100,6 +131,16 @@ class Resumable
     }
 
     /**
+     * Get final extension.
+     *
+     * @return string Final extension name
+     */
+    public function getExtension()
+    {
+        return $this->extension;
+    }
+
+    /**
      * Makes sure the orginal extension never gets overriden by user defined filename.
      *
      * @param string User defined filename
@@ -142,6 +183,7 @@ class Resumable
         }
 
         if ($this->isFileUploadComplete($filename, $identifier, $chunkSize, $totalSize)) {
+            $this->isUploadComplete = true;
             $this->createFileAndDeleteTmp($identifier, $filename);
         }
 
@@ -150,23 +192,22 @@ class Resumable
 
     /**
      * Create the final file from chunks
-     *
      */
     private function createFileAndDeleteTmp($identifier, $filename)
     {
         $tmpFolder = new Folder($this->tmpChunkDir($identifier));
         $chunkFiles = $tmpFolder->read(true, true, true)[1];
 
-        // save original filename
-        $this->originalFilename = $filename;
-        
-        // if the user has set a filename (or decided to slugify it), change the final filename
+        // if the user has set a custom filename
         if (null !== $this->filename) {
-            $this->filename = $this->createSafeFilename($this->filename, $filename);
+            $finalFilename = $this->createSafeFilename($this->filename, $filename);
+        } else {
+            $finalFilename = $filename;
         }
 
         // replace filename reference by the final file
-        $this->filepath = $this->uploadFolder . DIRECTORY_SEPARATOR . $this->filename;
+        $this->filepath = $this->uploadFolder . DIRECTORY_SEPARATOR . $finalFilename;
+        $this->extension = $this->findExtension($this->filepath);
 
         if ($this->createFileFromChunks($chunkFiles, $this->filepath) && $this->deleteTmpFolder) {
             $tmpFolder->delete();