Просмотр исходного кода

Issues fixed (getOrignalFilename, setFilename)

updated README as well.
Romain Bruckert 9 лет назад
Родитель
Сommit
43e884aa6f
2 измененных файлов с 36 добавлено и 14 удалено
  1. 7 2
      readme.md
  2. 29 12
      src/Resumable.php

+ 7 - 2
readme.md

@@ -33,8 +33,13 @@ $resumable->process();
 ### Setting custom filename(s) ###
 
 ```
-// custom filename (extension from original file will be kept)
-$resumable->setFilename('someCoolFilename');
+// 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);
+
+$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.

+ 29 - 12
src/Resumable.php

@@ -31,14 +31,16 @@ class Resumable
 
     protected $filename;
 
-    protected $originalFilename;
-
     protected $filepath;
 
     protected $extension;
 
+    protected $originalFilename;
+
     protected $isUploadComplete = false;
 
+    const WITHOUT_EXTENSION = true;
+
     public function __construct(Request $request, Response $response)
     {
         $this->request = $request;
@@ -46,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()
@@ -96,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;
+        }
     }
 
     /**
@@ -148,7 +167,7 @@ class Resumable
             return $this->response->header(200);
         }
     }
-    
+
     public function handleChunk()
     {
         $file = $this->request->file();
@@ -173,23 +192,21 @@ 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) {