Skip to content

Commit

Permalink
added per session chunk file name, fixed the move function for the re…
Browse files Browse the repository at this point in the history
…sult chunk file, updated readme
  • Loading branch information
Martin Kluska committed Jun 27, 2016
1 parent 5c803e3 commit 9732647
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions src/Handler/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions src/Handler/ContentRangeUploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}
3 changes: 2 additions & 1 deletion src/Save/ChunkSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand Down

0 comments on commit 9732647

Please sign in to comment.