Skip to content

Commit

Permalink
Fix chunk issues with Dropzone/Resumable.js parallel uploading that w…
Browse files Browse the repository at this point in the history
…as incorrect.
  • Loading branch information
pionl committed Jun 21, 2020
1 parent 2f40e95 commit a979029
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

## Introduction

> Supports Laravel from 5.2 to 7.
> Supports Laravel from 5.2 to 7 (covered by integration tests on all versions).
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.

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

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

> Before adding pull requests read CONTRIBUTION.md
> 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).
## Installation

Expand Down
24 changes: 24 additions & 0 deletions src/Handler/Traits/HandleParallelUploadTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ trait HandleParallelUploadTrait
{
protected $percentageDone = 0;

/**
* @return int
*/
abstract public function getTotalChunks();

/**
* Returns the chunk save instance for saving.
*
Expand All @@ -29,4 +34,23 @@ public function startSaving($chunkStorage)
$this->config
);
}

public function getPercentageDone()
{
return $this->percentageDone;
}

/**
* Sets percentegage done - should be calculated from chunks count.
*
* @param int $percentageDone
*
* @return HandleParallelUploadTrait
*/
public function setPercentageDone(int $percentageDone)
{
$this->percentageDone = $percentageDone;

return $this;
}
}
26 changes: 18 additions & 8 deletions src/Save/ParallelSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
*/
class ParallelSave extends ChunkSave
{
protected $totalChunks;
/**
* Stored on construct - the file is moved and isValid will return false.
*
* @var bool
*/
protected $isFileValid;

/**
* @var array
*/
protected $foundChunks = [];

/**
* ParallelSave constructor.
*
Expand Down Expand Up @@ -68,12 +72,16 @@ protected function handleChunkFile($file)
// Move the uploaded file to chunk folder
$this->file->move($this->getChunkDirectory(true), $this->chunkFileName);

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

protected function tryToBuildFullFileFromChunks()
{
return parent::tryToBuildFullFileFromChunks();
$percentage = floor((count($this->foundChunks)) / $this->handler()->getTotalChunks() * 100);
// We need to update the handler with correct percentage
$this->handler()->setPercentageDone($percentage);
$this->isLastChunk = $percentage >= 100;

return $this;
}

/**
Expand All @@ -98,7 +106,7 @@ protected function getSavedChunksFiles()
*/
protected function buildFullFileFromChunks()
{
$chunkFiles = $this->getSavedChunksFiles()->all();
$chunkFiles = $this->foundChunks;

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

// Get chunk files that matches the current chunk file name, also sort the chunk
// files.
$finalFilePath = $this->getChunkDirectory(true).'./'.$this->handler()->createChunkFileName();
$rootDirectory = $this->getChunkDirectory(true);
$finalFilePath = $rootDirectory.'./'.$this->handler()->createChunkFileName();

// Delete the file if exists
if (file_exists($finalFilePath)) {
@unlink($finalFilePath);
Expand Down

0 comments on commit a979029

Please sign in to comment.