Skip to content

Commit 85fec50

Browse files
L3o-poldpionl
authored andcommitted
Add ng-file-upload handler (#39)
* Add ng-file-upload handler * Update readme for the support of ng-file-upload * Add unit test for NgFileUpload handler * Fix ngfileupload integer params verification
1 parent 93915fb commit 85fec50

File tree

8 files changed

+362
-4
lines changed

8 files changed

+362
-4
lines changed

phpunit.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
convertWarningsToExceptions="true"
88
processIsolation="false"
99
stopOnFailure="false"
10-
syntaxCheck="false">
10+
syntaxCheck="false"
11+
bootstrap="vendor/autoload.php">
1112
<testsuites>
1213
<testsuite name="Application Test Suite">
1314
<directory>./tests/</directory>

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Setup is composed in 3 steps:
4747
| [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: |
4848
| [Plupload](https://github.com/moxiecode/plupload) | [Wiki](https://github.com/pionl/laravel-chunk-upload/wiki/plupload) | :heavy_check_mark: | :heavy_multiplication_x: |
4949
| [simple uploader](https://github.com/simple-uploader) | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: |
50+
| [ng-file-upload](https://github.com/danialfarid/ng-file-upload) | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: |
5051

5152
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).
5253

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Pion\Laravel\ChunkUpload\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* Class ChunkInvalidValueException
9+
*
10+
* @package Pion\Laravel\ChunkUpload\Exception
11+
*/
12+
class ChunkInvalidValueException extends \Exception
13+
{
14+
/**
15+
* ChunkInvalidValueException constructor.
16+
*
17+
* @param string $message
18+
* @param int $code
19+
* @param Exception|null $previous
20+
*/
21+
public function __construct(
22+
$message = 'The chunk parameters are invalid',
23+
$code = 500,
24+
Exception $previous = null
25+
) {
26+
parent::__construct($message, $code, $previous);
27+
}
28+
}

src/Handler/ChunksInRequestSimpleUploadHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Pion\Laravel\ChunkUpload\Handler;
33

4-
54
/**
65
* Class ChunksInRequestSimpleUploadHandler
76
*

src/Handler/HandlerFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class HandlerFactory
1414
ChunksInRequestUploadHandler::class,
1515
ResumableJSUploadHandler::class,
1616
DropZoneUploadHandler::class,
17-
ChunksInRequestSimpleUploadHandler::class
17+
ChunksInRequestSimpleUploadHandler::class,
18+
NgFileUploadHandler::class,
1819
);
1920

2021
/**

src/Handler/NgFileUploadHandler.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace Pion\Laravel\ChunkUpload\Handler;
4+
5+
use Pion\Laravel\ChunkUpload\Exceptions\ChunkInvalidValueException;
6+
use Illuminate\Http\Request;
7+
8+
/**
9+
* Class NgFileUploadHandler
10+
*
11+
* Upload receiver that detects the content range from he request value - chunks
12+
* Works with:
13+
* - ng-file-upload: https://github.com/danialfarid/ng-file-upload
14+
*
15+
* @package Pion\Laravel\ChunkUpload\Handler
16+
*/
17+
class NgFileUploadHandler extends ChunksInRequestUploadHandler
18+
{
19+
20+
/**
21+
* Key for number of sending chunk
22+
*
23+
* @static string
24+
*/
25+
const KEY_CHUNK_NUMBER = '_chunkNumber';
26+
27+
/**
28+
* Key for total size of all chunks
29+
*
30+
* @static string
31+
*/
32+
const KEY_TOTAL_SIZE = '_totalSize';
33+
34+
/**
35+
* Key for every chunk size
36+
*
37+
* @static string
38+
*/
39+
const KEY_CHUNK_SIZE = '_chunkSize';
40+
41+
/**
42+
* Key for current chunk size
43+
*
44+
* @static string
45+
*/
46+
const KEY_CHUNK_CURRENT_SIZE = '_currentChunkSize';
47+
48+
/**
49+
* Determines if the upload is via chunked upload
50+
*
51+
* @var bool
52+
*/
53+
protected $chunkedUpload = false;
54+
55+
/**
56+
* Checks if the current handler can be used via HandlerFactory
57+
*
58+
* @param Request $request
59+
*
60+
* @return bool
61+
* @throws ChunkInvalidValueException
62+
*/
63+
public static function canBeUsedForRequest(Request $request)
64+
{
65+
$hasChunkParams = $request->has(static::KEY_CHUNK_NUMBER)
66+
&& $request->has(static::KEY_TOTAL_SIZE)
67+
&& $request->has(static::KEY_CHUNK_SIZE)
68+
&& $request->has(static::KEY_CHUNK_CURRENT_SIZE);
69+
70+
return $hasChunkParams && self::checkChunkParams($request);
71+
}
72+
73+
/**
74+
* @return int
75+
*/
76+
public function getPercentageDone()
77+
{
78+
// Check that we have received total chunks
79+
if (!$this->chunksTotal) {
80+
return 0;
81+
}
82+
83+
return intval(parent::getPercentageDone());
84+
}
85+
86+
/**
87+
* @param Request $request
88+
*
89+
* @return bool
90+
* @throws ChunkInvalidValueException
91+
*/
92+
protected static function checkChunkParams($request)
93+
{
94+
$isInteger = ctype_digit($request->input(static::KEY_CHUNK_NUMBER))
95+
&& ctype_digit($request->input(static::KEY_TOTAL_SIZE))
96+
&& ctype_digit($request->input(static::KEY_CHUNK_SIZE))
97+
&& ctype_digit($request->input(static::KEY_CHUNK_CURRENT_SIZE));
98+
99+
if ($request->get(static::KEY_CHUNK_SIZE) < $request->get(static::KEY_CHUNK_CURRENT_SIZE)) {
100+
throw new ChunkInvalidValueException();
101+
}
102+
103+
if ($request->get(static::KEY_CHUNK_NUMBER) < 0) {
104+
throw new ChunkInvalidValueException();
105+
}
106+
107+
if ($request->get(static::KEY_TOTAL_SIZE) < 0) {
108+
throw new ChunkInvalidValueException();
109+
}
110+
111+
return $isInteger;
112+
}
113+
114+
/**
115+
* Returns current chunk from the request
116+
*
117+
* @param Request $request
118+
*
119+
* @return int
120+
*/
121+
protected function getTotalChunksFromRequest(Request $request)
122+
{
123+
if (!$request->get(static::KEY_CHUNK_SIZE)) {
124+
return 0;
125+
}
126+
127+
return intval(
128+
ceil($request->get(static::KEY_TOTAL_SIZE) / $request->get(static::KEY_CHUNK_SIZE))
129+
);
130+
}
131+
}

src/Handler/SingleUploadHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class SingleUploadHandler extends AbstractHandler
2525
*/
2626
public function startSaving($chunkStorage)
2727
{
28-
return new SingleSave($this->file, $this, $this->config);
28+
return new SingleSave($this->file, $this, $this->config);
2929
}
3030

3131
/**

0 commit comments

Comments
 (0)