-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathRenameController.php
84 lines (66 loc) · 2.59 KB
/
RenameController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace UniSharp\LaravelFilemanager\Controllers;
use Illuminate\Support\Str;
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming;
use UniSharp\LaravelFilemanager\Events\FolderWasRenamed;
class RenameController extends LfmController
{
public function getRename()
{
$old_name = $this->helper->input('file');
$new_name = $this->helper->input('new_name');
$old_file = $this->lfm->pretty($old_name);
$is_directory = $old_file->isDirectory();
if (empty($new_name)) {
if ($is_directory) {
return parent::error('folder-name');
} else {
return parent::error('file-name');
}
}
if ($is_directory && config('lfm.alphanumeric_directory')) {
if (config('lfm.convert_to_alphanumeric')) {
$new_name = Str::slug($new_name);
}
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);
}
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');
if ($is_directory) {
event(new FolderIsRenaming($old_file->path(), $new_file));
} else {
event(new ImageIsRenaming($old_file->path(), $new_file));
}
if ($old_file->hasThumb()) {
$this->lfm->setName($old_name)->thumb()
->move($this->lfm->setName($new_name)->thumb());
}
$this->lfm->setName($old_name)
->move($this->lfm->setName($new_name));
if ($is_directory) {
event(new FolderWasRenamed($old_file->path(), $new_file));
} else {
event(new ImageWasRenamed($old_file->path(), $new_file));
}
return parent::$success_response;
}
}