diff --git a/src/Froala.php b/src/Froala.php index 52ac169..fd91983 100644 --- a/src/Froala.php +++ b/src/Froala.php @@ -116,7 +116,7 @@ public function withFiles($disk = null, $path = '/') if (config('nova.froala-field.attachments_driver', self::DRIVER_NAME) !== self::DRIVER_NAME) { $this->images(new AttachedImagesList($this)); - return parent::withFiles($disk); + return parent::withFiles($disk, $path); } $this->attach(new StorePendingAttachment($this)) @@ -188,6 +188,16 @@ public function images(callable $imagesCallback) return $this; } + /** + * Get the path that the field is stored at on disk. + * + * @return string|null + */ + public function getStorageDir() + { + return $this->storagePath ?? '/'; + } + /** * Get the full path that the field is stored at on disk. * diff --git a/src/Handlers/StorePendingAttachment.php b/src/Handlers/StorePendingAttachment.php index 88aa933..29f23a4 100644 --- a/src/Handlers/StorePendingAttachment.php +++ b/src/Handlers/StorePendingAttachment.php @@ -43,10 +43,10 @@ public function __invoke(Request $request) 'draft_id' => $request->draftId, 'attachment' => config('nova.froala-field.preserve_file_names') ? $request->attachment->storeAs( - '/', + $this->field->getStorageDir(), $request->attachment->getClientOriginalName(), $this->field->disk - ) : $request->attachment->store('/', $this->field->disk), + ) : $request->attachment->store($this->field->getStorageDir(), $this->field->disk), 'disk' => $this->field->disk, ])->attachment; @@ -57,9 +57,11 @@ public function __invoke(Request $request) protected function abortIfFileNameExists(Request $request): void { + $path = rtrim($this->field->getStorageDir(), '/').'/'.$request->attachment->getClientOriginalName(); + if (config('nova.froala-field.preserve_file_names') && Storage::disk($this->field->disk) - ->exists($request->attachment->getClientOriginalName()) + ->exists($path) ) { abort(response()->json([ 'status' => Response::HTTP_CONFLICT, diff --git a/tests/Fixtures/TestResource.php b/tests/Fixtures/TestResource.php index 85b19ba..0ab0d4d 100644 --- a/tests/Fixtures/TestResource.php +++ b/tests/Fixtures/TestResource.php @@ -21,7 +21,7 @@ public function fields(Request $request) { return [ Text::make('Title'), - Froala::make('Content')->withFiles(TestCase::DISK), + Froala::make('Content')->withFiles(TestCase::DISK, TestCase::PATH), ]; } } diff --git a/tests/FroalaImageManagerControllerTest.php b/tests/FroalaImageManagerControllerTest.php index 15d1719..5ab8e87 100644 --- a/tests/FroalaImageManagerControllerTest.php +++ b/tests/FroalaImageManagerControllerTest.php @@ -16,7 +16,7 @@ public function get_images() for ($i = 0; $i <= 10; $i++) { $this->uploadPendingFile(); - $url = Storage::disk(TestCase::DISK)->url($this->file->hashName()); + $url = Storage::disk(TestCase::DISK)->url($this->getAttachmentLocation()); $images[] = [ 'url' => $url, @@ -47,6 +47,6 @@ public function destroy_image() 'field' => 'content', ]); - Storage::disk(static::DISK)->assertMissing($this->file->hashName()); + Storage::disk(static::DISK)->assertMissing($this->getAttachmentLocation()); } } diff --git a/tests/FroalaUploadControllerTest.php b/tests/FroalaUploadControllerTest.php index 3334c9e..f901193 100644 --- a/tests/FroalaUploadControllerTest.php +++ b/tests/FroalaUploadControllerTest.php @@ -17,16 +17,16 @@ public function store_pending_attachment() { $response = $this->uploadPendingFile(); - $response->assertJson(['link' => Storage::disk(static::DISK)->url($this->file->hashName())]); + $response->assertJson(['link' => Storage::disk(static::DISK)->url($this->getAttachmentLocation())]); $this->assertDatabaseHas((new PendingAttachment)->getTable(), [ 'draft_id' => $this->draftId, 'disk' => static::DISK, - 'attachment' => $this->file->hashName(), + 'attachment' => $this->getAttachmentLocation(), ]); // Assert the file was stored... - Storage::disk(static::DISK)->assertExists($this->file->hashName()); + Storage::disk(static::DISK)->assertExists($this->getAttachmentLocation()); // Assert a file does not exist... Storage::disk(static::DISK)->assertMissing('missing.jpg'); @@ -55,8 +55,8 @@ public function store_attachment() $this->assertDatabaseHas((new Attachment)->getTable(), [ 'disk' => static::DISK, - 'attachment' => $this->file->hashName(), - 'url' => Storage::disk(static::DISK)->url($this->file->hashName()), + 'attachment' => $this->getAttachmentLocation(), + 'url' => Storage::disk(static::DISK)->url($this->getAttachmentLocation()), 'attachable_id' => $response->json('id'), 'attachable_type' => Article::class, ]); @@ -69,13 +69,13 @@ public function detach_attachment() $this->storeArticle(); - Storage::disk(static::DISK)->assertExists($this->file->hashName()); + Storage::disk(static::DISK)->assertExists($this->getAttachmentLocation()); $this->json('DELETE', 'nova-vendor/froala-field/articles/attachments/content', [ 'src' => $src, ]); - Storage::disk(static::DISK)->assertMissing($this->file->hashName()); + Storage::disk(static::DISK)->assertMissing($this->getAttachmentLocation()); } /** @test */ @@ -86,7 +86,7 @@ public function discard_pending_attachments() for ($i = 0; $i <= 3; $i++) { $this->uploadPendingFile(); - $fileNames[] = $this->file->hashName(); + $fileNames[] = $this->getAttachmentLocation(); $this->regenerateUpload(); } @@ -110,7 +110,7 @@ public function delete_all_related_attachments() for ($i = 0; $i <= 5; $i++) { $this->uploadPendingFile(); - $fileNames[] = $this->file->hashName(); + $fileNames[] = $this->getAttachmentLocation(); $this->regenerateUpload(); } diff --git a/tests/PreserveFilenamesTest.php b/tests/PreserveFilenamesTest.php index 6bb2107..c8d99e5 100644 --- a/tests/PreserveFilenamesTest.php +++ b/tests/PreserveFilenamesTest.php @@ -24,16 +24,16 @@ public function save_image() { $response = $this->uploadPendingFile(); - $response->assertJson(['link' => Storage::disk(static::DISK)->url($this->file->getClientOriginalName())]); + $response->assertJson(['link' => Storage::disk(static::DISK)->url($this->getAttachmentLocation(true))]); $this->assertDatabaseHas((new PendingAttachment)->getTable(), [ 'draft_id' => $this->draftId, 'disk' => static::DISK, - 'attachment' => $this->file->getClientOriginalName(), + 'attachment' => $this->getAttachmentLocation(true), ]); // Assert the file was stored... - Storage::disk(static::DISK)->assertExists($this->file->getClientOriginalName()); + Storage::disk(static::DISK)->assertExists($this->getAttachmentLocation(true)); } /** @test */ diff --git a/tests/TestCase.php b/tests/TestCase.php index b87ec6c..1ae048a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -17,6 +17,8 @@ abstract class TestCase extends OrchestraTestCase { const DISK = 'public'; + const PATH = 'subpath'; + public static $user; public function setUp(): void diff --git a/tests/TrixDriverUploadTest.php b/tests/TrixDriverUploadTest.php index 37002bc..7f4d7eb 100644 --- a/tests/TrixDriverUploadTest.php +++ b/tests/TrixDriverUploadTest.php @@ -38,16 +38,16 @@ public function store_pending_attachment() { $response = $this->uploadPendingFile(); - $response->assertJson(['link' => Storage::disk(static::DISK)->url($this->file->hashName())]); + $response->assertJson(['link' => Storage::disk(static::DISK)->url($this->getAttachmentLocation())]); $this->assertDatabaseHas((new PendingAttachment)->getTable(), [ 'draft_id' => $this->draftId, 'disk' => static::DISK, - 'attachment' => $this->file->hashName(), + 'attachment' => $this->getAttachmentLocation(), ]); // Assert the file was stored... - Storage::disk(static::DISK)->assertExists($this->file->hashName()); + Storage::disk(static::DISK)->assertExists($this->getAttachmentLocation()); // Assert a file does not exist... Storage::disk(static::DISK)->assertMissing('missing.jpg'); @@ -76,8 +76,8 @@ public function store_attachment() $this->assertDatabaseHas((new Attachment)->getTable(), [ 'disk' => static::DISK, - 'attachment' => $this->file->hashName(), - 'url' => Storage::disk(static::DISK)->url($this->file->hashName()), + 'attachment' => $this->getAttachmentLocation(), + 'url' => Storage::disk(static::DISK)->url($this->getAttachmentLocation()), 'attachable_id' => $response->json('id'), 'attachable_type' => Article::class, ]); @@ -90,13 +90,13 @@ public function detach_attachment() $this->storeArticle(); - Storage::disk(static::DISK)->assertExists($this->file->hashName()); + Storage::disk(static::DISK)->assertExists($this->getAttachmentLocation()); $this->json('DELETE', 'nova-api/articles/trix-attachment/content', [ 'attachmentUrl' => $src, ]); - Storage::disk(static::DISK)->assertMissing($this->file->hashName()); + Storage::disk(static::DISK)->assertMissing($this->getAttachmentLocation()); } /** @test */ @@ -107,7 +107,7 @@ public function discard_pending_attachments() for ($i = 0; $i <= 3; $i++) { $this->uploadPendingFile(); - $fileNames[] = $this->file->hashName(); + $fileNames[] = $this->getAttachmentLocation(); $this->regenerateUpload(); } @@ -131,7 +131,7 @@ public function delete_all_related_attachments() for ($i = 0; $i <= 5; $i++) { $this->uploadPendingFile(); - $fileNames[] = $this->file->hashName(); + $fileNames[] = $this->getAttachmentLocation(); $this->regenerateUpload(); } diff --git a/tests/UploadsHelper.php b/tests/UploadsHelper.php index b20d12e..fa720f4 100644 --- a/tests/UploadsHelper.php +++ b/tests/UploadsHelper.php @@ -2,6 +2,7 @@ namespace Froala\NovaFroalaField\Tests; +use function Froala\NovaFroalaField\nova_version_at_least; use Illuminate\Foundation\Testing\TestResponse; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; @@ -49,4 +50,11 @@ protected function storeArticle(): TestResponse 'contentDraftId' => $this->draftId, ]); } + + protected function getAttachmentLocation($preserveFilename = false): string + { + $filename = $preserveFilename ? $this->file->getClientOriginalName() : $this->file->hashName(); + + return nova_version_at_least('2.7.0') ? rtrim(TestCase::PATH, '/').'/'.$filename : $filename; + } }