Skip to content
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

[upd] Slug filename #1065

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
18 changes: 18 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
@@ -116,6 +116,24 @@ Detail options are explained here:

## Upload / Validation:

| Key | Type | Description |
|----------------------------|---------|---------------------------------------------------------------------------|
| 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 | 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. |
| max\_file\_size | int | Specify max size of uploading file. |
| should\_validate\_mime | boolean | If true, the mime type of uploading file will be verified. |
| valid\_image\_mimetypes | array | Array of mime types. Available since v1.3.0 . |
| should\_create\_thumbnails | boolean | If true, thumbnails will be created for faster loading. |
| raster\_mimetypes | array | Array of mime types. Thumbnails will be created only for these mimetypes. |
| create\_folder\_mode | int | Permission setting for folders created by this package. |
| create\_file\_mode | int | Permission setting for files uploaded to this package. |
| should\_change\_file\_mode | boolean | If true, it will attempt to chmod the file after upload |
| valid\_file\_mimetypes | array | Array of mime types. Available since v1.3.0 . |
### disk

* type: `string`
11 changes: 8 additions & 3 deletions src/Controllers/CropController.php
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 3 additions & 3 deletions src/Controllers/FolderController.php
Original file line number Diff line number Diff line change
@@ -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();
}
9 changes: 2 additions & 7 deletions src/Controllers/RenameController.php
Original file line number Diff line number Diff line change
@@ -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();
14 changes: 12 additions & 2 deletions src/Controllers/ResizeController.php
Original file line number Diff line number Diff line change
@@ -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;
15 changes: 10 additions & 5 deletions src/LfmPath.php
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
use UniSharp\LaravelFilemanager\Events\FileWasUploaded;
use UniSharp\LaravelFilemanager\Events\ImageIsUploading;
use UniSharp\LaravelFilemanager\Events\ImageWasUploaded;
use Illuminate\Support\Str;
use UniSharp\LaravelFilemanager\LfmUploadValidator;

class LfmPath
@@ -227,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) {
@@ -274,11 +283,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);
}
$new_file_name = Str::slug($new_file_name, '_');

if ($extension) {
$new_file_name_with_extention = $new_file_name . '.' . $extension;
6 changes: 4 additions & 2 deletions src/config/lfm.php
Original file line number Diff line number Diff line change
@@ -100,9 +100,11 @@

'rename_duplicates' => false,

'alphanumeric_filename' => false,
'slug_filename' => false,

'alphanumeric_directory' => false,

'alphanumeric_directory' => false,
'alphanumeric_filename' => false,

'should_validate_size' => false,