From 65fe1b9061a129a68fcf5da4019c809bad8c3541 Mon Sep 17 00:00:00 2001 From: Candido Alberto Silva Date: Mon, 5 Apr 2021 12:33:57 -0400 Subject: [PATCH 1/3] [upd] Slug filename --- docs/config.md | 3 ++- src/LfmPath.php | 3 +++ src/config/lfm.php | 2 ++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/config.md b/docs/config.md index 809c9d9f..795cb14a 100644 --- a/docs/config.md +++ b/docs/config.md @@ -48,7 +48,8 @@ |----------------------------|---------|---------------------------------------------------------------------------| | disk (Alpha version only) | string | Correspond to `disks` section in `config/filesystems.php`. | | rename_file | string | If true, the uploaded file will be renamed to uniqid() + file extension. | -| alphanumeric_filename | string | If true, non-alphanumeric file name will be replaced with `_`. | +| alphanumeric_filename | boolean | If true, non-alphanumeric file name will be replaced with `_`. | +| slug_filename | boolean | If true, use Str::slug class Laravel method generates a URL friendly | | alphanumeric_directory | boolean | If true, non-alphanumeric folder name will be rejected. | | should\_validate\_size | boolean | If true, the size of uploading file will be verified. | | max\_image\_size | int | Specify max size of uploading image. | diff --git a/src/LfmPath.php b/src/LfmPath.php index fcd2f3c3..b1a790a5 100644 --- a/src/LfmPath.php +++ b/src/LfmPath.php @@ -8,6 +8,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile; use UniSharp\LaravelFilemanager\Events\ImageIsUploading; use UniSharp\LaravelFilemanager\Events\ImageWasUploaded; +use Illuminate\Support\Str; class LfmPath { @@ -280,6 +281,8 @@ private function getNewName($file) $new_file_name = uniqid(); } elseif (config('lfm.alphanumeric_filename') === true) { $new_file_name = preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_file_name); + } elseif (config('lfm.slug_filename') === true){ + $new_file_name = Str::slug($new_file_name, '_'); } $extension = $file->getClientOriginalExtension(); diff --git a/src/config/lfm.php b/src/config/lfm.php index b8d6e896..04512086 100644 --- a/src/config/lfm.php +++ b/src/config/lfm.php @@ -93,6 +93,8 @@ 'alphanumeric_filename' => false, + 'slug_filename' => false, + 'alphanumeric_directory' => false, 'should_validate_size' => false, From 3cc4695db77b8ec3ca2328a9c968f22bf802bd36 Mon Sep 17 00:00:00 2001 From: Candido Alberto Silva Date: Mon, 28 Feb 2022 22:26:05 -0400 Subject: [PATCH 2/3] [upd] alter name and publish packagist --- composer.json | 2 +- src/LfmPath.php | 11 +---------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 028f6c2c..a545d668 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "unisharp/laravel-filemanager", + "name": "candidoalberto/laravel-filemanager", "description": "A file upload/editor intended for use with Laravel 5 to 6 and CKEditor / TinyMCE", "license": "MIT", "keywords": [ diff --git a/src/LfmPath.php b/src/LfmPath.php index a503a193..0014c5e9 100644 --- a/src/LfmPath.php +++ b/src/LfmPath.php @@ -9,11 +9,8 @@ use UniSharp\LaravelFilemanager\Events\FileWasUploaded; use UniSharp\LaravelFilemanager\Events\ImageIsUploading; use UniSharp\LaravelFilemanager\Events\ImageWasUploaded; -<<<<<<< HEAD use Illuminate\Support\Str; -======= use UniSharp\LaravelFilemanager\LfmUploadValidator; ->>>>>>> 98f57e5f2eb5af652a32266cd9b9f7f047a77d62 class LfmPath { @@ -278,13 +275,7 @@ private function getNewName($file) $extension = $file->getClientOriginalExtension(); - if (config('lfm.rename_file') === true) { - $new_file_name = uniqid(); - } elseif (config('lfm.alphanumeric_filename') === true) { - $new_file_name = preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_file_name); - } elseif (config('lfm.slug_filename') === true){ - $new_file_name = Str::slug($new_file_name, '_'); - } + $new_file_name = Str::slug($new_file_name, '_'); if ($extension) { $new_file_name_with_extention = $new_file_name . '.' . $extension; From 96b8fa4eace40212b9f4816bae98758790a48f19 Mon Sep 17 00:00:00 2001 From: Candido Alberto Silva Date: Tue, 1 Mar 2022 00:08:35 -0400 Subject: [PATCH 3/3] [upd] rename slug name folder and files && upload and resize --- src/Controllers/CropController.php | 11 ++++++++--- src/Controllers/FolderController.php | 6 +++--- src/Controllers/RenameController.php | 9 ++------- src/Controllers/ResizeController.php | 14 ++++++++++++-- src/LfmPath.php | 8 ++++++++ 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Controllers/CropController.php b/src/Controllers/CropController.php index a78e4113..27446cc6 100644 --- a/src/Controllers/CropController.php +++ b/src/Controllers/CropController.php @@ -42,9 +42,14 @@ public function getCropimage($overWrite = true) $crop_info = request()->only('dataWidth', 'dataHeight', 'dataX', 'dataY'); // crop image - Image::make($image_path) - ->crop(...array_values($crop_info)) - ->save($crop_path); + $name = $this->helper->getNameFromPath($image_path); + + $imagefile = $this->lfm->pretty($image_path); + + $image = Image::make($imagefile->get()) + ->crop(...array_values($crop_info)); + + $this->lfm->setName($name)->storage->put($image->stream()); // make new thumbnail $this->lfm->generateThumbnail($image_name); diff --git a/src/Controllers/FolderController.php b/src/Controllers/FolderController.php index 3e5b666d..64a0cd56 100644 --- a/src/Controllers/FolderController.php +++ b/src/Controllers/FolderController.php @@ -4,7 +4,7 @@ use UniSharp\LaravelFilemanager\Events\FolderIsCreating; use UniSharp\LaravelFilemanager\Events\FolderWasCreated; - +use Illuminate\Support\Str; class FolderController extends LfmController { /** @@ -42,6 +42,8 @@ public function getAddfolder() { $folder_name = $this->helper->input('name'); + $folder_name = Str::slug($folder_name, '_'); + $new_path = $this->lfm->setName($folder_name)->path('absolute'); event(new FolderIsCreating($new_path)); @@ -51,8 +53,6 @@ public function getAddfolder() return $this->helper->error('folder-name'); } elseif ($this->lfm->setName($folder_name)->exists()) { return $this->helper->error('folder-exist'); - } elseif (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $folder_name)) { - return $this->helper->error('folder-alnum'); } else { $this->lfm->setName($folder_name)->createFolder(); } diff --git a/src/Controllers/RenameController.php b/src/Controllers/RenameController.php index d8187d58..4a86bb5e 100644 --- a/src/Controllers/RenameController.php +++ b/src/Controllers/RenameController.php @@ -9,6 +9,7 @@ use UniSharp\LaravelFilemanager\Events\FileWasRenamed; use UniSharp\LaravelFilemanager\Events\ImageIsRenaming; use UniSharp\LaravelFilemanager\Events\ImageWasRenamed; +use Illuminate\Support\Str; class RenameController extends LfmController { @@ -35,13 +36,7 @@ public function getRename() } } - if ($is_directory && config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) { - return parent::error('folder-alnum'); - } elseif (config('lfm.alphanumeric_filename') && preg_match('/[^.\w-]/i', $new_name)) { - return parent::error('file-alnum'); - } elseif ($this->lfm->setName($new_name)->exists()) { - return parent::error('rename'); - } + $new_name = Str::slug($new_name, '_'); if (! $is_directory) { $extension = $old_file->extension(); diff --git a/src/Controllers/ResizeController.php b/src/Controllers/ResizeController.php index 39693734..9ace12e8 100644 --- a/src/Controllers/ResizeController.php +++ b/src/Controllers/ResizeController.php @@ -18,7 +18,9 @@ public function getResize() $ratio = 1.0; $image = request('img'); - $original_image = Image::make($this->lfm->setName($image)->path('absolute')); + $imagefile = $this->lfm->pretty($this->lfm->setName($image)->path('absolute')); + + $original_image = Image::make($imagefile->get()); $original_width = $original_image->width(); $original_height = $original_image->height(); @@ -57,7 +59,15 @@ public function performResize() $image_path = $this->lfm->setName(request('img'))->path('absolute'); event(new ImageIsResizing($image_path)); - Image::make($image_path)->resize(request('dataWidth'), request('dataHeight'))->save(); + + $name = $this->helper->getNameFromPath($image_path); + + $imagefile = $this->lfm->pretty($image_path); + + $image = Image::make($imagefile->get())->resize(request('dataWidth'), request('dataHeight')); + + $this->lfm->setName($name)->storage->put($image->stream()); + event(new ImageWasResized($image_path)); return parent::$success_response; diff --git a/src/LfmPath.php b/src/LfmPath.php index 0014c5e9..b3f5e00e 100644 --- a/src/LfmPath.php +++ b/src/LfmPath.php @@ -228,7 +228,15 @@ public function upload($file) event(new FileIsUploading($new_file_path)); event(new ImageIsUploading($new_file_path)); try { + $this->setName($new_file_name)->storage->save($file); + + //----- Rezise image + $original_image = $this->pretty($new_file_name); + $image = Image::make($original_image->get())->resize(800, 600,function ($constraint) { + $constraint->aspectRatio(); + }); + $this->setName($new_file_name)->storage->put($image->stream()); $this->generateThumbnail($new_file_name); } catch (\Exception $e) {