Skip to content

Commit 9732647

Browse files
author
Martin Kluska
committed
added per session chunk file name, fixed the move function for the result chunk file, updated readme
1 parent 5c803e3 commit 9732647

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

readme.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ In your own controller create the `FileReceiver`, more in example.
1515
* Laravel 5+
1616
* [blueimp-file-upload](https://github.com/blueimp/jQuery-File-Upload) - partial support (simple chunked and single upload)
1717

18+
## Features
19+
* **Chunked uploads**
20+
uses **chunked writing** aswell to minimize the memory footprint
21+
* **Storing per Laravel Session to prevent overwrite**
22+
all TMP files are stored with session token
23+
1824
## Basic documentation
1925

2026
### FileReceiver
@@ -136,7 +142,7 @@ Route::post('upload', 'UploadController@upload');
136142
- [ ] add more providers (like pbupload)
137143
- [ ] add facade for a quick usage with callback and custom response based on the handler
138144
- [ ] cron to delete uncompleted files
139-
- [ ] file per session (to support multiple)
145+
- [x] file per session (to support multiple)
140146
- [ ] add a config with custom storage location
141147
- [ ] add an example project
142148

src/Handler/AbstractHandler.php

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Illuminate\Http\Request;
55
use Illuminate\Http\UploadedFile;
6+
use Session;
67

78
/**
89
* The handler that will detect if we can continue the chunked upload
@@ -33,6 +34,34 @@ public function __construct(Request $request, UploadedFile $file)
3334
$this->file = $file;
3435
}
3536

37+
/**
38+
* Builds the chunk file name per session and the original name. You can
39+
* provide custom aditional name at the end of the generated file name.
40+
*
41+
* @param string|null $aditionalName
42+
*
43+
* @return string
44+
*
45+
* @see UploadedFile::getClientOriginalName()
46+
* @see Session::getId()
47+
*/
48+
protected function createChunkFileName($aditionalName = null)
49+
{
50+
// prepare basic name structure
51+
$array = [
52+
$this->file->getClientOriginalName(),
53+
Session::getId(),
54+
];
55+
56+
// add
57+
if (!is_null($aditionalName)) {
58+
$array[] = $aditionalName;
59+
}
60+
61+
// build name
62+
return implode("-", $array);
63+
}
64+
3665
/**
3766
* Returns the chunk file name for a storing the tmp file
3867
*

src/Handler/ContentRangeUploadHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ public function getBytesTotal()
135135
*
136136
* @return string returns the original name with the part extension
137137
*
138-
* @see UploadedFile::getClientOriginalName()
138+
* @see createChunkFileName()
139139
*/
140140
public function getChunkFileName()
141141
{
142-
return $this->file->getClientOriginalName()."-".$this->bytesTotal.".part";
142+
return $this->createChunkFileName($this->bytesTotal.".part");
143143
}
144144

145145
}

src/Save/ChunkSave.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ protected function buildFullFileFromChunks($file)
119119
{
120120
$this->fullChunkFile = new UploadedFile(
121121
$file, $this->file->getClientOriginalName(), $this->file->getClientMimeType(),
122-
filesize($file), $this->file->getError()
122+
filesize($file), $this->file->getError(), true // we must pass the true as test to force the upload file
123+
// to use a standart copy method, not move uploaded file
123124
);
124125
}
125126

0 commit comments

Comments
 (0)