Skip to content

Commit a32bfdd

Browse files
authored
Add identifier to handlers based on Flow.js (#65)
* Add identifier * Fix style
1 parent 78bd2a1 commit a32bfdd

File tree

7 files changed

+39
-26
lines changed

7 files changed

+39
-26
lines changed

src/Driver/FlowJsUploadDriver.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
namespace CodingSocks\ChunkUploader\Driver;
44

5+
use CodingSocks\ChunkUploader\Identifier\Identifier;
6+
57
class FlowJsUploadDriver extends ResumableJsUploadDriver
68
{
7-
/**
8-
* ResumableJsUploadDriver constructor.
9-
*
10-
* @param array $config
11-
*/
12-
public function __construct($config)
9+
public function __construct($config, Identifier $identifier)
1310
{
1411
$config['parameter-namespace'] = '';
1512
$config['parameter-names'] = [
@@ -30,6 +27,6 @@ public function __construct($config)
3027
// The name of the current chunk size POST parameter to use for the file chunk.
3128
'current-chunk-size' => 'flowCurrentChunkSize',
3229
];
33-
parent::__construct($config);
30+
parent::__construct($config, $identifier);
3431
}
3532
}

src/Driver/ResumableJsUploadDriver.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Closure;
66
use CodingSocks\ChunkUploader\Helper\ChunkHelpers;
7+
use CodingSocks\ChunkUploader\Identifier\Identifier;
78
use CodingSocks\ChunkUploader\Range\ResumableJsRange;
89
use CodingSocks\ChunkUploader\Response\PercentageJsonResponse;
910
use CodingSocks\ChunkUploader\StorageConfig;
@@ -24,6 +25,11 @@ class ResumableJsUploadDriver extends UploadDriver
2425
*/
2526
private $fileParam;
2627

28+
/**
29+
* @var \CodingSocks\ChunkUploader\Identifier\Identifier
30+
*/
31+
private $identifier;
32+
2733
/**
2834
* @var string
2935
*/
@@ -48,10 +54,12 @@ class ResumableJsUploadDriver extends UploadDriver
4854
* ResumableJsUploadDriver constructor.
4955
*
5056
* @param array $config
57+
* @param \CodingSocks\ChunkUploader\Identifier\Identifier $identifier
5158
*/
52-
public function __construct($config)
59+
public function __construct($config, Identifier $identifier)
5360
{
5461
$this->fileParam = $config['param'];
62+
$this->identifier = $identifier;
5563

5664
$this->uploadMethod = $config['upload-method'];
5765
$this->testMethod = $config['test-method'];
@@ -101,10 +109,10 @@ public function resume(Request $request, StorageConfig $config): Response
101109
throw new BadRequestHttpException($e->getMessage(), $e);
102110
}
103111

104-
$filename = $request->query($this->buildParameterName('identifier'));
112+
$uid = $this->identifier->generateIdentifier($request->query($this->buildParameterName('identifier')));
105113
$chunkname = $this->buildChunkname($range);
106114

107-
if (! $this->chunkExists($config, $filename, $chunkname)) {
115+
if (! $this->chunkExists($config, $uid, $chunkname)) {
108116
return new Response('', Response::HTTP_NO_CONTENT);
109117
}
110118

@@ -165,9 +173,10 @@ private function saveChunk(UploadedFile $file, Request $request, StorageConfig $
165173
throw new BadRequestHttpException($e->getMessage(), $e);
166174
}
167175

168-
$uuid = $request->post($this->buildParameterName('identifier'));
176+
$weakId = $request->post($this->buildParameterName('identifier'));
177+
$uid = $this->identifier->generateIdentifier($weakId);
169178

170-
$chunks = $this->storeChunk($config, $range, $file, $uuid);
179+
$chunks = $this->storeChunk($config, $range, $file, $uid);
171180

172181
if (!$range->isFinished($chunks)) {
173182
return new PercentageJsonResponse($range->getPercentage($chunks));
@@ -178,7 +187,7 @@ private function saveChunk(UploadedFile $file, Request $request, StorageConfig $
178187
$path = $this->mergeChunks($config, $chunks, $targetFilename);
179188

180189
if ($config->sweep()) {
181-
$this->deleteChunkDirectory($config, $uuid);
190+
$this->deleteChunkDirectory($config, $uid);
182191
}
183192

184193
$this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded);

src/Driver/SimpleUploaderJsUploadDriver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace CodingSocks\ChunkUploader\Driver;
44

5+
use CodingSocks\ChunkUploader\Identifier\Identifier;
6+
57
class SimpleUploaderJsUploadDriver extends ResumableJsUploadDriver
68
{
7-
public function __construct($config)
9+
public function __construct($config, Identifier $identifier)
810
{
911
$config['parameter-namespace'] = '';
1012
$config['parameter-names'] = [
@@ -25,6 +27,6 @@ public function __construct($config)
2527
// The name of the current chunk size POST parameter to use for the file chunk.
2628
'current-chunk-size' => 'currentChunkSize',
2729
];
28-
parent::__construct($config);
30+
parent::__construct($config, $identifier);
2931
}
3032
}

src/UploadManager.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,35 @@ public function createDropzoneDriver()
3434

3535
public function createFlowJsDriver()
3636
{
37-
return new FlowJsUploadDriver($this->app['config']['chunk-uploader.resumable-js']);
37+
return new FlowJsUploadDriver($this->app['config']['chunk-uploader.resumable-js'], $this->identityManager()->driver());
3838
}
3939

4040
public function createNgFileUploadDriver()
4141
{
42-
/** @var \Illuminate\Support\Manager $identityManager */
43-
$identityManager = $this->app['chunk-uploader.identity-manager'];
44-
45-
return new NgFileUploadDriver($identityManager->driver());
42+
return new NgFileUploadDriver($this->identityManager()->driver());
4643
}
4744

4845
public function createPluploadDriver()
4946
{
50-
/** @var \Illuminate\Support\Manager $identityManager */
51-
$identityManager = $this->app['chunk-uploader.identity-manager'];
52-
53-
return new PluploadUploadDriver($identityManager->driver());
47+
return new PluploadUploadDriver($this->identityManager()->driver());
5448
}
5549

5650
public function createResumableJsDriver()
5751
{
58-
return new ResumableJsUploadDriver($this->app['config']['chunk-uploader.resumable-js']);
52+
return new ResumableJsUploadDriver($this->app['config']['chunk-uploader.resumable-js'], $this->identityManager()->driver());
5953
}
6054

6155
public function createSimpleUploaderJsDriver()
6256
{
63-
return new SimpleUploaderJsUploadDriver($this->app['config']['chunk-uploader.simple-uploader-js']);
57+
return new SimpleUploaderJsUploadDriver($this->app['config']['chunk-uploader.simple-uploader-js'], $this->identityManager()->driver());
58+
}
59+
60+
/**
61+
* @return \Illuminate\Support\Manager
62+
*/
63+
protected function identityManager()
64+
{
65+
return $this->app['chunk-uploader.identity-manager'];
6466
}
6567

6668
/**

tests/Driver/FlowJsUploadDriverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected function setUp(): void
2828
{
2929
parent::setUp();
3030

31+
$this->app->make('config')->set('chunk-uploader.identifier', 'nop');
3132
$this->app->make('config')->set('chunk-uploader.uploader', 'flow-js');
3233
$this->app->make('config')->set('chunk-uploader.sweep', false);
3334
$this->handler = $this->app->make(UploadHandler::class);

tests/Driver/ResumableJsUploadDriverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected function setUp(): void
2828
{
2929
parent::setUp();
3030

31+
$this->app->make('config')->set('chunk-uploader.identifier', 'nop');
3132
$this->app->make('config')->set('chunk-uploader.uploader', 'resumable-js');
3233
$this->app->make('config')->set('chunk-uploader.sweep', false);
3334
$this->handler = $this->app->make(UploadHandler::class);

tests/Driver/SimpleUploaderUploadDriverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected function setUp(): void
2828
{
2929
parent::setUp();
3030

31+
$this->app->make('config')->set('chunk-uploader.identifier', 'nop');
3132
$this->app->make('config')->set('chunk-uploader.uploader', 'simple-uploader-js');
3233
$this->app->make('config')->set('chunk-uploader.sweep', false);
3334
$this->handler = $this->app->make(UploadHandler::class);

0 commit comments

Comments
 (0)