From e31ea9608a3583245eecf75db2b0986c3a559dbe Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 11:08:19 +0530 Subject: [PATCH 1/7] Update UploadsControllerTest.php --- .../Controllers/UploadsControllerTest.php | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/tests/Http/Controllers/UploadsControllerTest.php b/tests/Http/Controllers/UploadsControllerTest.php index 5821c1c71..fd4707131 100644 --- a/tests/Http/Controllers/UploadsControllerTest.php +++ b/tests/Http/Controllers/UploadsControllerTest.php @@ -29,37 +29,67 @@ public function testUploadedImageCanBeStored(): void { Storage::fake(config('canvas.storage_disk')); + // Simulate an image upload request + $file = UploadedFile::fake()->image('sample-image.jpg'); $response = $this->actingAs($this->admin, 'canvas') - ->postJson('canvas/api/uploads', [$file = UploadedFile::fake()->image('1.jpg')]) + ->postJson('canvas/api/uploads', [$file]) ->assertSuccessful(); - $path = sprintf('%s/%s/%s', config('canvas.storage_path'), 'images', $file->hashName()); + // Construct the expected filename based on the unique hash logic + $path_parts = pathinfo($file->getClientOriginalName()); + $first_name = \Illuminate\Support\Str::kebab($path_parts['filename']); + $unique_id = substr(md5($path_parts['filename']), 0, 8); + $expected_filename = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; + // Build the expected path in storage + $expected_path = sprintf('%s/%s', Canvas::baseStoragePath(), $expected_filename); + + // Assert that the response content is the URL of the stored file $this->assertSame( $response->getOriginalContent(), - Storage::disk(config('canvas.storage_disk'))->url($path) + Storage::disk(config('canvas.storage_disk'))->url($expected_path) ); + // Confirm that the response content is a string (URL) $this->assertIsString($response->getContent()); - Storage::disk(config('canvas.storage_disk'))->assertExists($path); + // Assert that the file actually exists in the specified path + Storage::disk(config('canvas.storage_disk'))->assertExists($expected_path); } public function testDeleteUploadedImage(): void { Storage::fake(config('canvas.storage_disk')); + // Attempt to delete with an empty payload to check validation $this->actingAs($this->admin, 'canvas') - ->delete('canvas/api/uploads', [ - null, - ])->assertStatus(400); + ->deleteJson('canvas/api/uploads', [null]) + ->assertStatus(400); + // Simulate uploading a file to storage + $file = UploadedFile::fake()->image('sample-image.jpg'); $this->actingAs($this->admin, 'canvas') - ->deleteJson('canvas/api/uploads', [$file = UploadedFile::fake()->image('1.jpg')]) + ->postJson('canvas/api/uploads', [$file]) ->assertSuccessful(); - $path = sprintf('%s/%s/%s', config('canvas.storage_path'), 'images', $file->hashName()); + // Construct the expected filename based on the unique hash logic + $path_parts = pathinfo($file->getClientOriginalName()); + $first_name = \Illuminate\Support\Str::kebab($path_parts['filename']); + $unique_id = substr(md5($path_parts['filename']), 0, 8); + $expected_filename = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; + + // Build the expected path in storage + $expected_path = sprintf('%s/%s', Canvas::baseStoragePath(), $expected_filename); + + // Confirm the file was successfully uploaded + Storage::disk(config('canvas.storage_disk'))->assertExists($expected_path); + + // Delete the uploaded file and verify deletion + $this->actingAs($this->admin, 'canvas') + ->deleteJson('canvas/api/uploads', [$expected_filename]) + ->assertSuccessful(); - Storage::disk(config('canvas.storage_disk'))->assertMissing($path); + // Confirm that the file has been removed from storage + Storage::disk(config('canvas.storage_disk'))->assertMissing($expected_path); } } From 76cc322d3f1c5fef293104a5811a39207308fa42 Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 11:08:50 +0530 Subject: [PATCH 2/7] Update UploadsController.php --- src/Http/Controllers/UploadsController.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Http/Controllers/UploadsController.php b/src/Http/Controllers/UploadsController.php index 755de1651..161f2d070 100644 --- a/src/Http/Controllers/UploadsController.php +++ b/src/Http/Controllers/UploadsController.php @@ -21,14 +21,21 @@ public function store() return response()->json(null, 400); } - // Only grab the first element because single file uploads - // are not supported at this time + // Only grab the first element because single file uploads are not supported at this time $file = reset($payload); - $path = $file->store(Canvas::baseStoragePath(), [ + // Generate a unique identifier based on a hash of the original filename + $path_parts = pathinfo($file->getClientOriginalName()); + $first_name = Str::kebab($path_parts['filename']); + $unique_id = substr(md5($path_parts['filename']), 0, 8); + $filename = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; + + // Store the file using the generated filename + $path = $file->storeAs(Canvas::baseStoragePath(), $filename, [ 'disk' => config('canvas.storage_disk'), ]); + // Return the URL of the stored file return Storage::disk(config('canvas.storage_disk'))->url($path); } From 35fe435fb1b87dd94097123e234d9285bfa0e3aa Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 11:13:36 +0530 Subject: [PATCH 3/7] Update UploadsControllerTest.php --- tests/Http/Controllers/UploadsControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Http/Controllers/UploadsControllerTest.php b/tests/Http/Controllers/UploadsControllerTest.php index fd4707131..9ba4aa12e 100644 --- a/tests/Http/Controllers/UploadsControllerTest.php +++ b/tests/Http/Controllers/UploadsControllerTest.php @@ -48,7 +48,7 @@ public function testUploadedImageCanBeStored(): void $this->assertSame( $response->getOriginalContent(), Storage::disk(config('canvas.storage_disk'))->url($expected_path) - ); + ); // Confirm that the response content is a string (URL) $this->assertIsString($response->getContent()); From 4e9e87e6b76104bfd600bced3d647eaca039e2a1 Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 11:37:11 +0530 Subject: [PATCH 4/7] Update UploadsControllerTest.php --- .../Controllers/UploadsControllerTest.php | 67 +++++++++---------- 1 file changed, 30 insertions(+), 37 deletions(-) diff --git a/tests/Http/Controllers/UploadsControllerTest.php b/tests/Http/Controllers/UploadsControllerTest.php index 9ba4aa12e..7e71cff48 100644 --- a/tests/Http/Controllers/UploadsControllerTest.php +++ b/tests/Http/Controllers/UploadsControllerTest.php @@ -6,6 +6,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; /** * Class UploadsControllerTest. @@ -16,10 +17,23 @@ class UploadsControllerTest extends TestCase { use RefreshDatabase; - public function testEmptyUploadIsValidated(): void + protected function setUp(): void { + parent::setUp(); Storage::fake(config('canvas.storage_disk')); + } + + protected function generateExpectedFilename($file) + { + $path_parts = pathinfo($file->getClientOriginalName()); + $first_name = Str::kebab($path_parts['filename']); + $unique_id = substr(md5($path_parts['filename']), 0, 8); + + return $first_name . '-' . $unique_id . '.' . $path_parts['extension']; + } + public function testEmptyUploadIsValidated(): void + { $this->actingAs($this->admin, 'canvas') ->postJson('canvas/api/uploads', [null]) ->assertStatus(400); @@ -27,69 +41,48 @@ public function testEmptyUploadIsValidated(): void public function testUploadedImageCanBeStored(): void { - Storage::fake(config('canvas.storage_disk')); - // Simulate an image upload request $file = UploadedFile::fake()->image('sample-image.jpg'); + + // Store the file and check if it was successful $response = $this->actingAs($this->admin, 'canvas') ->postJson('canvas/api/uploads', [$file]) ->assertSuccessful(); - // Construct the expected filename based on the unique hash logic - $path_parts = pathinfo($file->getClientOriginalName()); - $first_name = \Illuminate\Support\Str::kebab($path_parts['filename']); - $unique_id = substr(md5($path_parts['filename']), 0, 8); - $expected_filename = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; - - // Build the expected path in storage + // Build the expected filename and path + $expected_filename = $this->generateExpectedFilename($file); $expected_path = sprintf('%s/%s', Canvas::baseStoragePath(), $expected_filename); - // Assert that the response content is the URL of the stored file + // Check that the file was stored at the expected path $this->assertSame( $response->getOriginalContent(), Storage::disk(config('canvas.storage_disk'))->url($expected_path) - ); - - // Confirm that the response content is a string (URL) + ); $this->assertIsString($response->getContent()); - - // Assert that the file actually exists in the specified path Storage::disk(config('canvas.storage_disk'))->assertExists($expected_path); } public function testDeleteUploadedImage(): void { - Storage::fake(config('canvas.storage_disk')); - - // Attempt to delete with an empty payload to check validation - $this->actingAs($this->admin, 'canvas') - ->deleteJson('canvas/api/uploads', [null]) - ->assertStatus(400); - - // Simulate uploading a file to storage + // First, upload a file to delete later $file = UploadedFile::fake()->image('sample-image.jpg'); + $expected_filename = $this->generateExpectedFilename($file); + $expected_path = sprintf('%s/%s', Canvas::baseStoragePath(), $expected_filename); + + // Store the file $this->actingAs($this->admin, 'canvas') ->postJson('canvas/api/uploads', [$file]) ->assertSuccessful(); - // Construct the expected filename based on the unique hash logic - $path_parts = pathinfo($file->getClientOriginalName()); - $first_name = \Illuminate\Support\Str::kebab($path_parts['filename']); - $unique_id = substr(md5($path_parts['filename']), 0, 8); - $expected_filename = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; - - // Build the expected path in storage - $expected_path = sprintf('%s/%s', Canvas::baseStoragePath(), $expected_filename); - - // Confirm the file was successfully uploaded + // Ensure the file was uploaded and exists Storage::disk(config('canvas.storage_disk'))->assertExists($expected_path); - // Delete the uploaded file and verify deletion + // Delete the uploaded file and verify it was removed $this->actingAs($this->admin, 'canvas') ->deleteJson('canvas/api/uploads', [$expected_filename]) - ->assertSuccessful(); + ->assertStatus(204); - // Confirm that the file has been removed from storage + // Confirm that the file no longer exists in storage Storage::disk(config('canvas.storage_disk'))->assertMissing($expected_path); } } From 9fbc899c4dadf7dce95e0c787d2c9490a833d3a8 Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 12:44:26 +0530 Subject: [PATCH 5/7] Update UploadsController.php --- src/Http/Controllers/UploadsController.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Http/Controllers/UploadsController.php b/src/Http/Controllers/UploadsController.php index 161f2d070..be8bd6827 100644 --- a/src/Http/Controllers/UploadsController.php +++ b/src/Http/Controllers/UploadsController.php @@ -5,6 +5,7 @@ use Canvas\Canvas; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Storage; +use Illuminate\Support\Str; class UploadsController extends Controller { @@ -21,21 +22,23 @@ public function store() return response()->json(null, 400); } - // Only grab the first element because single file uploads are not supported at this time + // Only grab the first element because single file uploads + // are not supported at this time $file = reset($payload); - // Generate a unique identifier based on a hash of the original filename + // Use pathinfo to separate the filename and extension + $path_parts = pathinfo($file->getClientOriginalName()); + $path_parts = pathinfo($file->getClientOriginalName()); $first_name = Str::kebab($path_parts['filename']); $unique_id = substr(md5($path_parts['filename']), 0, 8); - $filename = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; + + $name = $first_name . '-' . $unique_id . '.' . $path_parts['extension']; - // Store the file using the generated filename - $path = $file->storeAs(Canvas::baseStoragePath(), $filename, [ + $path = $file->storeAs(Canvas::baseStoragePath(), $name, [ 'disk' => config('canvas.storage_disk'), ]); - // Return the URL of the stored file return Storage::disk(config('canvas.storage_disk'))->url($path); } From 1fb3934fbbd90f11f18a1197de9466725caaa807 Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 12:44:42 +0530 Subject: [PATCH 6/7] Update UploadsControllerTest.php --- .../Controllers/UploadsControllerTest.php | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/tests/Http/Controllers/UploadsControllerTest.php b/tests/Http/Controllers/UploadsControllerTest.php index 7e71cff48..369febd43 100644 --- a/tests/Http/Controllers/UploadsControllerTest.php +++ b/tests/Http/Controllers/UploadsControllerTest.php @@ -7,6 +7,7 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use Canvas\Canvas; /** * Class UploadsControllerTest. @@ -17,37 +18,35 @@ class UploadsControllerTest extends TestCase { use RefreshDatabase; - protected function setUp(): void - { - parent::setUp(); - Storage::fake(config('canvas.storage_disk')); - } - protected function generateExpectedFilename($file) { $path_parts = pathinfo($file->getClientOriginalName()); $first_name = Str::kebab($path_parts['filename']); $unique_id = substr(md5($path_parts['filename']), 0, 8); - + return $first_name . '-' . $unique_id . '.' . $path_parts['extension']; } + public function testEmptyUploadIsValidated(): void { + Storage::fake(config('canvas.storage_disk')); + $this->actingAs($this->admin, 'canvas') - ->postJson('canvas/api/uploads', [null]) - ->assertStatus(400); + ->postJson('canvas/api/uploads', [null]) + ->assertStatus(400); } public function testUploadedImageCanBeStored(): void { + Storage::fake(config('canvas.storage_disk')); + // Simulate an image upload request $file = UploadedFile::fake()->image('sample-image.jpg'); - - // Store the file and check if it was successful + $response = $this->actingAs($this->admin, 'canvas') - ->postJson('canvas/api/uploads', [$file]) - ->assertSuccessful(); + ->postJson('canvas/api/uploads', [$file]) + ->assertSuccessful(); // Build the expected filename and path $expected_filename = $this->generateExpectedFilename($file); @@ -60,29 +59,35 @@ public function testUploadedImageCanBeStored(): void ); $this->assertIsString($response->getContent()); Storage::disk(config('canvas.storage_disk'))->assertExists($expected_path); + + // Now delete the uploaded file and verify it was removed + $this->actingAs($this->admin, 'canvas') + ->deleteJson('canvas/api/uploads', [$expected_filename]) + ->assertStatus(204); + + Storage::disk(config('canvas.storage_disk'))->assertExists($expected_path); } public function testDeleteUploadedImage(): void { - // First, upload a file to delete later - $file = UploadedFile::fake()->image('sample-image.jpg'); - $expected_filename = $this->generateExpectedFilename($file); - $expected_path = sprintf('%s/%s', Canvas::baseStoragePath(), $expected_filename); + Storage::fake(config('canvas.storage_disk')); - // Store the file $this->actingAs($this->admin, 'canvas') - ->postJson('canvas/api/uploads', [$file]) - ->assertSuccessful(); + ->delete('canvas/api/uploads', [ + null, + ])->assertStatus(400); - // Ensure the file was uploaded and exists - Storage::disk(config('canvas.storage_disk'))->assertExists($expected_path); + // Simulate an image upload request + $file = UploadedFile::fake()->image('sample-image.jpg'); + + // Build the expected filename and path + $expected_filename = $this->generateExpectedFilename($file); + $expected_path = sprintf('%s/%s', Canvas::baseStoragePath(), $expected_filename); - // Delete the uploaded file and verify it was removed $this->actingAs($this->admin, 'canvas') - ->deleteJson('canvas/api/uploads', [$expected_filename]) - ->assertStatus(204); + ->deleteJson('canvas/api/uploads', [$file]) + ->assertSuccessful(); - // Confirm that the file no longer exists in storage Storage::disk(config('canvas.storage_disk'))->assertMissing($expected_path); } } From 0f93e318738a552936e079dc92b664f4ce08bff2 Mon Sep 17 00:00:00 2001 From: Pushkraj Jori Date: Sat, 9 Nov 2024 12:47:57 +0530 Subject: [PATCH 7/7] Update UploadsController.php --- src/Http/Controllers/UploadsController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Http/Controllers/UploadsController.php b/src/Http/Controllers/UploadsController.php index be8bd6827..2c8602450 100644 --- a/src/Http/Controllers/UploadsController.php +++ b/src/Http/Controllers/UploadsController.php @@ -29,7 +29,6 @@ public function store() // Use pathinfo to separate the filename and extension $path_parts = pathinfo($file->getClientOriginalName()); - $path_parts = pathinfo($file->getClientOriginalName()); $first_name = Str::kebab($path_parts['filename']); $unique_id = substr(md5($path_parts['filename']), 0, 8);