Skip to content

Revise ability to custom the name or path of the stored file on disk #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions resources/views/trixassets.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ function createFormData(data) {
formData.append("disk", data.disk);
}

if(data.path != undefined) {
formData.append("path", data.path);
}

return formData;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/TrixAttachmentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function store(Request $request)
return response()->json(['errors'=>$validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY);
}

$attachment = $request->file->store('/', $request->disk ?? config('laravel-trix.storage_disk'));
$attachment = $request->file->store($request->path ?? '/', $request->disk ?? config('laravel-trix.storage_disk'));

$url = Storage::disk($request->disk ?? config('laravel-trix.storage_disk'))->url($attachment);

Expand All @@ -39,7 +39,7 @@ public function store(Request $request)

public function destroy($url)
{
$attachment = TrixAttachment::where('attachment', basename($url))->first();
$attachment = TrixAttachment::where('attachment', 'LIKE', '%'.basename($url))->first();

return response()->json(optional($attachment)->purge());
}
Expand Down
14 changes: 9 additions & 5 deletions src/Traits/HasTrixRichText.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public static function bootHasTrixRichText()

$attachments = Arr::get($model->savedAttachments, $field, []);

TrixAttachment::whereIn('attachment', is_string($attachments) ? json_decode($attachments) : $attachments)
->update([
'is_pending' => 0,
'attachable_id' => $model->id,
]);
$attachments = is_string($attachments) ? json_decode($attachments) : $attachments;

foreach ($attachments as $attachment) {
TrixAttachment::where('attachment', 'LIKE', "%$attachment")
->update([
'is_pending' => 0,
'attachable_id' => $model->id,
]);
}
}

$model->savedTrixFields = [];
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/TrixAttachmentControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public function it_can_store_attachment_request()
'modelClass' => Post::class,
'field' => 'fooField',
'disk' => 'fooDisk',
'path' => 'fooPath',
]);
$this->assertTrue(
TrixAttachment::where('attachment', basename($response->decodeResponseJson()->json('url')))
TrixAttachment::where('attachment', 'LIKE', '%'.basename($response->decodeResponseJson()->json('url')))
->where('is_pending', 1)
->exists()
);
Expand Down