From 905f37544552fbf792efbba94a7f8e9f03eacca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Sat, 18 Jul 2020 12:51:31 +0200 Subject: [PATCH 1/2] Add identifier --- src/Driver/FlowJsUploadDriver.php | 11 ++++----- src/Driver/ResumableJsUploadDriver.php | 21 ++++++++++++----- src/Driver/SimpleUploaderJsUploadDriver.php | 6 +++-- src/UploadManager.php | 23 ++++++++++--------- tests/Driver/FlowJsUploadDriverTest.php | 1 + tests/Driver/ResumableJsUploadDriverTest.php | 1 + .../Driver/SimpleUploaderUploadDriverTest.php | 1 + 7 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/Driver/FlowJsUploadDriver.php b/src/Driver/FlowJsUploadDriver.php index f17d9c7..57c9b3d 100644 --- a/src/Driver/FlowJsUploadDriver.php +++ b/src/Driver/FlowJsUploadDriver.php @@ -2,14 +2,11 @@ namespace CodingSocks\ChunkUploader\Driver; +use CodingSocks\ChunkUploader\Identifier\Identifier; + class FlowJsUploadDriver extends ResumableJsUploadDriver { - /** - * ResumableJsUploadDriver constructor. - * - * @param array $config - */ - public function __construct($config) + public function __construct($config, Identifier $identifier) { $config['parameter-namespace'] = ''; $config['parameter-names'] = [ @@ -30,6 +27,6 @@ public function __construct($config) // The name of the current chunk size POST parameter to use for the file chunk. 'current-chunk-size' => 'flowCurrentChunkSize', ]; - parent::__construct($config); + parent::__construct($config, $identifier); } } diff --git a/src/Driver/ResumableJsUploadDriver.php b/src/Driver/ResumableJsUploadDriver.php index dbeff50..0482eae 100644 --- a/src/Driver/ResumableJsUploadDriver.php +++ b/src/Driver/ResumableJsUploadDriver.php @@ -4,6 +4,7 @@ use Closure; use CodingSocks\ChunkUploader\Helper\ChunkHelpers; +use CodingSocks\ChunkUploader\Identifier\Identifier; use CodingSocks\ChunkUploader\Range\ResumableJsRange; use CodingSocks\ChunkUploader\Response\PercentageJsonResponse; use CodingSocks\ChunkUploader\StorageConfig; @@ -24,6 +25,11 @@ class ResumableJsUploadDriver extends UploadDriver */ private $fileParam; + /** + * @var \CodingSocks\ChunkUploader\Identifier\Identifier + */ + private $identifier; + /** * @var string */ @@ -48,10 +54,12 @@ class ResumableJsUploadDriver extends UploadDriver * ResumableJsUploadDriver constructor. * * @param array $config + * @param \CodingSocks\ChunkUploader\Identifier\Identifier $identifier */ - public function __construct($config) + public function __construct($config, Identifier $identifier) { $this->fileParam = $config['param']; + $this->identifier = $identifier; $this->uploadMethod = $config['upload-method']; $this->testMethod = $config['test-method']; @@ -101,10 +109,10 @@ public function resume(Request $request, StorageConfig $config): Response throw new BadRequestHttpException($e->getMessage(), $e); } - $filename = $request->query($this->buildParameterName('identifier')); + $uid = $this->identifier->generateIdentifier($request->query($this->buildParameterName('identifier'))); $chunkname = $this->buildChunkname($range); - if (! $this->chunkExists($config, $filename, $chunkname)) { + if (! $this->chunkExists($config, $uid, $chunkname)) { return new Response('', Response::HTTP_NO_CONTENT); } @@ -165,9 +173,10 @@ private function saveChunk(UploadedFile $file, Request $request, StorageConfig $ throw new BadRequestHttpException($e->getMessage(), $e); } - $uuid = $request->post($this->buildParameterName('identifier')); + $weakId = $request->post($this->buildParameterName('identifier')); + $uid = $this->identifier->generateIdentifier($weakId); - $chunks = $this->storeChunk($config, $range, $file, $uuid); + $chunks = $this->storeChunk($config, $range, $file, $uid); if (!$range->isFinished($chunks)) { return new PercentageJsonResponse($range->getPercentage($chunks)); @@ -178,7 +187,7 @@ private function saveChunk(UploadedFile $file, Request $request, StorageConfig $ $path = $this->mergeChunks($config, $chunks, $targetFilename); if ($config->sweep()) { - $this->deleteChunkDirectory($config, $uuid); + $this->deleteChunkDirectory($config, $uid); } $this->triggerFileUploadedEvent($config->getDisk(), $path, $fileUploaded); diff --git a/src/Driver/SimpleUploaderJsUploadDriver.php b/src/Driver/SimpleUploaderJsUploadDriver.php index 70183f2..3e9a61c 100644 --- a/src/Driver/SimpleUploaderJsUploadDriver.php +++ b/src/Driver/SimpleUploaderJsUploadDriver.php @@ -2,9 +2,11 @@ namespace CodingSocks\ChunkUploader\Driver; +use CodingSocks\ChunkUploader\Identifier\Identifier; + class SimpleUploaderJsUploadDriver extends ResumableJsUploadDriver { - public function __construct($config) + public function __construct($config, Identifier $identifier) { $config['parameter-namespace'] = ''; $config['parameter-names'] = [ @@ -25,6 +27,6 @@ public function __construct($config) // The name of the current chunk size POST parameter to use for the file chunk. 'current-chunk-size' => 'currentChunkSize', ]; - parent::__construct($config); + parent::__construct($config, $identifier); } } diff --git a/src/UploadManager.php b/src/UploadManager.php index 0d60f00..6d426be 100644 --- a/src/UploadManager.php +++ b/src/UploadManager.php @@ -34,33 +34,34 @@ public function createDropzoneDriver() public function createFlowJsDriver() { - return new FlowJsUploadDriver($this->app['config']['chunk-uploader.resumable-js']); + return new FlowJsUploadDriver($this->app['config']['chunk-uploader.resumable-js'], $this->identityManager()->driver()); } public function createNgFileUploadDriver() { - /** @var \Illuminate\Support\Manager $identityManager */ - $identityManager = $this->app['chunk-uploader.identity-manager']; - - return new NgFileUploadDriver($identityManager->driver()); + return new NgFileUploadDriver($this->identityManager()->driver()); } public function createPluploadDriver() { - /** @var \Illuminate\Support\Manager $identityManager */ - $identityManager = $this->app['chunk-uploader.identity-manager']; - - return new PluploadUploadDriver($identityManager->driver()); + return new PluploadUploadDriver($this->identityManager()->driver()); } public function createResumableJsDriver() { - return new ResumableJsUploadDriver($this->app['config']['chunk-uploader.resumable-js']); + return new ResumableJsUploadDriver($this->app['config']['chunk-uploader.resumable-js'], $this->identityManager()->driver()); } public function createSimpleUploaderJsDriver() { - return new SimpleUploaderJsUploadDriver($this->app['config']['chunk-uploader.simple-uploader-js']); + return new SimpleUploaderJsUploadDriver($this->app['config']['chunk-uploader.simple-uploader-js'], $this->identityManager()->driver()); + } + + /** + * @return \Illuminate\Support\Manager + */ + protected function identityManager() { + return $this->app['chunk-uploader.identity-manager']; } /** diff --git a/tests/Driver/FlowJsUploadDriverTest.php b/tests/Driver/FlowJsUploadDriverTest.php index 67f653a..86f3d52 100644 --- a/tests/Driver/FlowJsUploadDriverTest.php +++ b/tests/Driver/FlowJsUploadDriverTest.php @@ -28,6 +28,7 @@ protected function setUp(): void { parent::setUp(); + $this->app->make('config')->set('chunk-uploader.identifier', 'nop'); $this->app->make('config')->set('chunk-uploader.uploader', 'flow-js'); $this->app->make('config')->set('chunk-uploader.sweep', false); $this->handler = $this->app->make(UploadHandler::class); diff --git a/tests/Driver/ResumableJsUploadDriverTest.php b/tests/Driver/ResumableJsUploadDriverTest.php index 04fba54..a573a2d 100644 --- a/tests/Driver/ResumableJsUploadDriverTest.php +++ b/tests/Driver/ResumableJsUploadDriverTest.php @@ -28,6 +28,7 @@ protected function setUp(): void { parent::setUp(); + $this->app->make('config')->set('chunk-uploader.identifier', 'nop'); $this->app->make('config')->set('chunk-uploader.uploader', 'resumable-js'); $this->app->make('config')->set('chunk-uploader.sweep', false); $this->handler = $this->app->make(UploadHandler::class); diff --git a/tests/Driver/SimpleUploaderUploadDriverTest.php b/tests/Driver/SimpleUploaderUploadDriverTest.php index a3e82d0..907f434 100644 --- a/tests/Driver/SimpleUploaderUploadDriverTest.php +++ b/tests/Driver/SimpleUploaderUploadDriverTest.php @@ -28,6 +28,7 @@ protected function setUp(): void { parent::setUp(); + $this->app->make('config')->set('chunk-uploader.identifier', 'nop'); $this->app->make('config')->set('chunk-uploader.uploader', 'simple-uploader-js'); $this->app->make('config')->set('chunk-uploader.sweep', false); $this->handler = $this->app->make(UploadHandler::class); From c0770e3ef13138f0d30ffba5ca0901d4ffc218e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Sat, 18 Jul 2020 13:01:34 +0200 Subject: [PATCH 2/2] Fix style --- src/UploadManager.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/UploadManager.php b/src/UploadManager.php index 6d426be..266a79a 100644 --- a/src/UploadManager.php +++ b/src/UploadManager.php @@ -60,7 +60,8 @@ public function createSimpleUploaderJsDriver() /** * @return \Illuminate\Support\Manager */ - protected function identityManager() { + protected function identityManager() + { return $this->app['chunk-uploader.identity-manager']; }