Skip to content
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

Add ng-file-upload handler #39

Merged
merged 6 commits into from
May 30, 2018
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
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
syntaxCheck="false"
bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Setup is composed in 3 steps:
| [jQuery-File-Upload](https://github.com/blueimp/jQuery-File-Upload) | [Wiki](https://github.com/pionl/laravel-chunk-upload/wiki/blueimp-file-upload) | :heavy_check_mark: | :heavy_multiplication_x: |
| [Plupload](https://github.com/moxiecode/plupload) | [Wiki](https://github.com/pionl/laravel-chunk-upload/wiki/plupload) | :heavy_check_mark: | :heavy_multiplication_x: |
| [simple uploader](https://github.com/simple-uploader) | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: |
| [ng-file-upload](https://github.com/danialfarid/ng-file-upload) | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: |

For more detailed information (tips) use the [Wiki](https://github.com/pionl/laravel-chunk-upload/wiki) or for working example continue to separate repository with [example](https://github.com/pionl/laravel-chunk-upload-example).

Expand Down
28 changes: 28 additions & 0 deletions src/Exceptions/ChunkInvalidValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Pion\Laravel\ChunkUpload\Exceptions;

use Exception;

/**
* Class ChunkInvalidValueException
*
* @package Pion\Laravel\ChunkUpload\Exception
*/
class ChunkInvalidValueException extends \Exception
{
/**
* ChunkInvalidValueException constructor.
*
* @param string $message
* @param int $code
* @param Exception|null $previous
*/
public function __construct(
$message = 'The chunk parameters are invalid',
$code = 500,
Exception $previous = null
) {
parent::__construct($message, $code, $previous);
}
}
1 change: 0 additions & 1 deletion src/Handler/ChunksInRequestSimpleUploadHandler.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Pion\Laravel\ChunkUpload\Handler;


/**
* Class ChunksInRequestSimpleUploadHandler
*
Expand Down
3 changes: 2 additions & 1 deletion src/Handler/HandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class HandlerFactory
ChunksInRequestUploadHandler::class,
ResumableJSUploadHandler::class,
DropZoneUploadHandler::class,
ChunksInRequestSimpleUploadHandler::class
ChunksInRequestSimpleUploadHandler::class,
NgFileUploadHandler::class,
);

/**
Expand Down
131 changes: 131 additions & 0 deletions src/Handler/NgFileUploadHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace Pion\Laravel\ChunkUpload\Handler;

use Pion\Laravel\ChunkUpload\Exceptions\ChunkInvalidValueException;
use Illuminate\Http\Request;

/**
* Class NgFileUploadHandler
*
* Upload receiver that detects the content range from he request value - chunks
* Works with:
* - ng-file-upload: https://github.com/danialfarid/ng-file-upload
*
* @package Pion\Laravel\ChunkUpload\Handler
*/
class NgFileUploadHandler extends ChunksInRequestUploadHandler
{

/**
* Key for number of sending chunk
*
* @static string
*/
const KEY_CHUNK_NUMBER = '_chunkNumber';

/**
* Key for total size of all chunks
*
* @static string
*/
const KEY_TOTAL_SIZE = '_totalSize';

/**
* Key for every chunk size
*
* @static string
*/
const KEY_CHUNK_SIZE = '_chunkSize';

/**
* Key for current chunk size
*
* @static string
*/
const KEY_CHUNK_CURRENT_SIZE = '_currentChunkSize';

/**
* Determines if the upload is via chunked upload
*
* @var bool
*/
protected $chunkedUpload = false;

/**
* Checks if the current handler can be used via HandlerFactory
*
* @param Request $request
*
* @return bool
* @throws ChunkInvalidValueException
*/
public static function canBeUsedForRequest(Request $request)
{
$hasChunkParams = $request->has(static::KEY_CHUNK_NUMBER)
&& $request->has(static::KEY_TOTAL_SIZE)
&& $request->has(static::KEY_CHUNK_SIZE)
&& $request->has(static::KEY_CHUNK_CURRENT_SIZE);

return $hasChunkParams && self::checkChunkParams($request);
}

/**
* @return int
*/
public function getPercentageDone()
{
// Check that we have received total chunks
if (!$this->chunksTotal) {
return 0;
}

return intval(parent::getPercentageDone());
}

/**
* @param Request $request
*
* @return bool
* @throws ChunkInvalidValueException
*/
protected static function checkChunkParams($request)
{
$isInteger = ctype_digit($request->input(static::KEY_CHUNK_NUMBER))
&& ctype_digit($request->input(static::KEY_TOTAL_SIZE))
&& ctype_digit($request->input(static::KEY_CHUNK_SIZE))
&& ctype_digit($request->input(static::KEY_CHUNK_CURRENT_SIZE));

if ($request->get(static::KEY_CHUNK_SIZE) < $request->get(static::KEY_CHUNK_CURRENT_SIZE)) {
throw new ChunkInvalidValueException();
}

if ($request->get(static::KEY_CHUNK_NUMBER) < 0) {
throw new ChunkInvalidValueException();
}

if ($request->get(static::KEY_TOTAL_SIZE) < 0) {
throw new ChunkInvalidValueException();
}

return $isInteger;
}

/**
* Returns current chunk from the request
*
* @param Request $request
*
* @return int
*/
protected function getTotalChunksFromRequest(Request $request)
{
if (!$request->get(static::KEY_CHUNK_SIZE)) {
return 0;
}

return intval(
ceil($request->get(static::KEY_TOTAL_SIZE) / $request->get(static::KEY_CHUNK_SIZE))
);
}
}
2 changes: 1 addition & 1 deletion src/Handler/SingleUploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SingleUploadHandler extends AbstractHandler
*/
public function startSaving($chunkStorage)
{
return new SingleSave($this->file, $this, $this->config);
return new SingleSave($this->file, $this, $this->config);
}

/**
Expand Down
Loading