diff --git a/src/Http/Controllers/EditorJsImageUploadController.php b/src/Http/Controllers/EditorJsImageUploadController.php index f3f90e5..efe7cd3 100644 --- a/src/Http/Controllers/EditorJsImageUploadController.php +++ b/src/Http/Controllers/EditorJsImageUploadController.php @@ -19,6 +19,7 @@ class EditorJsImageUploadController extends Controller */ public function file(NovaRequest $request) { + $validator = Validator::make($request->all(), [ 'image' => 'required|image', ]); @@ -34,8 +35,22 @@ public function file(NovaRequest $request) config('nova-editor-js.toolSettings.image.disk') ); - $this->applyAlterations(Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->path($path)); - $thumbnails = $this->applyThumbnails($path); + if(config('nova-editor-js.toolSettings.image.disk') !== 'local'){ + + $tempPath = $request->file('image')->store( + config('nova-editor-js.toolSettings.image.path'), + 'local' + ); + + $this->applyAlterations(Storage::disk('local')->path($tempPath)); + $thumbnails = $this->applyThumbnails($tempPath); + + $this->deleteThumbnails(Storage::disk('local')->path($tempPath)); + Storage::disk('local')->delete($path); + }else{ + $this->applyAlterations(Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->path($path)); + $thumbnails = $this->applyThumbnails($path); + } return [ 'success' => 1, @@ -100,6 +115,7 @@ function ($attribute, $value, $fail) { private function applyAlterations($path, $alterations = []) { try { + $image = Image::load($path); $imageSettings = config('nova-editor-js.toolSettings.image.alterations'); @@ -178,16 +194,47 @@ private function applyThumbnails($path) $newThumbnailName = $filename . $thumbnailName . '.' . $extension; $newThumbnailPath = config('nova-editor-js.toolSettings.image.path') . '/' . $newThumbnailName; + + Storage::disk(config('nova-editor-js.toolSettings.image.disk') )->copy($path, $newThumbnailPath); - Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->copy($path, $newThumbnailPath); + if(config('nova-editor-js.toolSettings.image.disk') !== 'local'){ + Storage::disk('local')->copy($path, $newThumbnailPath); + $newPath = Storage::disk('local')->path($newThumbnailPath); + $this->applyAlterations($newPath, $setting); + }else{ + $newPath = Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->path($newThumbnailPath); + $this->applyAlterations($newPath, $setting); + } - $newPath = Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->path($newThumbnailPath); - $this->applyAlterations($newPath, $setting); + $generatedThumbnails[] = Storage::disk(config('nova-editor-js.toolSettings.image.disk') )->url($newThumbnailPath); - $generatedThumbnails[] = Storage::disk(config('nova-editor-js.toolSettings.image.disk'))->url($newThumbnailPath); + // Storage::disk('local')->delete($path, $newThumbnailPath); } } return $generatedThumbnails; } + + + private function deleteThumbnails($path) + { + $thumbnailSettings = config('nova-editor-js.toolSettings.image.thumbnails'); + + if (!empty($thumbnailSettings)) { + foreach ($thumbnailSettings as $thumbnailName => $setting) { + $filename = pathinfo($path, PATHINFO_FILENAME); + $extension = pathinfo($path, PATHINFO_EXTENSION); + + $newThumbnailName = $filename . $thumbnailName . '.' . $extension; + $newThumbnailPath = config('nova-editor-js.toolSettings.image.path') . '/' . $newThumbnailName; + + Storage::disk('local')->delete($path, $newThumbnailPath); + } + } + + } + + + + }