Skip to content

Commit 437b337

Browse files
nerg4laxlon
andauthored
Extend RequestBodyRange (#21)
* Make constructor more generic * Update src/Range/RequestBodyRange.php Co-Authored-By: Choraimy Kroonstuiver <choraimy@23g.nl> * Update tests/Range/RequestBodyRangeTest.php Co-Authored-By: Choraimy Kroonstuiver <choraimy@23g.nl> * Update tests/Range/RequestBodyRangeTest.php Co-Authored-By: Choraimy Kroonstuiver <choraimy@23g.nl> * Preserve exception Co-authored-by: Choraimy Kroonstuiver <choraimy@23g.nl>
1 parent 4c4312d commit 437b337

File tree

5 files changed

+292
-110
lines changed

5 files changed

+292
-110
lines changed

src/Driver/BlueimpUploadDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function save(Request $request, StorageConfig $config, Closure $fileUploa
136136
try {
137137
$range = new ContentRange($request->headers);
138138
} catch (InvalidArgumentException $e) {
139-
throw new BadRequestHttpException($e->getMessage());
139+
throw new BadRequestHttpException($e->getMessage(), $e);
140140
}
141141

142142
$filename = $this->identifier->generateUploadedFileIdentifierName($file);

src/Driver/DropzoneUploadDriver.php

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Http\UploadedFile;
88
use Illuminate\Support\Facades\Storage;
9+
use InvalidArgumentException;
910
use LaraCrafts\ChunkUploader\Helper\ChunkHelpers;
1011
use LaraCrafts\ChunkUploader\Identifier\Identifier;
11-
use LaraCrafts\ChunkUploader\Range\RequestRange;
12+
use LaraCrafts\ChunkUploader\Range\RequestBodyRange;
1213
use LaraCrafts\ChunkUploader\Response\PercentageJsonResponse;
1314
use LaraCrafts\ChunkUploader\StorageConfig;
1415
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1517
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1618

1719
class DropzoneUploadDriver extends UploadDriver
@@ -124,14 +126,17 @@ private function saveMonolith(UploadedFile $file, StorageConfig $config, Closure
124126
*/
125127
private function saveChunk(UploadedFile $file, Request $request, StorageConfig $config, Closure $fileUploaded = null): Response
126128
{
127-
$numberOfChunks = $request->post('dztotalchunkcount');
128-
129-
$range = new RequestRange(
130-
$request->post('dzchunkindex'),
131-
$numberOfChunks,
132-
$request->post('dzchunksize'),
133-
$request->post('dztotalfilesize')
134-
);
129+
try {
130+
$range = new RequestBodyRange(
131+
$request,
132+
'dzchunkindex',
133+
'dztotalchunkcount',
134+
'dzchunksize',
135+
'dztotalfilesize'
136+
);
137+
} catch (InvalidArgumentException $e) {
138+
throw new BadRequestHttpException($e->getMessage(), $e);
139+
}
135140

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

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

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

145-
if (!$this->isFinished($numberOfChunks, $chunks)) {
146-
return new PercentageJsonResponse($this->getPercentage($chunks, $numberOfChunks));
150+
if (!$range->isFinished($chunks)) {
151+
return new PercentageJsonResponse($range->getPercentage($chunks));
147152
}
148153

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

157162
return new PercentageJsonResponse(100);
158163
}
159-
160-
/**
161-
* @param $numberOfChunks
162-
* @param $chunks
163-
*
164-
* @return bool
165-
*/
166-
private function isFinished($numberOfChunks, $chunks)
167-
{
168-
return $numberOfChunks === count($chunks);
169-
}
170-
171-
/**
172-
* @param array $chunks
173-
* @param $numberOfChunks
174-
*
175-
* @return float|int
176-
*/
177-
private function getPercentage(array $chunks, $numberOfChunks)
178-
{
179-
return floor(count($chunks) / $numberOfChunks * 100);
180-
}
181164
}

src/Range/RequestBodyRange.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace LaraCrafts\ChunkUploader\Range;
4+
5+
use Illuminate\Http\Request;
6+
use InvalidArgumentException;
7+
8+
class RequestBodyRange implements Range
9+
{
10+
private $index;
11+
12+
private $numberOfChunks;
13+
14+
private $totalSize;
15+
16+
private $chunkSize;
17+
18+
/**
19+
* RequestRange constructor.
20+
*
21+
* @param \Illuminate\Http\Request|\Symfony\Component\HttpFoundation\ParameterBag $request
22+
* @param string $indexKey The index of the current chunk
23+
* @param string $numberOfChunksKey The total number of the chunks
24+
* @param string $chunkSizeKey The size of the chunk
25+
* @param string $totalSizeKey The size of the original file
26+
*/
27+
public function __construct($request, string $indexKey, string $numberOfChunksKey, string $chunkSizeKey, string $totalSizeKey)
28+
{
29+
if ($request instanceof Request) {
30+
$request = $request->request;
31+
}
32+
33+
$this->index = $request->get($indexKey);
34+
$this->numberOfChunks = $request->get($numberOfChunksKey);
35+
$this->chunkSize = $request->get($chunkSizeKey);
36+
$this->totalSize = $request->get($totalSizeKey);
37+
38+
if ($this->numberOfChunks <= 0) {
39+
throw new InvalidArgumentException(sprintf('`%s` must be greater than zero', $numberOfChunksKey));
40+
}
41+
if ($this->index < 0) {
42+
throw new InvalidArgumentException(sprintf('`%s` must be greater than or equal to zero', $indexKey));
43+
}
44+
if ($this->index >= $this->numberOfChunks) {
45+
throw new InvalidArgumentException(sprintf('`%s` must be smaller than `%s`', $indexKey, $numberOfChunksKey));
46+
}
47+
if ($this->chunkSize < 1) {
48+
throw new InvalidArgumentException(sprintf('`%s` must be greater than zero', $chunkSizeKey));
49+
}
50+
if ($this->totalSize < 1) {
51+
throw new InvalidArgumentException(sprintf('`%s` must be greater than zero', $totalSizeKey));
52+
} elseif ($this->totalSize <= $this->index * $this->chunkSize) {
53+
throw new InvalidArgumentException(
54+
sprintf('`%s` must be greater than the multiple of `%s` and `%s`', $totalSizeKey, $chunkSizeKey, $indexKey)
55+
);
56+
} elseif ($this->totalSize > $this->numberOfChunks * $this->chunkSize) {
57+
throw new InvalidArgumentException(
58+
sprintf('`%s` must be smaller than or equal to the multiple of `%s` and `%s`', $totalSizeKey, $chunkSizeKey, $numberOfChunksKey)
59+
);
60+
}
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*/
66+
public function getStart(): float
67+
{
68+
return $this->index * $this->chunkSize;
69+
}
70+
71+
/**
72+
* {@inheritDoc}
73+
*/
74+
public function getEnd(): float
75+
{
76+
$end = (($this->index + 1) * $this->chunkSize) - 1;
77+
78+
$sizeIndex = $this->totalSize - 1;
79+
if ($end > ($sizeIndex)) {
80+
return $sizeIndex;
81+
}
82+
83+
return $end;
84+
}
85+
86+
/**
87+
* {@inheritDoc}
88+
*/
89+
public function getTotal(): float
90+
{
91+
return $this->totalSize;
92+
}
93+
94+
/**
95+
* {@inheritDoc}
96+
*/
97+
public function isFirst(): bool
98+
{
99+
return $this->index === 0;
100+
}
101+
102+
/**
103+
* {@inheritDoc}
104+
*/
105+
public function isLast(): bool
106+
{
107+
return $this->index === $this->numberOfChunks - 1;
108+
}
109+
110+
/**
111+
* @param $uploadedChunks
112+
*
113+
* @return float
114+
*/
115+
public function getPercentage($uploadedChunks): float
116+
{
117+
return floor(count($uploadedChunks) / $this->numberOfChunks * 100);
118+
}
119+
120+
/**
121+
* @param $uploadedChunks
122+
*
123+
* @return bool
124+
*/
125+
public function isFinished($uploadedChunks): bool
126+
{
127+
return $this->numberOfChunks === count($uploadedChunks);
128+
}
129+
}

src/Range/RequestRange.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)