-
Notifications
You must be signed in to change notification settings - Fork 728
/
Copy pathRenameController.php
76 lines (59 loc) · 2.29 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
<?php
namespace UniSharp\LaravelFilemanager\Controllers;
use Illuminate\Support\Facades\Storage;
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming;
use UniSharp\LaravelFilemanager\Events\FolderWasRenamed;
use UniSharp\LaravelFilemanager\Events\FileIsRenaming;
use UniSharp\LaravelFilemanager\Events\FileWasRenamed;
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming;
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed;
use Illuminate\Support\Str;
class RenameController extends LfmController
{
public function getRename()
{
$old_name = $this->helper->input('file');
$new_name = $this->helper->input('new_name');
$file = $this->lfm->setName($old_name);
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) {
abort(404);
}
$old_file = $this->lfm->pretty($old_name);
$is_directory = $file->isDirectory();
if (empty($new_name)) {
if ($is_directory) {
return parent::error('folder-name');
} else {
return parent::error('file-name');
}
}
$new_name = Str::slug($new_name, '_');
if (! $is_directory) {
$extension = $old_file->extension();
if ($extension) {
$new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension;
}
}
$new_path = $this->lfm->setName($new_name)->path('absolute');
if ($is_directory) {
event(new FolderIsRenaming($old_file->path(), $new_path));
} else {
event(new FileIsRenaming($old_file->path(), $new_path));
event(new ImageIsRenaming($old_file->path(), $new_path));
}
$old_path = $old_file->path();
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_path, $new_path));
} else {
event(new FileWasRenamed($old_path, $new_path));
event(new ImageWasRenamed($old_path, $new_path));
}
return parent::$success_response;
}
}