Skip to content

Commit

Permalink
fix chunk count
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Apr 30, 2023
1 parent 7d05f9c commit 5c0886d
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=YNNLC9V28YDPN)

## Pure Ajax Upload And for Laravel 5 (Support jQuery-File-Upload, FileApi, Plupload)
## Pure Ajax Upload And for Laravel (Support jQuery-File-Upload, FileApi, Plupload)

[![StyleCI](https://styleci.io/repos/48772854/shield?style=flat)](https://styleci.io/repos/48772854)
[![Build Status](https://travis-ci.org/recca0120/upload.svg)](https://travis-ci.org/recca0120/upload)
Expand Down
8 changes: 3 additions & 5 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,9 @@ public function cleanDirectory(string $path): Api
*/
public function receive(string $name)
{
if (! $this->isChunked($name)) {
return $this->request->file($name);
}

return $this->receiveChunked($name);
return $this->isChunked($name)
? $this->receiveChunked($name)
: $this->request->file($name);
}

public function deleteUploadedFile($uploadedFile)
Expand Down
14 changes: 8 additions & 6 deletions src/Dropzone.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,27 @@ protected function isChunked(string $name): bool

protected function isCompleted(string $name): bool
{
$totalparts = (int) $this->request->get('dztotalchunkcount', 1);
$partindex = (int) $this->request->get('dzchunkindex');
$totalChunkCount = (int) $this->request->get('dztotalchunkcount', 1);
$chunkIndex = (int) $this->request->get('dzchunkindex');

return $totalparts - 1 === $partindex;
return $totalChunkCount - 1 === $chunkIndex;
}

protected function receiveChunked(string $name)
{
$uploadedFile = $this->request->file($name);
$originalName = $uploadedFile->getClientOriginalName();
$totalparts = (int) $this->request->get('dztotalchunkcount', 1);
$partindex = (int) $this->request->get('dzchunkindex');
$uuid = $this->request->get('dzuuid');

$chunkFile = $this->createChunkFile($originalName, $uuid);
$chunkFile->appendFile($uploadedFile->getRealPath(), $partindex);
$chunkFile->appendFile(
$uploadedFile->getRealPath(),
(int) $this->request->get('dzchunkindex')
);

if (! $this->isCompleted($name)) {
throw new ChunkedResponseException(['success' => true, 'uuid' => $uuid], []);
throw new ChunkedResponseException(['success' => true, 'uuid' => $uuid]);
}

return $chunkFile->createUploadedFile($totalparts);
Expand Down
2 changes: 1 addition & 1 deletion src/FilePond.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function isCompleted(string $name): bool
$size = (int) $this->request->header('Upload-Length');
$length = (int) $this->request->header('Content-Length');

return $size === $offset + $length;
return $offset + $length >= $size;
}

protected function receiveChunked(string $name)
Expand Down
9 changes: 3 additions & 6 deletions src/FineUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,16 @@ protected function isCompleted(string $name): bool
protected function receiveChunked(string $name)
{
$uploadedFile = $this->request->file($name);
$originalName = $this->request->get('qqfilename');
$totalparts = (int) $this->request->get('qqtotalparts', 1);
$partindex = (int) $this->request->get('qqpartindex');
$uuid = $this->request->get('qquuid');

$chunkFile = $this->createChunkFile($originalName, $uuid);
$chunkFile = $this->createChunkFile($this->request->get('qqfilename'), $uuid);

if (! $this->isCompleted($name)) {
$chunkFile->appendFile($uploadedFile->getRealPath(), $partindex);
$chunkFile->appendFile($uploadedFile->getRealPath(), (int) $this->request->get('qqpartindex'));

throw new ChunkedResponseException(['success' => true, 'uuid' => $uuid]);
}

return $chunkFile->createUploadedFile($totalparts);
return $chunkFile->createUploadedFile((int) $this->request->get('qqtotalparts', 1));
}
}
1 change: 0 additions & 1 deletion tests/FilePondTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function testReceiveChunkedFileAndThrowUniqIdChunkedResponseException():
public function testReceiveChunkedFile(): void
{
$size = $this->uploadedFile->getSize();

$this->request->replace(['file' => '{}']);
$this->request->headers->replace(['Upload-Length' => $this->uploadedFile->getSize()]);
$uuid = '';
Expand Down
26 changes: 10 additions & 16 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,23 @@ protected function setUp(): void
/**
* @throws ReflectionException
*/
protected function chunkUpload(int $chunks, ?callable $callback): void
protected function chunkUpload(int $chunks, callable $callback): void
{
$content = $this->uploadedFile->getContent();
$size = $this->uploadedFile->getSize();
$remainder = $size % $chunks;
$chunkSize = ($size - $remainder) / $chunks;
$totalCount = (int) ceil($size / $chunkSize);
$chunkLength = ($size - ($size % $chunks)) / $chunks + ($size % $chunks);

$offset = 0;
for ($i = 0; $i < $chunks; $i++) {
$index = 0;
do {
$chunkSize = min($chunkLength, $size - $offset);
$this->setRequestContent(substr($content, $offset, $chunkSize));
if (is_callable($callback)) {
$callback($offset, $chunkSize, $i, $totalCount);
}
$offset = (($i + 1) * $chunkSize);
}
if ($remainder > 0) {
$this->setRequestContent(substr($content, $offset, $remainder));
if (is_callable($callback)) {
$callback($offset, $remainder, $i, $totalCount);
}
}
$callback($offset, $chunkSize, $index, $chunks);
$offset += $chunkLength;
$index++;
} while ($offset <= $size);
$this->setRequestContent($content);
self::assertEquals($chunks, $index);
}

/**
Expand Down

0 comments on commit 5c0886d

Please sign in to comment.