Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Driver/BlueimpUploadDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function save(Request $request, StorageConfig $config, Closure $fileUploa
try {
$range = new ContentRange($request->headers);
} catch (InvalidArgumentException $e) {
throw new BadRequestHttpException($e->getMessage());
throw new BadRequestHttpException($e->getMessage(), $e);
}

$filename = $this->identifier->generateUploadedFileIdentifierName($file);
Expand Down
49 changes: 16 additions & 33 deletions src/Driver/DropzoneUploadDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use InvalidArgumentException;
use LaraCrafts\ChunkUploader\Helper\ChunkHelpers;
use LaraCrafts\ChunkUploader\Identifier\Identifier;
use LaraCrafts\ChunkUploader\Range\RequestRange;
use LaraCrafts\ChunkUploader\Range\RequestBodyRange;
use LaraCrafts\ChunkUploader\Response\PercentageJsonResponse;
use LaraCrafts\ChunkUploader\StorageConfig;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

class DropzoneUploadDriver extends UploadDriver
Expand Down Expand Up @@ -124,14 +126,17 @@ private function saveMonolith(UploadedFile $file, StorageConfig $config, Closure
*/
private function saveChunk(UploadedFile $file, Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
{
$numberOfChunks = $request->post('dztotalchunkcount');

$range = new RequestRange(
$request->post('dzchunkindex'),
$numberOfChunks,
$request->post('dzchunksize'),
$request->post('dztotalfilesize')
);
try {
$range = new RequestBodyRange(
$request,
'dzchunkindex',
'dztotalchunkcount',
'dzchunksize',
'dztotalfilesize'
);
} catch (InvalidArgumentException $e) {
throw new BadRequestHttpException($e->getMessage(), $e);
}

$filename = $request->post('dzuuid');

Expand All @@ -142,8 +147,8 @@ private function saveChunk(UploadedFile $file, Request $request, StorageConfig $

$chunks = $this->storeChunk($config, $range, $file, $filename);

if (!$this->isFinished($numberOfChunks, $chunks)) {
return new PercentageJsonResponse($this->getPercentage($chunks, $numberOfChunks));
if (!$range->isFinished($chunks)) {
return new PercentageJsonResponse($range->getPercentage($chunks));
}

$path = $this->mergeChunks($config, $chunks, $filename);
Expand All @@ -156,26 +161,4 @@ private function saveChunk(UploadedFile $file, Request $request, StorageConfig $

return new PercentageJsonResponse(100);
}

/**
* @param $numberOfChunks
* @param $chunks
*
* @return bool
*/
private function isFinished($numberOfChunks, $chunks)
{
return $numberOfChunks === count($chunks);
}

/**
* @param array $chunks
* @param $numberOfChunks
*
* @return float|int
*/
private function getPercentage(array $chunks, $numberOfChunks)
{
return floor(count($chunks) / $numberOfChunks * 100);
}
}
129 changes: 129 additions & 0 deletions src/Range/RequestBodyRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace LaraCrafts\ChunkUploader\Range;

use Illuminate\Http\Request;
use InvalidArgumentException;

class RequestBodyRange implements Range
{
private $index;

private $numberOfChunks;

private $totalSize;

private $chunkSize;

/**
* RequestRange constructor.
*
* @param \Illuminate\Http\Request|\Symfony\Component\HttpFoundation\ParameterBag $request
* @param string $indexKey The index of the current chunk
* @param string $numberOfChunksKey The total number of the chunks
* @param string $chunkSizeKey The size of the chunk
* @param string $totalSizeKey The size of the original file
*/
public function __construct($request, string $indexKey, string $numberOfChunksKey, string $chunkSizeKey, string $totalSizeKey)
{
if ($request instanceof Request) {
$request = $request->request;
}

$this->index = $request->get($indexKey);
$this->numberOfChunks = $request->get($numberOfChunksKey);
$this->chunkSize = $request->get($chunkSizeKey);
$this->totalSize = $request->get($totalSizeKey);

if ($this->numberOfChunks <= 0) {
throw new InvalidArgumentException(sprintf('`%s` must be greater than zero', $numberOfChunksKey));
}
if ($this->index < 0) {
throw new InvalidArgumentException(sprintf('`%s` must be greater than or equal to zero', $indexKey));
}
if ($this->index >= $this->numberOfChunks) {
throw new InvalidArgumentException(sprintf('`%s` must be smaller than `%s`', $indexKey, $numberOfChunksKey));
}
if ($this->chunkSize < 1) {
throw new InvalidArgumentException(sprintf('`%s` must be greater than zero', $chunkSizeKey));
}
if ($this->totalSize < 1) {
throw new InvalidArgumentException(sprintf('`%s` must be greater than zero', $totalSizeKey));
} elseif ($this->totalSize <= $this->index * $this->chunkSize) {
throw new InvalidArgumentException(
sprintf('`%s` must be greater than the multiple of `%s` and `%s`', $totalSizeKey, $chunkSizeKey, $indexKey)
);
} elseif ($this->totalSize > $this->numberOfChunks * $this->chunkSize) {
throw new InvalidArgumentException(
sprintf('`%s` must be smaller than or equal to the multiple of `%s` and `%s`', $totalSizeKey, $chunkSizeKey, $numberOfChunksKey)
);
}
}

/**
* {@inheritDoc}
*/
public function getStart(): float
{
return $this->index * $this->chunkSize;
}

/**
* {@inheritDoc}
*/
public function getEnd(): float
{
$end = (($this->index + 1) * $this->chunkSize) - 1;

$sizeIndex = $this->totalSize - 1;
if ($end > ($sizeIndex)) {
return $sizeIndex;
}

return $end;
}

/**
* {@inheritDoc}
*/
public function getTotal(): float
{
return $this->totalSize;
}

/**
* {@inheritDoc}
*/
public function isFirst(): bool
{
return $this->index === 0;
}

/**
* {@inheritDoc}
*/
public function isLast(): bool
{
return $this->index === $this->numberOfChunks - 1;
}

/**
* @param $uploadedChunks
*
* @return float
*/
public function getPercentage($uploadedChunks): float
{
return floor(count($uploadedChunks) / $this->numberOfChunks * 100);
}

/**
* @param $uploadedChunks
*
* @return bool
*/
public function isFinished($uploadedChunks): bool
{
return $this->numberOfChunks === count($uploadedChunks);
}
}
76 changes: 0 additions & 76 deletions src/Range/RequestRange.php

This file was deleted.

Loading