From 7d7ffa95d01b001e96daeb1a14aaeb1802aefa30 Mon Sep 17 00:00:00 2001 From: "PITCHAYAKIT-LEG\\Pitchayakit" Date: Mon, 9 Jan 2023 16:47:18 +0700 Subject: [PATCH 1/9] Ability to add path in configuration --- resources/views/trixassets.blade.php | 4 ++++ src/Http/Controllers/TrixAttachmentController.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/resources/views/trixassets.blade.php b/resources/views/trixassets.blade.php index 0170bf4..2bc7cb7 100644 --- a/resources/views/trixassets.blade.php +++ b/resources/views/trixassets.blade.php @@ -113,6 +113,10 @@ function createFormData(data) { formData.append("disk", data.disk); } + if(data.path != undefined) { + formData.append("path", data.path); + } + return formData; } diff --git a/src/Http/Controllers/TrixAttachmentController.php b/src/Http/Controllers/TrixAttachmentController.php index 7408b6f..be50701 100644 --- a/src/Http/Controllers/TrixAttachmentController.php +++ b/src/Http/Controllers/TrixAttachmentController.php @@ -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); @@ -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()); } From 9bd53b8157c19f54a5ebcd47ea6157192c4bcf39 Mon Sep 17 00:00:00 2001 From: pitchayakit Date: Mon, 9 Jan 2023 19:30:17 +0700 Subject: [PATCH 2/9] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c80200e..462f8b4 100644 --- a/README.md +++ b/README.md @@ -219,6 +219,7 @@ if you want to hide buttons or toolbar you can do this. for more configuration r | hideTools | Array | ['text-tools', 'block-tools', 'file-tools', 'history-tools'] | hides group of buttons | hideButtonIcons | Array | ['attach', 'bold', 'italic', 'strike', 'link', 'heading-1', 'quote', 'code', 'bullet-list', 'number-list', 'decrease-nesting-level', 'increase-nesting-level'] | hides a single button | disk | String | 'local' or 's3' or 'any-disk' | sets the attachment storage per field +| path | Strinh | 'any-path' | customize the name or path of the stored file on disk | id | String | 'any-value-you-want' | the id of input which renders [trix. check this link](https://github.com/basecamp/trix#integrating-with-forms) . current id follows this convention `(model lowered class name)-field-modelId` like `post-content-1` or `post-content-new-model` | containerElement | String | valid html tag like `span` or `div` | default container tag is set to `span` you can change it as you want From cdf28d84346653e13adad48ecbdf14d4ed379ff3 Mon Sep 17 00:00:00 2001 From: pitchayakit Date: Mon, 9 Jan 2023 19:30:53 +0700 Subject: [PATCH 3/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 462f8b4..ed7f8b2 100644 --- a/README.md +++ b/README.md @@ -219,7 +219,7 @@ if you want to hide buttons or toolbar you can do this. for more configuration r | hideTools | Array | ['text-tools', 'block-tools', 'file-tools', 'history-tools'] | hides group of buttons | hideButtonIcons | Array | ['attach', 'bold', 'italic', 'strike', 'link', 'heading-1', 'quote', 'code', 'bullet-list', 'number-list', 'decrease-nesting-level', 'increase-nesting-level'] | hides a single button | disk | String | 'local' or 's3' or 'any-disk' | sets the attachment storage per field -| path | Strinh | 'any-path' | customize the name or path of the stored file on disk +| path | String | 'any-path' | customize the name or path of the stored file on disk | id | String | 'any-value-you-want' | the id of input which renders [trix. check this link](https://github.com/basecamp/trix#integrating-with-forms) . current id follows this convention `(model lowered class name)-field-modelId` like `post-content-1` or `post-content-new-model` | containerElement | String | valid html tag like `span` or `div` | default container tag is set to `span` you can change it as you want From 1262a29d14bd4ee28eaa815d0cbef5f3e87e950f Mon Sep 17 00:00:00 2001 From: "PITCHAYAKIT-LEG\\Pitchayakit" Date: Mon, 9 Jan 2023 21:57:37 +0700 Subject: [PATCH 4/9] Fixed code style to support styleci --- src/Http/Controllers/TrixAttachmentController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Controllers/TrixAttachmentController.php b/src/Http/Controllers/TrixAttachmentController.php index be50701..3ae1b88 100644 --- a/src/Http/Controllers/TrixAttachmentController.php +++ b/src/Http/Controllers/TrixAttachmentController.php @@ -39,7 +39,7 @@ public function store(Request $request) public function destroy($url) { - $attachment = TrixAttachment::where('attachment', 'LIKE', "%" . basename($url))->first(); + $attachment = TrixAttachment::where('attachment', 'LIKE', '%'.basename($url))->first(); return response()->json(optional($attachment)->purge()); } From f1f77d7b9b90c46f4c77557bd264f6dd6557219d Mon Sep 17 00:00:00 2001 From: "PITCHAYAKIT-LEG\\Pitchayakit" Date: Tue, 10 Jan 2023 09:38:38 +0700 Subject: [PATCH 5/9] Fixed files doesn't remove when store with custom path --- src/Traits/HasTrixRichText.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Traits/HasTrixRichText.php b/src/Traits/HasTrixRichText.php index 6d8e1c2..b762614 100644 --- a/src/Traits/HasTrixRichText.php +++ b/src/Traits/HasTrixRichText.php @@ -38,12 +38,18 @@ public static function bootHasTrixRichText() ]); $attachments = Arr::get($model->savedAttachments, $field, []); + + $attachments = is_string($attachments) ? json_decode($attachments) : $attachments; - TrixAttachment::whereIn('attachment', is_string($attachments) ? json_decode($attachments) : $attachments) - ->update([ - 'is_pending' => 0, - 'attachable_id' => $model->id, - ]); + foreach($attachments as $attachment) { + TrixAttachment::where('attachment', 'LIKE', "%$attachment") + ->update([ + 'is_pending' => 0, + 'attachable_id' => $model->id, + ]); + } + + } $model->savedTrixFields = []; From fef96c9266e96c138810e7c769b6acd76aea72f6 Mon Sep 17 00:00:00 2001 From: "PITCHAYAKIT-LEG\\Pitchayakit" Date: Tue, 10 Jan 2023 10:46:36 +0700 Subject: [PATCH 6/9] Apply code to support StyleCI --- src/Traits/HasTrixRichText.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Traits/HasTrixRichText.php b/src/Traits/HasTrixRichText.php index b762614..035c68d 100644 --- a/src/Traits/HasTrixRichText.php +++ b/src/Traits/HasTrixRichText.php @@ -38,18 +38,16 @@ public static function bootHasTrixRichText() ]); $attachments = Arr::get($model->savedAttachments, $field, []); - + $attachments = is_string($attachments) ? json_decode($attachments) : $attachments; - foreach($attachments as $attachment) { + foreach ($attachments as $attachment) { TrixAttachment::where('attachment', 'LIKE', "%$attachment") ->update([ 'is_pending' => 0, 'attachable_id' => $model->id, ]); } - - } $model->savedTrixFields = []; From 8c36074af71e9512d753f6d09807d6aa9a526da7 Mon Sep 17 00:00:00 2001 From: "PITCHAYAKIT-LEG\\Pitchayakit" Date: Wed, 11 Jan 2023 11:33:42 +0700 Subject: [PATCH 7/9] Update test script to support store file with path feature --- .phpunit.result.cache | 1 + resources/views/trixassets.blade.php | 4 ++++ src/Http/Controllers/TrixAttachmentController.php | 4 ++-- tests/Feature/TrixAttachmentControllerTest.php | 3 ++- 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..e087bac --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\TrixAttachmentControllerTest::it_can_store_attachment_request":3},"times":{"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\HasTrixRichTextTest::it_can_store_rich_text":0.274,"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\HasTrixRichTextTest::it_can_store_attachment":0.016,"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\TrixAttachmentControllerTest::it_can_store_attachment_request":0.065,"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\TrixAttachmentControllerTest::it_destroy_attachment":0.021,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_returns_default_trix_html":0.013,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_hides_toolbar":0.009,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_hides_tools":0.009,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_hides_button_icons":0.008,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_changes_id":0.008,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_changes_disk":0.01,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_change_container_element":0.009,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_renders_correct_id_for_new_model":0.011,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_returns_new_model_using_app_make":0.009}} \ No newline at end of file diff --git a/resources/views/trixassets.blade.php b/resources/views/trixassets.blade.php index 0170bf4..2bc7cb7 100644 --- a/resources/views/trixassets.blade.php +++ b/resources/views/trixassets.blade.php @@ -113,6 +113,10 @@ function createFormData(data) { formData.append("disk", data.disk); } + if(data.path != undefined) { + formData.append("path", data.path); + } + return formData; } diff --git a/src/Http/Controllers/TrixAttachmentController.php b/src/Http/Controllers/TrixAttachmentController.php index 7408b6f..3ae1b88 100644 --- a/src/Http/Controllers/TrixAttachmentController.php +++ b/src/Http/Controllers/TrixAttachmentController.php @@ -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); @@ -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()); } diff --git a/tests/Feature/TrixAttachmentControllerTest.php b/tests/Feature/TrixAttachmentControllerTest.php index 52ddbf9..fa2ac5e 100644 --- a/tests/Feature/TrixAttachmentControllerTest.php +++ b/tests/Feature/TrixAttachmentControllerTest.php @@ -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() ); From d21760296697d598ee8e1fbab6074e25317a2923 Mon Sep 17 00:00:00 2001 From: "PITCHAYAKIT-LEG\\Pitchayakit" Date: Wed, 11 Jan 2023 11:37:32 +0700 Subject: [PATCH 8/9] Remove non related file from previous commit --- .phpunit.result.cache | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index e087bac..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"defects":{"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\TrixAttachmentControllerTest::it_can_store_attachment_request":3},"times":{"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\HasTrixRichTextTest::it_can_store_rich_text":0.274,"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\HasTrixRichTextTest::it_can_store_attachment":0.016,"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\TrixAttachmentControllerTest::it_can_store_attachment_request":0.065,"Te7aHoudini\\LaravelTrix\\Tests\\Feature\\TrixAttachmentControllerTest::it_destroy_attachment":0.021,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_returns_default_trix_html":0.013,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_hides_toolbar":0.009,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_hides_tools":0.009,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_hides_button_icons":0.008,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_changes_id":0.008,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_changes_disk":0.01,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_change_container_element":0.009,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_renders_correct_id_for_new_model":0.011,"Te7aHoudini\\LaravelTrix\\Tests\\Unit\\LaravelTrixTest::it_returns_new_model_using_app_make":0.009}} \ No newline at end of file From c47e53c529880a1a5de026f7111e17960ffc2b37 Mon Sep 17 00:00:00 2001 From: "PITCHAYAKIT-LEG\\Pitchayakit" Date: Wed, 11 Jan 2023 11:43:41 +0700 Subject: [PATCH 9/9] Fixed code to support StyleCI --- tests/Feature/TrixAttachmentControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/TrixAttachmentControllerTest.php b/tests/Feature/TrixAttachmentControllerTest.php index fa2ac5e..a81f347 100644 --- a/tests/Feature/TrixAttachmentControllerTest.php +++ b/tests/Feature/TrixAttachmentControllerTest.php @@ -23,7 +23,7 @@ public function it_can_store_attachment_request() 'modelClass' => Post::class, 'field' => 'fooField', 'disk' => 'fooDisk', - 'path' => 'fooPath' + 'path' => 'fooPath', ]); $this->assertTrue( TrixAttachment::where('attachment', 'LIKE', '%'.basename($response->decodeResponseJson()->json('url')))