From 9732647adcb883ee61460d11f15ea2ae18a72ba7 Mon Sep 17 00:00:00 2001 From: Martin Kluska Date: Mon, 27 Jun 2016 19:46:44 +0200 Subject: [PATCH] added per session chunk file name, fixed the move function for the result chunk file, updated readme --- readme.md | 8 ++++++- src/Handler/AbstractHandler.php | 29 +++++++++++++++++++++++ src/Handler/ContentRangeUploadHandler.php | 4 ++-- src/Save/ChunkSave.php | 3 ++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index d7a206d..341381f 100644 --- a/readme.md +++ b/readme.md @@ -15,6 +15,12 @@ In your own controller create the `FileReceiver`, more in example. * Laravel 5+ * [blueimp-file-upload](https://github.com/blueimp/jQuery-File-Upload) - partial support (simple chunked and single upload) +## Features +* **Chunked uploads** + uses **chunked writing** aswell to minimize the memory footprint +* **Storing per Laravel Session to prevent overwrite** + all TMP files are stored with session token + ## Basic documentation ### FileReceiver @@ -136,7 +142,7 @@ Route::post('upload', 'UploadController@upload'); - [ ] add more providers (like pbupload) - [ ] add facade for a quick usage with callback and custom response based on the handler - [ ] cron to delete uncompleted files -- [ ] file per session (to support multiple) +- [x] file per session (to support multiple) - [ ] add a config with custom storage location - [ ] add an example project diff --git a/src/Handler/AbstractHandler.php b/src/Handler/AbstractHandler.php index ce1b2e9..c948db4 100644 --- a/src/Handler/AbstractHandler.php +++ b/src/Handler/AbstractHandler.php @@ -3,6 +3,7 @@ use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; +use Session; /** * The handler that will detect if we can continue the chunked upload @@ -33,6 +34,34 @@ public function __construct(Request $request, UploadedFile $file) $this->file = $file; } + /** + * Builds the chunk file name per session and the original name. You can + * provide custom aditional name at the end of the generated file name. + * + * @param string|null $aditionalName + * + * @return string + * + * @see UploadedFile::getClientOriginalName() + * @see Session::getId() + */ + protected function createChunkFileName($aditionalName = null) + { + // prepare basic name structure + $array = [ + $this->file->getClientOriginalName(), + Session::getId(), + ]; + + // add + if (!is_null($aditionalName)) { + $array[] = $aditionalName; + } + + // build name + return implode("-", $array); + } + /** * Returns the chunk file name for a storing the tmp file * diff --git a/src/Handler/ContentRangeUploadHandler.php b/src/Handler/ContentRangeUploadHandler.php index d9b3510..453806f 100644 --- a/src/Handler/ContentRangeUploadHandler.php +++ b/src/Handler/ContentRangeUploadHandler.php @@ -135,11 +135,11 @@ public function getBytesTotal() * * @return string returns the original name with the part extension * - * @see UploadedFile::getClientOriginalName() + * @see createChunkFileName() */ public function getChunkFileName() { - return $this->file->getClientOriginalName()."-".$this->bytesTotal.".part"; + return $this->createChunkFileName($this->bytesTotal.".part"); } } \ No newline at end of file diff --git a/src/Save/ChunkSave.php b/src/Save/ChunkSave.php index 5504daf..beddeea 100644 --- a/src/Save/ChunkSave.php +++ b/src/Save/ChunkSave.php @@ -119,7 +119,8 @@ protected function buildFullFileFromChunks($file) { $this->fullChunkFile = new UploadedFile( $file, $this->file->getClientOriginalName(), $this->file->getClientMimeType(), - filesize($file), $this->file->getError() + filesize($file), $this->file->getError(), true // we must pass the true as test to force the upload file + // to use a standart copy method, not move uploaded file ); }