Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
Fix withFiles($disk, $path) not writing to $path (#53)
Browse files Browse the repository at this point in the history
Fixed files storing consider to new `withFiles($disk, $path)` method signature and `StorableContract`
  • Loading branch information
clingle authored Feb 17, 2020
1 parent 34e2849 commit c32882f
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 28 deletions.
12 changes: 11 additions & 1 deletion src/Froala.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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.
*
Expand Down
8 changes: 5 additions & 3 deletions src/Handlers/StorePendingAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/TestResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
}
}
4 changes: 2 additions & 2 deletions tests/FroalaImageManagerControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
}
}
18 changes: 9 additions & 9 deletions tests/FroalaUploadControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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 */
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PreserveFilenamesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ abstract class TestCase extends OrchestraTestCase
{
const DISK = 'public';

const PATH = 'subpath';

public static $user;

public function setUp(): void
Expand Down
18 changes: 9 additions & 9 deletions tests/TrixDriverUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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 */
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
8 changes: 8 additions & 0 deletions tests/UploadsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit c32882f

Please sign in to comment.