From 2c0a4952033ef7ad790b5c727c80bcbbfda27f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Fri, 12 Jun 2020 19:46:37 +0200 Subject: [PATCH 1/6] Fix file identifier --- src/Driver/BlueimpUploadDriver.php | 2 +- src/Identifier/Identifier.php | 9 +++++---- tests/Driver/BlueimpUploadDriverTest.php | 4 ++-- tests/Identifier/SessionIdentifierTest.php | 8 ++++---- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Driver/BlueimpUploadDriver.php b/src/Driver/BlueimpUploadDriver.php index 17be67a..5776e4a 100644 --- a/src/Driver/BlueimpUploadDriver.php +++ b/src/Driver/BlueimpUploadDriver.php @@ -148,7 +148,7 @@ public function save(Request $request, StorageConfig $config, Closure $fileUploa throw new BadRequestHttpException($e->getMessage(), $e); } - $uuid = $this->identifier->generateUploadedFileIdentifierName($file); + $uuid = $this->identifier->generateFileIdentifierName($file, $range->getTotal()); $chunks = $this->storeChunk($config, $range, $file, $uuid); diff --git a/src/Identifier/Identifier.php b/src/Identifier/Identifier.php index 7cf063f..3cc26f2 100644 --- a/src/Identifier/Identifier.php +++ b/src/Identifier/Identifier.php @@ -14,14 +14,15 @@ abstract class Identifier abstract public function generateIdentifier(string $data): string; /** - * @param \Illuminate\Http\UploadedFile $file + * @param \Illuminate\Http\UploadedFile $chunk + * @param $totalSize * * @return string */ - public function generateUploadedFileIdentifierName(UploadedFile $file): string + public function generateFileIdentifierName(UploadedFile $chunk, $totalSize): string { - $data = $file->getClientOriginalName(); + $data = $totalSize . '_' . $chunk->getClientOriginalName(); - return $this->generateIdentifier($data) . '.' . $file->guessExtension(); + return $this->generateIdentifier($data); } } diff --git a/tests/Driver/BlueimpUploadDriverTest.php b/tests/Driver/BlueimpUploadDriverTest.php index 8cba11e..0fbeaa2 100644 --- a/tests/Driver/BlueimpUploadDriverTest.php +++ b/tests/Driver/BlueimpUploadDriverTest.php @@ -149,7 +149,7 @@ public function testUploadFirstChunk() $response->assertSuccessful(); $response->assertJson(['done' => 50]); - Storage::disk('local')->assertExists('chunks/2494cefe4d234bd331aeb4514fe97d810efba29b.txt/000-099'); + Storage::disk('local')->assertExists('chunks/5d5115c1064c6e9dead0b7b71506bdfe273fd11c/000-099'); Event::assertNotDispatched(FileUploaded::class, function ($event) use ($file) { return $event->file = $file->hashName('merged'); @@ -190,7 +190,7 @@ public function testUploadLastChunk() $response->assertSuccessful(); $response->assertJson(['done' => 100]); - Storage::disk('local')->assertExists('chunks/2494cefe4d234bd331aeb4514fe97d810efba29b.txt/100-199'); + Storage::disk('local')->assertExists('chunks/5d5115c1064c6e9dead0b7b71506bdfe273fd11c/100-199'); Storage::disk('local')->assertExists($file->hashName('merged')); Event::assertDispatched(FileUploaded::class, function ($event) use ($file) { diff --git a/tests/Identifier/SessionIdentifierTest.php b/tests/Identifier/SessionIdentifierTest.php index c6f3b83..6930464 100644 --- a/tests/Identifier/SessionIdentifierTest.php +++ b/tests/Identifier/SessionIdentifierTest.php @@ -39,14 +39,14 @@ public function testGenerateIdentifierWithoutExtension() public function testUploadedFileIdentifierName() { $file = UploadedFile::fake()->create('test.txt', 100); - $identifier = $this->identifier->generateUploadedFileIdentifierName($file); - $this->assertEquals('2494cefe4d234bd331aeb4514fe97d810efba29b.txt', $identifier); + $identifier = $this->identifier->generateFileIdentifierName($file, 200); + $this->assertEquals('5d5115c1064c6e9dead0b7b71506bdfe273fd11c', $identifier); } public function testUploadedFileIdentifierNameWithoutExtension() { $file = UploadedFile::fake()->create('test', 100); - $identifier = $this->identifier->generateUploadedFileIdentifierName($file); - $this->assertEquals('3b7b99bf70a98a544319cf3bad9e912e1b89984d.bin', $identifier); + $identifier = $this->identifier->generateFileIdentifierName($file, 200); + $this->assertEquals('19daf2dc95ccbc0c856a1ce7a13c949f9e81fd2e', $identifier); } } From 0a2d5477ef96e261da7ddc1d7c1855b944951281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Fri, 12 Jun 2020 20:33:47 +0200 Subject: [PATCH 2/6] Change file identifier generation parameters --- src/Driver/BlueimpUploadDriver.php | 3 +-- src/Identifier/Identifier.php | 10 ++++------ tests/Identifier/SessionIdentifierTest.php | 6 ++---- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/Driver/BlueimpUploadDriver.php b/src/Driver/BlueimpUploadDriver.php index 5776e4a..86debc7 100644 --- a/src/Driver/BlueimpUploadDriver.php +++ b/src/Driver/BlueimpUploadDriver.php @@ -148,7 +148,7 @@ public function save(Request $request, StorageConfig $config, Closure $fileUploa throw new BadRequestHttpException($e->getMessage(), $e); } - $uuid = $this->identifier->generateFileIdentifierName($file, $range->getTotal()); + $uuid = $this->identifier->generateFileIdentifier($file->getClientOriginalName(), $range->getTotal()); $chunks = $this->storeChunk($config, $range, $file, $uuid); @@ -178,7 +178,6 @@ public function save(Request $request, StorageConfig $config, Closure $fileUploa public function delete(Request $request, StorageConfig $config): Response { $filename = $request->post($this->fileParam); - $path = $config->getMergedDirectory() . '/' . $filename; Storage::disk($config->getDisk())->delete($path); diff --git a/src/Identifier/Identifier.php b/src/Identifier/Identifier.php index 3cc26f2..45b4060 100644 --- a/src/Identifier/Identifier.php +++ b/src/Identifier/Identifier.php @@ -2,8 +2,6 @@ namespace LaraCrafts\ChunkUploader\Identifier; -use Illuminate\Http\UploadedFile; - abstract class Identifier { /** @@ -14,14 +12,14 @@ abstract class Identifier abstract public function generateIdentifier(string $data): string; /** - * @param \Illuminate\Http\UploadedFile $chunk - * @param $totalSize + * @param string $originalFilename + * @param float $totalSize * * @return string */ - public function generateFileIdentifierName(UploadedFile $chunk, $totalSize): string + public function generateFileIdentifier(string $originalFilename, float $totalSize): string { - $data = $totalSize . '_' . $chunk->getClientOriginalName(); + $data = $totalSize . '_' . $originalFilename; return $this->generateIdentifier($data); } diff --git a/tests/Identifier/SessionIdentifierTest.php b/tests/Identifier/SessionIdentifierTest.php index 6930464..538b90d 100644 --- a/tests/Identifier/SessionIdentifierTest.php +++ b/tests/Identifier/SessionIdentifierTest.php @@ -38,15 +38,13 @@ public function testGenerateIdentifierWithoutExtension() public function testUploadedFileIdentifierName() { - $file = UploadedFile::fake()->create('test.txt', 100); - $identifier = $this->identifier->generateFileIdentifierName($file, 200); + $identifier = $this->identifier->generateFileIdentifier('test.txt', 200); $this->assertEquals('5d5115c1064c6e9dead0b7b71506bdfe273fd11c', $identifier); } public function testUploadedFileIdentifierNameWithoutExtension() { - $file = UploadedFile::fake()->create('test', 100); - $identifier = $this->identifier->generateFileIdentifierName($file, 200); + $identifier = $this->identifier->generateFileIdentifier('test', 200); $this->assertEquals('19daf2dc95ccbc0c856a1ce7a13c949f9e81fd2e', $identifier); } } From 05044bbbdd320e52087b0d7dc6d1acc12364d61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Fri, 12 Jun 2020 20:35:31 +0200 Subject: [PATCH 3/6] Fix bluimp driver file ID generation --- src/Driver/BlueimpUploadDriver.php | 20 +++++++++++++------- tests/Driver/BlueimpUploadDriverTest.php | 3 ++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Driver/BlueimpUploadDriver.php b/src/Driver/BlueimpUploadDriver.php index 86debc7..582ffe0 100644 --- a/src/Driver/BlueimpUploadDriver.php +++ b/src/Driver/BlueimpUploadDriver.php @@ -100,26 +100,32 @@ public function download(Request $request, StorageConfig $config): Response { $download = $request->query('download', false); if ($download !== false) { - $filename = $request->query($this->fileParam); + $uuid = $request->query($this->fileParam); - return $this->fileResponse($filename, $config); + return $this->fileResponse($uuid, $config); } - $request->validate([$this->fileParam => 'required']); - $filename = $request->query($this->fileParam); + $request->validate([ + $this->fileParam => 'required', + 'totalSize' => 'required', + ]); + + $originalFilename = $request->query($this->fileParam); + $totalSize = $request->query('totalSize'); + $uuid = $this->identifier->generateFileIdentifier($originalFilename, $totalSize); - if (!$this->chunkExists($config, $filename)) { + if (!$this->chunkExists($config, $uuid)) { return new JsonResponse([ 'file' => null, ]); } - $chunk = Arr::last($this->chunks($config, $filename)); + $chunk = Arr::last($this->chunks($config, $uuid)); $size = explode('-', basename($chunk))[1] + 1; return new JsonResponse([ 'file' => [ - 'name' => $filename, + 'name' => $originalFilename, 'size' => $size, ], ]); diff --git a/tests/Driver/BlueimpUploadDriverTest.php b/tests/Driver/BlueimpUploadDriverTest.php index 0fbeaa2..948338f 100644 --- a/tests/Driver/BlueimpUploadDriverTest.php +++ b/tests/Driver/BlueimpUploadDriverTest.php @@ -94,10 +94,11 @@ public function testDownload() public function testResume() { - $this->createFakeLocalFile('chunks/2494cefe4d234bd331aeb4514fe97d810efba29b.txt', '000-099'); + $this->createFakeLocalFile('chunks/4f0fce4ab7d03efd246b25d3c9e6546a0d65794d', '000-099'); $request = Request::create('', Request::METHOD_GET, [ 'file' => '2494cefe4d234bd331aeb4514fe97d810efba29b.txt', + 'totalSize' => '200' ]); $response = $this->createTestResponse($this->handler->handle($request)); From bcc4266ac071551caac93679a2c75d2dd4c9de2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Fri, 12 Jun 2020 21:15:03 +0200 Subject: [PATCH 4/6] Style CI fix --- tests/Driver/BlueimpUploadDriverTest.php | 2 +- tests/Identifier/SessionIdentifierTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Driver/BlueimpUploadDriverTest.php b/tests/Driver/BlueimpUploadDriverTest.php index 948338f..f8f61d0 100644 --- a/tests/Driver/BlueimpUploadDriverTest.php +++ b/tests/Driver/BlueimpUploadDriverTest.php @@ -98,7 +98,7 @@ public function testResume() $request = Request::create('', Request::METHOD_GET, [ 'file' => '2494cefe4d234bd331aeb4514fe97d810efba29b.txt', - 'totalSize' => '200' + 'totalSize' => '200', ]); $response = $this->createTestResponse($this->handler->handle($request)); diff --git a/tests/Identifier/SessionIdentifierTest.php b/tests/Identifier/SessionIdentifierTest.php index 538b90d..ba964de 100644 --- a/tests/Identifier/SessionIdentifierTest.php +++ b/tests/Identifier/SessionIdentifierTest.php @@ -2,7 +2,6 @@ namespace LaraCrafts\ChunkUploader\Tests\Identifier; -use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Session; use LaraCrafts\ChunkUploader\Identifier\SessionIdentifier; use Orchestra\Testbench\TestCase; From 0bb292957e9b996a0cd69527578f5b7286d2831d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Sun, 14 Jun 2020 16:47:44 +0200 Subject: [PATCH 5/6] Change parameter order --- src/Driver/BlueimpUploadDriver.php | 4 ++-- src/Identifier/Identifier.php | 4 ++-- tests/Identifier/SessionIdentifierTest.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Driver/BlueimpUploadDriver.php b/src/Driver/BlueimpUploadDriver.php index 582ffe0..e2adf4a 100644 --- a/src/Driver/BlueimpUploadDriver.php +++ b/src/Driver/BlueimpUploadDriver.php @@ -112,7 +112,7 @@ public function download(Request $request, StorageConfig $config): Response $originalFilename = $request->query($this->fileParam); $totalSize = $request->query('totalSize'); - $uuid = $this->identifier->generateFileIdentifier($originalFilename, $totalSize); + $uuid = $this->identifier->generateFileIdentifier($totalSize, $originalFilename); if (!$this->chunkExists($config, $uuid)) { return new JsonResponse([ @@ -154,7 +154,7 @@ public function save(Request $request, StorageConfig $config, Closure $fileUploa throw new BadRequestHttpException($e->getMessage(), $e); } - $uuid = $this->identifier->generateFileIdentifier($file->getClientOriginalName(), $range->getTotal()); + $uuid = $this->identifier->generateFileIdentifier($range->getTotal(), $file->getClientOriginalName()); $chunks = $this->storeChunk($config, $range, $file, $uuid); diff --git a/src/Identifier/Identifier.php b/src/Identifier/Identifier.php index 45b4060..b5b72fc 100644 --- a/src/Identifier/Identifier.php +++ b/src/Identifier/Identifier.php @@ -12,12 +12,12 @@ abstract class Identifier abstract public function generateIdentifier(string $data): string; /** - * @param string $originalFilename * @param float $totalSize + * @param string $originalFilename * * @return string */ - public function generateFileIdentifier(string $originalFilename, float $totalSize): string + public function generateFileIdentifier(float $totalSize, string $originalFilename): string { $data = $totalSize . '_' . $originalFilename; diff --git a/tests/Identifier/SessionIdentifierTest.php b/tests/Identifier/SessionIdentifierTest.php index ba964de..07ac940 100644 --- a/tests/Identifier/SessionIdentifierTest.php +++ b/tests/Identifier/SessionIdentifierTest.php @@ -37,13 +37,13 @@ public function testGenerateIdentifierWithoutExtension() public function testUploadedFileIdentifierName() { - $identifier = $this->identifier->generateFileIdentifier('test.txt', 200); + $identifier = $this->identifier->generateFileIdentifier(200, 'test.txt'); $this->assertEquals('5d5115c1064c6e9dead0b7b71506bdfe273fd11c', $identifier); } public function testUploadedFileIdentifierNameWithoutExtension() { - $identifier = $this->identifier->generateFileIdentifier('test', 200); + $identifier = $this->identifier->generateFileIdentifier(200, 'test'); $this->assertEquals('19daf2dc95ccbc0c856a1ce7a13c949f9e81fd2e', $identifier); } } From 6484c6e0f4dff33d2df1b9d79353203167473f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20G=C3=B6r=C3=B6g?= Date: Sun, 14 Jun 2020 17:01:18 +0200 Subject: [PATCH 6/6] Update tests --- tests/Identifier/SessionIdentifierTest.php | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/tests/Identifier/SessionIdentifierTest.php b/tests/Identifier/SessionIdentifierTest.php index 07ac940..f9ee9f9 100644 --- a/tests/Identifier/SessionIdentifierTest.php +++ b/tests/Identifier/SessionIdentifierTest.php @@ -25,25 +25,13 @@ protected function setUp(): void public function testGenerateIdentifier() { - $identifier = $this->identifier->generateIdentifier('test.txt'); - $this->assertEquals('2494cefe4d234bd331aeb4514fe97d810efba29b', $identifier); - } - - public function testGenerateIdentifierWithoutExtension() - { - $identifier = $this->identifier->generateIdentifier('test'); - $this->assertEquals('3b7b99bf70a98a544319cf3bad9e912e1b89984d', $identifier); + $identifier = $this->identifier->generateIdentifier('any_string'); + $this->assertEquals('b41d07049729f460973494395f9bf8fe23834d48', $identifier); } public function testUploadedFileIdentifierName() { - $identifier = $this->identifier->generateFileIdentifier(200, 'test.txt'); - $this->assertEquals('5d5115c1064c6e9dead0b7b71506bdfe273fd11c', $identifier); - } - - public function testUploadedFileIdentifierNameWithoutExtension() - { - $identifier = $this->identifier->generateFileIdentifier(200, 'test'); - $this->assertEquals('19daf2dc95ccbc0c856a1ce7a13c949f9e81fd2e', $identifier); + $identifier = $this->identifier->generateFileIdentifier(200, 'any_filename.ext'); + $this->assertEquals('ec1669bf4dee72e6dd30b94d2d29413601f1b69b', $identifier); } }