diff --git a/src/Driver/BlueimpUploadDriver.php b/src/Driver/BlueimpUploadDriver.php index 4265b7e..194bc34 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($totalSize, $originalFilename); - 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, ], ]); @@ -148,7 +154,7 @@ public function save(Request $request, StorageConfig $config, Closure $fileUploa throw new BadRequestHttpException($e->getMessage(), $e); } - $uuid = $this->identifier->generateUploadedFileIdentifierName($file); + $uuid = $this->identifier->generateFileIdentifier($range->getTotal(), $file->getClientOriginalName()); $chunks = $this->storeChunk($config, $range, $file, $uuid); @@ -178,7 +184,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 94a1205..681058d 100644 --- a/src/Identifier/Identifier.php +++ b/src/Identifier/Identifier.php @@ -2,8 +2,6 @@ namespace CodingSocks\ChunkUploader\Identifier; -use Illuminate\Http\UploadedFile; - abstract class Identifier { /** @@ -14,14 +12,15 @@ abstract class Identifier abstract public function generateIdentifier(string $data): string; /** - * @param \Illuminate\Http\UploadedFile $file + * @param float $totalSize + * @param string $originalFilename * * @return string */ - public function generateUploadedFileIdentifierName(UploadedFile $file): string + public function generateFileIdentifier(float $totalSize, string $originalFilename): string { - $data = $file->getClientOriginalName(); + $data = $totalSize . '_' . $originalFilename; - return $this->generateIdentifier($data) . '.' . $file->guessExtension(); + return $this->generateIdentifier($data); } } diff --git a/tests/Driver/BlueimpUploadDriverTest.php b/tests/Driver/BlueimpUploadDriverTest.php index 0b1ccb1..a420037 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)); @@ -149,7 +150,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 +191,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 a4c6826..c3b1bfe 100644 --- a/tests/Identifier/SessionIdentifierTest.php +++ b/tests/Identifier/SessionIdentifierTest.php @@ -2,7 +2,6 @@ namespace CodingSocks\ChunkUploader\Tests\Identifier; -use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Session; use CodingSocks\ChunkUploader\Identifier\SessionIdentifier; use Orchestra\Testbench\TestCase; @@ -26,27 +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() { - $file = UploadedFile::fake()->create('test.txt', 100); - $identifier = $this->identifier->generateUploadedFileIdentifierName($file); - $this->assertEquals('2494cefe4d234bd331aeb4514fe97d810efba29b.txt', $identifier); - } - - public function testUploadedFileIdentifierNameWithoutExtension() - { - $file = UploadedFile::fake()->create('test', 100); - $identifier = $this->identifier->generateUploadedFileIdentifierName($file); - $this->assertEquals('3b7b99bf70a98a544319cf3bad9e912e1b89984d.bin', $identifier); + $identifier = $this->identifier->generateFileIdentifier(200, 'any_filename.ext'); + $this->assertEquals('ec1669bf4dee72e6dd30b94d2d29413601f1b69b', $identifier); } }