Jelajahi Sumber

Fixed double extension bug after slugify.

Romain Bruckert 9 tahun lalu
induk
melakukan
6f2302a52f
2 mengubah file dengan 43 tambahan dan 13 penghapusan
  1. 1 1
      src/Helper/Slugify.php
  2. 42 12
      src/Resumable.php

+ 1 - 1
src/Helper/Slugify.php

@@ -24,7 +24,7 @@ namespace Dilab\Helper;
 class Slugify implements SlugifyInterface
 {
     const LOWERCASE_NUMBERS_DASHES = '/([^A-Za-z0-9]|-)+/';
-    
+
     /** @var array */
     protected $rules = array(
         // Numeric characters, superscript

+ 42 - 12
src/Resumable.php

@@ -33,8 +33,12 @@ class Resumable
 
     protected $filename;
 
-    const SLUGIFY = true;
+    protected $originalFilename;
+
+    protected $filepath;
 
+    const SLUGIFY = true;
+    
     public function __construct(Request $request, Response $response)
     {
         $this->request = $request;
@@ -76,25 +80,30 @@ class Resumable
     {
         return $this->filename;
     }
-    
+
+    /**
+     * Get final filapath.
+     *
+     * @return string Final filename
+     */
+    public function getFilepath()
+    {
+        return $this->filepath;
+    }
+
     /**
-     * Makes sure the extension never gets overriden by user defined filname.
+     * 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
-     * adadgio-pr
      */
     private function createSafeFilename($filename, $originalFilename)
     {
-        $parts = explode('.', $originalFilename);
-        $ext = end($parts); // get extension
+        $filename = $this->removeExtension($filename);
+        $extension = $this->findExtension($originalFilename);
 
-        // remove extension from filename if any
-        $filename = str_replace(sprintf('.%s', $ext), '', $filename);
-
-        // return safe filename with appended extension from original filename
-        return $filename.sprintf('.%s', $ext);
+        return sprintf('%s.%s', $filename, $extension);
     }
 
     public function handleTestChunk()
@@ -140,6 +149,9 @@ class Resumable
         $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 (null !== $this->filename) {
             // optionaly create a unique slugified filename
@@ -151,7 +163,11 @@ class Resumable
             }
         }
 
-        if ($this->createFileFromChunks($chunkFiles, $this->uploadFolder . DIRECTORY_SEPARATOR . $filename) && $this->deleteTmpFolder) {
+        // replace filename reference by the final file
+        $this->filename = $filename;
+        $this->filepath = $this->uploadFolder . DIRECTORY_SEPARATOR . $this->filename;
+
+        if ($this->createFileFromChunks($chunkFiles, $this->filepath) && $this->deleteTmpFolder) {
             $tmpFolder->delete();
         }
     }
@@ -253,5 +269,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);
+    }
 }