-
Notifications
You must be signed in to change notification settings - Fork 4
Extend RequestBodyRange #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nerg4l
merged 6 commits into
coding-socks:master
from
nerg4l:feature/request-body-range
Feb 14, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d5b9ffe
Make constructor more generic
nerg4l a5792de
Update src/Range/RequestBodyRange.php
nerg4l 7f7fdc9
Update tests/Range/RequestBodyRangeTest.php
nerg4l 33ef221
Update tests/Range/RequestBodyRangeTest.php
nerg4l 6eb9042
Merge branch 'master' into feature/request-body-range
nerg4l 63ca7ea
Preserve exception
nerg4l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.