Skip to content

Commit a979029

Browse files
committed
Fix chunk issues with Dropzone/Resumable.js parallel uploading that was incorrect.
1 parent 2f40e95 commit a979029

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
## Introduction
1010

11-
> Supports Laravel from 5.2 to 7.
11+
> Supports Laravel from 5.2 to 7 (covered by integration tests on all versions).
1212
1313
Easy to use service/library for chunked upload with supporting multiple JS libraries on top of Laravel's file upload with low memory footprint in mind.
1414

1515
Supports feature as [cross domains requests](https://github.com/pionl/laravel-chunk-upload/wiki/cross-domain-requests), automatic clean schedule and easy usage.
1616

17-
Example repository with integration test can be found in [laravel-chunk-upload-example](https://github.com/pionl/laravel-chunk-upload-example).
17+
Example repository with **integration tests** can be found in [laravel-chunk-upload-example](https://github.com/pionl/laravel-chunk-upload-example).
1818

19-
> Before adding pull requests read CONTRIBUTION.md
19+
> Before adding pull requests read CONTRIBUTION.md. Help me fix your bugs by debugging your issues using XDEBUG (and try to do a fix - it will help you become better).
2020
2121
## Installation
2222

src/Handler/Traits/HandleParallelUploadTrait.php

+24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ trait HandleParallelUploadTrait
1010
{
1111
protected $percentageDone = 0;
1212

13+
/**
14+
* @return int
15+
*/
16+
abstract public function getTotalChunks();
17+
1318
/**
1419
* Returns the chunk save instance for saving.
1520
*
@@ -29,4 +34,23 @@ public function startSaving($chunkStorage)
2934
$this->config
3035
);
3136
}
37+
38+
public function getPercentageDone()
39+
{
40+
return $this->percentageDone;
41+
}
42+
43+
/**
44+
* Sets percentegage done - should be calculated from chunks count.
45+
*
46+
* @param int $percentageDone
47+
*
48+
* @return HandleParallelUploadTrait
49+
*/
50+
public function setPercentageDone(int $percentageDone)
51+
{
52+
$this->percentageDone = $percentageDone;
53+
54+
return $this;
55+
}
3256
}

src/Save/ParallelSave.php

+18-8
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
*/
2121
class ParallelSave extends ChunkSave
2222
{
23-
protected $totalChunks;
2423
/**
2524
* Stored on construct - the file is moved and isValid will return false.
2625
*
2726
* @var bool
2827
*/
2928
protected $isFileValid;
3029

30+
/**
31+
* @var array
32+
*/
33+
protected $foundChunks = [];
34+
3135
/**
3236
* ParallelSave constructor.
3337
*
@@ -68,12 +72,16 @@ protected function handleChunkFile($file)
6872
// Move the uploaded file to chunk folder
6973
$this->file->move($this->getChunkDirectory(true), $this->chunkFileName);
7074

71-
return $this;
72-
}
75+
// Found current number of chunks to determine if we have all chunks (we cant use the
76+
// index because order of chunks are different.
77+
$this->foundChunks = $this->getSavedChunksFiles()->all();
7378

74-
protected function tryToBuildFullFileFromChunks()
75-
{
76-
return parent::tryToBuildFullFileFromChunks();
79+
$percentage = floor((count($this->foundChunks)) / $this->handler()->getTotalChunks() * 100);
80+
// We need to update the handler with correct percentage
81+
$this->handler()->setPercentageDone($percentage);
82+
$this->isLastChunk = $percentage >= 100;
83+
84+
return $this;
7785
}
7886

7987
/**
@@ -98,7 +106,7 @@ protected function getSavedChunksFiles()
98106
*/
99107
protected function buildFullFileFromChunks()
100108
{
101-
$chunkFiles = $this->getSavedChunksFiles()->all();
109+
$chunkFiles = $this->foundChunks;
102110

103111
if (0 === count($chunkFiles)) {
104112
throw new MissingChunkFilesException();
@@ -109,7 +117,9 @@ protected function buildFullFileFromChunks()
109117

110118
// Get chunk files that matches the current chunk file name, also sort the chunk
111119
// files.
112-
$finalFilePath = $this->getChunkDirectory(true).'./'.$this->handler()->createChunkFileName();
120+
$rootDirectory = $this->getChunkDirectory(true);
121+
$finalFilePath = $rootDirectory.'./'.$this->handler()->createChunkFileName();
122+
113123
// Delete the file if exists
114124
if (file_exists($finalFilePath)) {
115125
@unlink($finalFilePath);

0 commit comments

Comments
 (0)