1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UniSharp\LaravelFilemanager\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
use UniSharp\LaravelFilemanager\Events\FolderIsRenaming; |
7
|
|
|
use UniSharp\LaravelFilemanager\Events\FolderWasRenamed; |
8
|
|
|
use UniSharp\LaravelFilemanager\Events\FileIsRenaming; |
9
|
|
|
use UniSharp\LaravelFilemanager\Events\FileWasRenamed; |
10
|
|
|
use UniSharp\LaravelFilemanager\Events\ImageIsRenaming; |
11
|
|
|
use UniSharp\LaravelFilemanager\Events\ImageWasRenamed; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
class RenameController extends LfmController |
15
|
|
|
{ |
16
|
|
|
public function getRename() |
17
|
|
|
{ |
18
|
|
|
$old_name = $this->helper->input('file'); |
|
|
|
|
19
|
|
|
$new_name = $this->helper->input('new_name'); |
20
|
|
|
|
21
|
|
|
$file = $this->lfm->setName($old_name); |
|
|
|
|
22
|
|
|
|
23
|
|
|
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) { |
24
|
|
|
abort(404); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$old_file = $this->lfm->pretty($old_name); |
28
|
|
|
|
29
|
|
|
$is_directory = $file->isDirectory(); |
30
|
|
|
|
31
|
|
|
if (empty($new_name)) { |
32
|
|
|
if ($is_directory) { |
33
|
|
|
return parent::error('folder-name'); |
34
|
|
|
} else { |
35
|
|
|
return parent::error('file-name'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$new_name = Str::slug($new_name, '_'); |
40
|
|
|
|
41
|
|
|
if (! $is_directory) { |
42
|
|
|
$extension = $old_file->extension(); |
43
|
|
|
if ($extension) { |
44
|
|
|
$new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$new_path = $this->lfm->setName($new_name)->path('absolute'); |
49
|
|
|
|
50
|
|
|
if ($is_directory) { |
51
|
|
|
event(new FolderIsRenaming($old_file->path(), $new_path)); |
|
|
|
|
52
|
|
|
} else { |
53
|
|
|
event(new FileIsRenaming($old_file->path(), $new_path)); |
54
|
|
|
event(new ImageIsRenaming($old_file->path(), $new_path)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$old_path = $old_file->path(); |
58
|
|
|
|
59
|
|
|
if ($old_file->hasThumb()) { |
60
|
|
|
$this->lfm->setName($old_name)->thumb() |
61
|
|
|
->move($this->lfm->setName($new_name)->thumb()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->lfm->setName($old_name) |
65
|
|
|
->move($this->lfm->setName($new_name)); |
66
|
|
|
|
67
|
|
|
if ($is_directory) { |
68
|
|
|
event(new FolderWasRenamed($old_path, $new_path)); |
69
|
|
|
} else { |
70
|
|
|
event(new FileWasRenamed($old_path, $new_path)); |
71
|
|
|
event(new ImageWasRenamed($old_path, $new_path)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return parent::$success_response; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|