فهرست منبع

Added filename setter/getters

Enable custom file name (keeps the original extension)
Romain Bruckert 9 سال پیش
والد
کامیت
c65f46874b
3فایلهای تغییر یافته به همراه90 افزوده شده و 1 حذف شده
  1. 11 1
      readme.md
  2. 17 0
      src/Helper/Slugify.php
  3. 62 0
      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
 ```
-

+ 17 - 0
src/Helper/Slugify.php

@@ -0,0 +1,17 @@
+<?php
+
+namespace Dilab\Helper;
+
+class Slugify
+{
+    /**
+     * Slugifies a string.
+     *
+     * @param  string Any diry string
+     * @return string Slugified string
+     */
+    public static function slugify($string)
+    {
+        return 'hello';
+    }
+}

+ 62 - 0
src/Resumable.php

@@ -8,6 +8,8 @@ use Dilab\Network\Response;
 use Monolog\Logger;
 use Monolog\Handler\StreamHandler;
 
+use Dilab\Helper\Slugify;
+
 class Resumable
 {
     public $debug = false;
@@ -29,6 +31,10 @@ class Resumable
 
     protected $log;
 
+    protected $filename;
+
+    const SLUGIFY = true;
+
     public function __construct(Request $request, Response $response)
     {
         $this->request = $request;
@@ -49,6 +55,47 @@ 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($filename)
+    {
+        return $this->filename;
+    }
+
+    /**
+     * Makes sure the extension never gets overriden by user defined filname.
+     *
+     * @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)
+    {
+        $parts = explode('.', $originalFilename);
+        $ext = end($parts); // get extension
+
+        // 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);
+    }
+
     public function handleTestChunk()
     {
         $identifier = $this->resumableParam('identifier');
@@ -83,10 +130,25 @@ 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 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
+            if ($this->filename === static::SLUGIFY) {
+                $filename = Slugify::slugify($filename);
+            } else {
+                $filename = $this->createSafeFilename($this->filename, $filename);
+            }
+        }
+
         if ($this->createFileFromChunks($chunkFiles, $this->uploadFolder . DIRECTORY_SEPARATOR . $filename) && $this->deleteTmpFolder) {
             $tmpFolder->delete();
         }