Skip to content

Commit e18712e

Browse files
committed
Change how paraller uploads are handled
Related to #37
1 parent a6b6170 commit e18712e

File tree

8 files changed

+92
-83
lines changed

8 files changed

+92
-83
lines changed

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ php artisan vendor:publish --provider="Pion\Laravel\ChunkUpload\Providers\ChunkU
3434

3535
## Usage
3636

37-
**simultaneous uploads are not working correctly - at this moment in experimental state.**
38-
3937
Setup is composed in 3 steps:
4038

4139
1. Integrate your controller that will handle the file upload. [How to](https://github.com/pionl/laravel-chunk-upload/wiki/controller)
@@ -51,6 +49,10 @@ Setup is composed in 3 steps:
5149
| [simple uploader](https://github.com/simple-uploader) | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | [@dyktek](https://github.com/dyktek) |
5250
| [ng-file-upload](https://github.com/danialfarid/ng-file-upload) | [Wiki](https://github.com/pionl/laravel-chunk-upload/wiki/ng-file-upload) | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | [@L3o-pold](https://github.com/L3o-pold) |
5351

52+
**Simultaneous uploads:** The library must send last chunk as last, otherwise the merging will not work correctly.
53+
54+
**Custom disk:** At this moment I recommend to use the basic storage setup (not linking public folder). It is not tested (Have free time to ensure it is working? PR the changes!).
55+
5456
For more detailed information (tips) use the [Wiki](https://github.com/pionl/laravel-chunk-upload/wiki) or for working example continue to separate repository with [example](https://github.com/pionl/laravel-chunk-upload-example).
5557

5658
## Changelog

src/Handler/ChunksInRequestUploadHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function isFirstChunk()
119119
}
120120

121121
/**
122-
* Returns the chunks count
122+
* Checks if the chunk is last
123123
*
124124
* @return int
125125
*/

src/Handler/DropZoneUploadHandler.php

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
use Illuminate\Http\Request;
55
use Illuminate\Http\UploadedFile;
66
use Pion\Laravel\ChunkUpload\Config\AbstractConfig;
7-
use Pion\Laravel\ChunkUpload\Exceptions\ChunkSaveException;
8-
use Pion\Laravel\ChunkUpload\Save\ParallelSave;
9-
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
7+
use Pion\Laravel\ChunkUpload\Handler\Traits\HandleParallelUploadTrait;
108

119
class DropZoneUploadHandler extends ChunksInRequestUploadHandler
1210
{
11+
use HandleParallelUploadTrait;
12+
1313
const CHUNK_UUID_INDEX = 'dzuuid';
1414
const CHUNK_INDEX = 'dzchunkindex';
1515
const CHUNK_FILE_SIZE_INDEX = 'dztotalfilesize';
@@ -33,29 +33,9 @@ class DropZoneUploadHandler extends ChunksInRequestUploadHandler
3333
public function __construct(Request $request, $file, $config)
3434
{
3535
parent::__construct($request, $file, $config);
36-
3736
$this->fileUuid = $request->get(self::CHUNK_UUID_INDEX);
3837
}
3938

40-
/**
41-
* Returns the chunk save instance for saving
42-
*
43-
* @param ChunkStorage $chunkStorage the chunk storage
44-
*
45-
* @return ParallelSave
46-
* @throws ChunkSaveException
47-
* @throws ChunkSaveException
48-
*/
49-
public function startSaving($chunkStorage)
50-
{
51-
return new ParallelSave(
52-
$this->getTotalChunksFromRequest($this->request),
53-
$this->file,
54-
$this,
55-
$chunkStorage,
56-
$this->config
57-
);
58-
}
5939

6040
/**
6141
* Builds the chunk file name from file uuid and current chunk

src/Handler/ResumableJSUploadHandler.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
use Illuminate\Http\Request;
55
use Illuminate\Http\UploadedFile;
66
use Pion\Laravel\ChunkUpload\Config\AbstractConfig;
7-
use Pion\Laravel\ChunkUpload\Exceptions\ChunkSaveException;
8-
use Pion\Laravel\ChunkUpload\Save\ParallelSave;
9-
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
7+
use Pion\Laravel\ChunkUpload\Handler\Traits\HandleParallelUploadTrait;
108

119
class ResumableJSUploadHandler extends ChunksInRequestUploadHandler
1210
{
11+
use HandleParallelUploadTrait;
12+
1313
const CHUNK_UUID_INDEX = 'resumableIdentifier';
1414
const CHUNK_NUMBER_INDEX = 'resumableChunkNumber';
1515
const TOTAL_CHUNKS_INDEX = 'resumableTotalChunks';
@@ -34,25 +34,6 @@ public function __construct(Request $request, $file, $config)
3434
$this->fileUuid = $request->get(self::CHUNK_UUID_INDEX);
3535
}
3636

37-
/**
38-
* Returns the chunk save instance for saving
39-
*
40-
* @param ChunkStorage $chunkStorage the chunk storage
41-
*
42-
* @return ParallelSave
43-
* @throws ChunkSaveException
44-
*/
45-
public function startSaving($chunkStorage)
46-
{
47-
return new ParallelSave(
48-
$this->getTotalChunksFromRequest($this->request),
49-
$this->file,
50-
$this,
51-
$chunkStorage,
52-
$this->config
53-
);
54-
}
55-
5637
/**
5738
* Append the resumable file - uuid and pass the current chunk index for parallel upload
5839
* @return string
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Pion\Laravel\ChunkUpload\Handler\Traits;
4+
5+
use Pion\Laravel\ChunkUpload\Exceptions\ChunkSaveException;
6+
use Pion\Laravel\ChunkUpload\Save\ParallelSave;
7+
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
8+
9+
trait HandleParallelUploadTrait
10+
{
11+
protected $percentageDone = 0;
12+
13+
/**
14+
* Returns the chunk save instance for saving
15+
*
16+
* @param ChunkStorage $chunkStorage the chunk storage
17+
*
18+
* @return ParallelSave
19+
* @throws ChunkSaveException
20+
*/
21+
public function startSaving($chunkStorage)
22+
{
23+
// Build the parallel save
24+
return new ParallelSave(
25+
$this->file,
26+
$this,
27+
$chunkStorage,
28+
$this->config
29+
);
30+
}
31+
}

src/Save/ChunkSave.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,21 @@ protected function handleChunk()
152152
$this->createChunksFolderIfNeeded();
153153
$file = $this->getChunkFilePath();
154154

155-
$this->handleChunkFile($file);
155+
$this->handleChunkFile($file)
156+
->tryToBuildFullFileFromChunks();
157+
}
156158

159+
/**
160+
* Checks if the current chunk is last
161+
* @return $this
162+
*/
163+
protected function tryToBuildFullFileFromChunks()
164+
{
157165
// build the last file because of the last chunk
158166
if ($this->isLastChunk) {
159167
$this->buildFullFileFromChunks();
160168
}
169+
return $this;
161170
}
162171

163172
/**

src/Save/ParallelSave.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@
99
use Pion\Laravel\ChunkUpload\FileMerger;
1010
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler;
1111
use Pion\Laravel\ChunkUpload\ChunkFile;
12+
use Pion\Laravel\ChunkUpload\Handler\Traits\HandleParallelUploadTrait;
1213
use Pion\Laravel\ChunkUpload\Storage\ChunkStorage;
1314

15+
/**
16+
* Class ParallelSave
17+
*
18+
* @method HandleParallelUploadTrait|AbstractHandler handler()
19+
*
20+
* @package Pion\Laravel\ChunkUpload\Save
21+
*/
1422
class ParallelSave extends ChunkSave
1523
{
1624
protected $totalChunks;
@@ -23,23 +31,24 @@ class ParallelSave extends ChunkSave
2331
/**
2432
* ParallelSave constructor.
2533
*
26-
* @param int|string $totalChunks
27-
* @param UploadedFile $file the uploaded file (chunk file)
28-
* @param AbstractHandler $handler the handler that detected the correct save method
29-
* @param ChunkStorage $chunkStorage the chunk storage
30-
* @param AbstractConfig $config the config manager
34+
* @param UploadedFile $file the uploaded file (chunk file)
35+
* @param AbstractHandler|HandleParallelUploadTrait $handler the handler that detected the correct save method
36+
* @param ChunkStorage $chunkStorage the chunk storage
37+
* @param AbstractConfig $config the config manager
3138
*
3239
* @throws ChunkSaveException
3340
*/
3441
public function __construct(
35-
$totalChunks,
3642
UploadedFile $file,
3743
AbstractHandler $handler,
3844
ChunkStorage $chunkStorage,
3945
AbstractConfig $config
40-
) {
41-
$this->totalChunks = intval($totalChunks);
46+
)
47+
{
48+
// Get current file validation - the file instance is changed
4249
$this->isFileValid = $file->isValid();
50+
51+
// Handle the file upload
4352
parent::__construct($file, $handler, $chunkStorage, $config);
4453
}
4554

@@ -49,31 +58,6 @@ public function isValid()
4958
}
5059

5160

52-
/**
53-
* Searches for all chunk files
54-
* @return \Illuminate\Support\Collection
55-
*/
56-
protected function savedChunksFiles()
57-
{
58-
return $this->chunkStorage()->files(function ($file) {
59-
return !preg_match("/\.[\d]+\.".ChunkStorage::CHUNK_EXTENSION."$/", $file);
60-
});
61-
}
62-
63-
/**
64-
* Handles the chunk merging
65-
* @throws ChunkSaveException
66-
*/
67-
protected function handleChunk()
68-
{
69-
$files = $this->savedChunksFiles();
70-
// We need to detect if this is last chunk - get all uploaded chunks (and increase the count by 1 for this
71-
// un-moved chunk) - it's safer due possibility of un-ordered chunks
72-
$this->isLastChunk = ($files->count() + 1) === $this->totalChunks;
73-
74-
parent::handleChunk();
75-
}
76-
7761
/**
7862
* Moves the uploaded chunk file to separate chunk file for merging
7963
*
@@ -88,13 +72,33 @@ protected function handleChunkFile($file)
8872
return $this;
8973
}
9074

75+
protected function tryToBuildFullFileFromChunks()
76+
{
77+
return parent::tryToBuildFullFileFromChunks();
78+
}
79+
80+
/**
81+
* Searches for all chunk files
82+
*
83+
* @return \Illuminate\Support\Collection
84+
*/
85+
protected function getSavedChunksFiles()
86+
{
87+
$chunkFileName = preg_replace(
88+
"/\.[\d]+\.".ChunkStorage::CHUNK_EXTENSION."$/", '', $this->handler()->getChunkFileName()
89+
);
90+
return $this->chunkStorage->files(function ($file) use ($chunkFileName) {
91+
return str_contains($file, $chunkFileName) === false;
92+
});
93+
}
94+
9195
/**
9296
* @throws MissingChunkFilesException
9397
* @throws ChunkSaveException
9498
*/
9599
protected function buildFullFileFromChunks()
96100
{
97-
$chunkFiles = $this->savedChunksFiles()->all();
101+
$chunkFiles = $this->getSavedChunksFiles()->all();
98102

99103
if (count($chunkFiles) === 0) {
100104
throw new MissingChunkFilesException();

src/Storage/ChunkStorage.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public function directory()
104104
/**
105105
* Returns an array of files in the chunks directory
106106
*
107+
* @param \Closure|null $rejectClosure
108+
*
107109
* @return Collection
108110
*
109111
* @see FilesystemAdapter::files()

0 commit comments

Comments
 (0)