Skip to content

Commit 38e304f

Browse files
authored
Merge pull request #941 from arxeiss/better-slug
Use slug instead of preg_replace
2 parents 319439f + fac6001 commit 38e304f

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

src/Controllers/FolderController.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace UniSharp\LaravelFilemanager\Controllers;
44

5+
use Illuminate\Support\Str;
56
use UniSharp\LaravelFilemanager\Events\FolderIsCreating;
67
use UniSharp\LaravelFilemanager\Events\FolderWasCreated;
78

@@ -49,13 +50,20 @@ public function getAddfolder()
4950
try {
5051
if ($folder_name === null || $folder_name == '') {
5152
return $this->helper->error('folder-name');
52-
} elseif ($this->lfm->setName($folder_name)->exists()) {
53+
}
54+
if ($this->lfm->setName($folder_name)->exists()) {
5355
return $this->helper->error('folder-exist');
54-
} elseif (config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $folder_name)) {
55-
return $this->helper->error('folder-alnum');
56-
} else {
57-
$this->lfm->setName($folder_name)->createFolder();
5856
}
57+
58+
if (config('lfm.alphanumeric_directory')) {
59+
if (config('lfm.convert_to_alphanumeric')) {
60+
$folder_name = Str::slug($folder_name);
61+
} elseif (preg_match('/[^\w\-_]/i', $folder_name)) {
62+
return $this->helper->error('folder-alnum');
63+
}
64+
}
65+
66+
$this->lfm->setName($folder_name)->createFolder();
5967
} catch (\Exception $e) {
6068
return $e->getMessage();
6169
}

src/Controllers/RenameController.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
namespace UniSharp\LaravelFilemanager\Controllers;
44

55
use Illuminate\Support\Facades\Storage;
6+
use Illuminate\Support\Str;
7+
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
8+
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
69
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming;
710
use UniSharp\LaravelFilemanager\Events\FolderWasRenamed;
811
use UniSharp\LaravelFilemanager\Events\FileIsRenaming;
@@ -35,19 +38,34 @@ public function getRename()
3538
}
3639
}
3740

38-
if ($is_directory && config('lfm.alphanumeric_directory') && preg_match('/[^\w-]/i', $new_name)) {
39-
return response()->json(parent::error('folder-alnum'), 400);
40-
} elseif (config('lfm.alphanumeric_filename') && preg_match('/[^.\w-]/i', $new_name)) {
41-
return response()->json(parent::error('file-alnum'), 400);
42-
} elseif ($this->lfm->setName($new_name)->exists()) {
43-
return response()->json(parent::error('rename'), 400);
44-
}
41+
if ($is_directory && config('lfm.alphanumeric_directory')) {
42+
if (config('lfm.convert_to_alphanumeric')) {
43+
$new_name = Str::slug($new_name);
44+
}
4545

46-
if (! $is_directory) {
46+
if (preg_match('/[^\w\-_]/i', $new_name)) {
47+
return parent::error('folder-alnum');
48+
}
49+
} elseif (!$is_directory && config('lfm.alphanumeric_filename')) {
50+
// Remove extension for checks to alphanum characters
4751
$extension = $old_file->extension();
4852
if ($extension) {
49-
$new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension;
53+
$new_name = str_replace('.' . $extension, '', $new_name);
54+
}
55+
56+
if (config('lfm.convert_to_alphanumeric')) {
57+
$new_name = Str::slug($new_name);
58+
}
59+
60+
if (preg_match('/[^\w\-_]/i', $new_name)) {
61+
return parent::error('file-alnum');
5062
}
63+
64+
$new_name .= ($extension) ? '.' . $extension : null;
65+
}
66+
67+
if ($this->lfm->setName($new_name)->exists()) {
68+
return parent::error('rename');
5169
}
5270

5371
$new_path = $this->lfm->setName($new_name)->path('absolute');

src/LfmPath.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace UniSharp\LaravelFilemanager;
44

55
use Illuminate\Container\Container;
6+
use Illuminate\Support\Str;
7+
use Intervention\Image\Facades\Image;
68
use Symfony\Component\HttpFoundation\File\UploadedFile;
79
use UniSharp\LaravelFilemanager\Services\ImageService;
810
use UniSharp\LaravelFilemanager\Events\FileIsUploading;
@@ -287,7 +289,7 @@ private function getNewName($file)
287289
if (config('lfm.rename_file') === true) {
288290
$new_file_name = uniqid();
289291
} elseif (config('lfm.alphanumeric_filename') === true) {
290-
$new_file_name = preg_replace('/[^A-Za-z0-9\-\']/', '_', $new_file_name);
292+
$new_file_name = Str::slug($new_file_name);
291293
}
292294

293295
if ($extension) {

src/config/lfm.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@
112112

113113
'alphanumeric_directory' => false,
114114

115+
// When creating folder or renaming folder/file, automatically convert to alphanumeric instead of error
116+
'convert_to_alphanumeric' => false,
117+
115118
'should_validate_size' => false,
116119

117120
'should_validate_mime' => true,

0 commit comments

Comments
 (0)