Skip to content

Commit

Permalink
make use of ResumableTotalChunks requestParam
Browse files Browse the repository at this point in the history
  • Loading branch information
abilogos committed Dec 14, 2023
1 parent 988948f commit 72f438c
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/Resumable.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class Resumable
'filename' => 'filename',
'chunkNumber' => 'chunkNumber',
'chunkSize' => 'chunkSize',
'totalSize' => 'totalSize'
'totalSize' => 'totalSize',
'totalChunks' => 'totalChunks'
];

const WITHOUT_EXTENSION = true;
Expand Down Expand Up @@ -178,12 +179,12 @@ public function handleTestChunk()
$filename = $this->resumableParam($this->resumableOption['filename']);
$chunkNumber = $this->resumableParam($this->resumableOption['chunkNumber']);
$chunkSize = $this->resumableParam($this->resumableOption['chunkSize']);
$totalSize = $this->resumableParam($this->resumableOption['totalSize']);
$totalChunks = $this->resumableParam($this->resumableOption['totalChunks']);

if (!$this->isChunkUploaded($identifier, $filename, $chunkNumber)) {
return $this->response->header(204);
} else {
if ($this->isFileUploadComplete($filename, $identifier, $chunkSize, $totalSize)) {
if ($this->isFileUploadComplete($filename, $identifier, $totalChunks)) {
$this->isUploadComplete = true;
$this->createFileAndDeleteTmp($identifier, $filename);
return $this->response->header(201);
Expand All @@ -200,14 +201,14 @@ public function handleChunk()
$filename = $this->resumableParam($this->resumableOption['filename']);
$chunkNumber = $this->resumableParam($this->resumableOption['chunkNumber']);
$chunkSize = $this->resumableParam($this->resumableOption['chunkSize']);
$totalSize = $this->resumableParam($this->resumableOption['totalSize']);
$totalChunks = $this->resumableParam($this->resumableOption['totalChunks']);

if (!$this->isChunkUploaded($identifier, $filename, $chunkNumber)) {
$chunkFile = $this->tmpChunkDir($identifier) . DIRECTORY_SEPARATOR . $this->tmpChunkFilename($filename, $chunkNumber);
$this->moveUploadedFile($file['tmp_name'], $chunkFile);
}

if ($this->isFileUploadComplete($filename, $identifier, $chunkSize, $totalSize)) {
if ($this->isFileUploadComplete($filename, $identifier, $totalChunks)) {
$this->isUploadComplete = true;
$this->createFileAndDeleteTmp($identifier, $filename);
return $this->response->header(201);
Expand Down Expand Up @@ -265,13 +266,9 @@ public function resumableParams()
}
}

public function isFileUploadComplete($filename, $identifier, $chunkSize, $totalSize)
public function isFileUploadComplete($filename, $identifier, $totalChunks)
{
if ($chunkSize <= 0) {
return false;
}
$numOfChunks = intval($totalSize / $chunkSize) + ($totalSize % $chunkSize == 0 ? 0 : 1);
for ($i = 1; $i <= $numOfChunks; $i++) {
for ($i = 1; $i <= $totalChunks; $i++) {
if (!$this->isChunkUploaded($identifier, $filename, $i)) {
return false;
}
Expand Down

0 comments on commit 72f438c

Please sign in to comment.