Skip to content

Use slug instead of preg_replace #941

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 2 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
19 changes: 14 additions & 5 deletions src/Controllers/FolderController.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

namespace UniSharp\LaravelFilemanager\Controllers;

use Illuminate\Support\Str;

class FolderController extends LfmController
{
/**
@@ -42,13 +44,20 @@ public function getAddfolder()
try {
if ($folder_name === null || $folder_name == '') {
return $this->helper->error('folder-name');
} elseif ($this->lfm->setName($folder_name)->exists()) {
}
if ($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();
}

if (config('lfm.alphanumeric_directory')) {
if (config('lfm.convert_to_alphanumeric')) {
$folder_name = Str::slug($folder_name);
} elseif (preg_match('/[^\w\-_]/i', $folder_name)) {
return $this->helper->error('folder-alnum');
}
}

$this->lfm->setName($folder_name)->createFolder();
} catch (\Exception $e) {
return $e->getMessage();
}
33 changes: 25 additions & 8 deletions src/Controllers/RenameController.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace UniSharp\LaravelFilemanager\Controllers;

use Illuminate\Support\Str;
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming;
@@ -26,18 +27,34 @@ public function getRename()
}
}

if (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
return parent::error('folder-alnum');
// return parent::error('file-alnum');
} elseif ($this->lfm->setName($new_name)->exists()) {
return parent::error('rename');
}
if ($is_directory && config('lfm.alphanumeric_directory')) {
if (config('lfm.convert_to_alphanumeric')) {
$new_name = Str::slug($new_name);
}

if (! $is_directory) {
if (preg_match('/[^\w\-_]/i', $new_name)) {
return parent::error('folder-alnum');
}
} elseif (!$is_directory && config('lfm.alphanumeric_filename')) {
// Remove extension for checks to alphanum characters
$extension = $old_file->extension();
if ($extension) {
$new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension;
$new_name = str_replace('.' . $extension, '', $new_name);
}

if (config('lfm.convert_to_alphanumeric')) {
$new_name = Str::slug($new_name);
}

if (preg_match('/[^\w\-_]/i', $new_name)) {
return parent::error('file-alnum');
}

$new_name .= ($extension) ? '.' . $extension : null;
}

if ($this->lfm->setName($new_name)->exists()) {
return parent::error('rename');
}

$new_file = $this->lfm->setName($new_name)->path('absolute');
3 changes: 2 additions & 1 deletion src/LfmPath.php
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
namespace UniSharp\LaravelFilemanager;

use Illuminate\Container\Container;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use UniSharp\LaravelFilemanager\Events\ImageIsUploading;
@@ -276,7 +277,7 @@ private function getNewName($file)
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);
}

$extension = $file->getClientOriginalExtension();
3 changes: 3 additions & 0 deletions src/config/lfm.php
Original file line number Diff line number Diff line change
@@ -98,6 +98,9 @@

'alphanumeric_directory' => false,

// When creating folder or renaming folder/file, automatically convert to alphanumeric instead of error
'convert_to_alphanumeric' => false,

'should_validate_size' => false,

'should_validate_mime' => false,