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
|
|
|
|
13
|
|
|
class RenameController extends LfmController |
14
|
|
|
{ |
15
|
|
|
public function getRename() |
16
|
|
|
{ |
17
|
|
|
$old_name = $this->helper->input('file'); |
|
|
|
|
18
|
|
|
$new_name = $this->helper->input('new_name'); |
19
|
|
|
|
20
|
|
|
$file = $this->lfm->setName($old_name); |
|
|
|
|
21
|
|
|
|
22
|
|
|
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) { |
23
|
|
|
abort(404); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$old_file = $this->lfm->pretty($old_name); |
27
|
|
|
|
28
|
|
|
$is_directory = $file->isDirectory(); |
29
|
|
|
|
30
|
|
|
if (empty($new_name)) { |
31
|
|
|
if ($is_directory) { |
32
|
|
|
return response()->json(parent::error('folder-name'), 400); |
|
|
|
|
33
|
|
|
} else { |
34
|
|
|
return response()->json(parent::error('file-name'), 400); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
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
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (! $is_directory) { |
47
|
|
|
$extension = $old_file->extension(); |
48
|
|
|
if ($extension) { |
49
|
|
|
$new_name = str_replace('.' . $extension, '', $new_name) . '.' . $extension; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$new_path = $this->lfm->setName($new_name)->path('absolute'); |
54
|
|
|
|
55
|
|
|
if ($is_directory) { |
56
|
|
|
event(new FolderIsRenaming($old_file->path(), $new_path)); |
|
|
|
|
57
|
|
|
} else { |
58
|
|
|
event(new FileIsRenaming($old_file->path(), $new_path)); |
59
|
|
|
event(new ImageIsRenaming($old_file->path(), $new_path)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$old_path = $old_file->path(); |
63
|
|
|
|
64
|
|
|
if ($old_file->hasThumb()) { |
65
|
|
|
$this->lfm->setName($old_name)->thumb() |
66
|
|
|
->move($this->lfm->setName($new_name)->thumb()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->lfm->setName($old_name) |
70
|
|
|
->move($this->lfm->setName($new_name)); |
71
|
|
|
|
72
|
|
|
if ($is_directory) { |
73
|
|
|
event(new FolderWasRenamed($old_path, $new_path)); |
74
|
|
|
} else { |
75
|
|
|
event(new FileWasRenamed($old_path, $new_path)); |
76
|
|
|
event(new ImageWasRenamed($old_path, $new_path)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return parent::$success_response; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|