1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace UniSharp\LaravelFilemanager\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Storage; |
6
|
|
|
use UniSharp\LaravelFilemanager\Events\FileIsMoving; |
7
|
|
|
use UniSharp\LaravelFilemanager\Events\FileWasMoving; |
8
|
|
|
use UniSharp\LaravelFilemanager\Events\FolderIsMoving; |
9
|
|
|
use UniSharp\LaravelFilemanager\Events\FolderWasMoving; |
10
|
|
|
|
11
|
|
|
class ItemsController extends LfmController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return mixed |
15
|
|
|
*/ |
16
|
|
|
private static function getKeywordFromRequest() |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
return request()->get('keyword', ""); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return int |
24
|
|
|
*/ |
25
|
|
|
private static function getCurrentPageFromRequest(): int |
26
|
|
|
{ |
27
|
|
|
$currentPage = (int) request()->get('page', 1); |
|
|
|
|
28
|
|
|
|
29
|
|
|
return max($currentPage, 1); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the images to load for a selected folder. |
35
|
|
|
* |
36
|
|
|
* @return mixed |
37
|
|
|
*/ |
38
|
|
|
public function getItems() |
39
|
|
|
{ |
40
|
|
|
$currentPage = self::getCurrentPageFromRequest(); |
41
|
|
|
|
42
|
|
|
$perPage = $this->helper->getPaginationPerPage(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
if (config('lfm.default_sort') == 'time') { |
|
|
|
|
45
|
|
|
$items = array_merge($this->lfm->folders(), array_reverse($this->lfm->files())); |
|
|
|
|
46
|
|
|
} else { |
47
|
|
|
$items = array_merge($this->lfm->folders(), $this->lfm->files()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$items = array_map(function ($item) { |
51
|
|
|
return $item->fill()->attributes; |
52
|
|
|
}, $items); |
53
|
|
|
|
54
|
|
|
$keyword = request()->get('keyword', ""); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (!empty($keyword)) { |
57
|
|
|
$items = array_values(array_filter($items, function ($item) use ($keyword) { |
58
|
|
|
if ($this->like_match("%" . $keyword . "%", $item['name'])) { |
59
|
|
|
return TRUE; |
60
|
|
|
} else { |
61
|
|
|
return FALSE; |
62
|
|
|
} |
63
|
|
|
})); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return [ |
67
|
|
|
'items' => array_slice($items, ($currentPage - 1) * $perPage, $perPage), |
68
|
|
|
'paginator' => [ |
69
|
|
|
'current_page' => $currentPage, |
70
|
|
|
'total' => count($items), |
71
|
|
|
'per_page' => $perPage, |
72
|
|
|
], |
73
|
|
|
'display' => $this->helper->getDisplayMode(), |
74
|
|
|
'working_dir' => $this->lfm->path('working_dir'), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
public function like_match($pattern, $subject) |
80
|
|
|
{ |
81
|
|
|
$pattern = str_replace('%', '.*', preg_quote($pattern, '/')); |
82
|
|
|
|
83
|
|
|
return (bool) preg_match("/^{$pattern}$/i", $subject); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function move() |
88
|
|
|
{ |
89
|
|
|
$items = request('items'); |
|
|
|
|
90
|
|
|
$folder_types = array_filter(['user', 'share'], function ($type) { |
91
|
|
|
return $this->helper->allowFolderType($type); |
|
|
|
|
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
return view('laravel-filemanager::move') |
|
|
|
|
95
|
|
|
->with([ |
96
|
|
|
'root_folders' => array_map(function ($type) use ($folder_types) { |
97
|
|
|
$path = $this->lfm->dir($this->helper->getRootFolder($type)); |
|
|
|
|
98
|
|
|
|
99
|
|
|
return (object) [ |
100
|
|
|
'name' => trans('laravel-filemanager::lfm.title-' . $type), |
|
|
|
|
101
|
|
|
'url' => $path->path('working_dir'), |
102
|
|
|
'children' => $path->folders(), |
103
|
|
|
'has_next' => !($type == end($folder_types)), |
104
|
|
|
]; |
105
|
|
|
}, $folder_types), |
106
|
|
|
]) |
107
|
|
|
->with('items', $items); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
public function domove() |
112
|
|
|
{ |
113
|
|
|
$target = $this->helper->input('goToFolder'); |
|
|
|
|
114
|
|
|
$items = $this->helper->input('items'); |
115
|
|
|
|
116
|
|
|
foreach ($items as $item) { |
117
|
|
|
$old_file = $this->lfm->pretty($item); |
|
|
|
|
118
|
|
|
$is_directory = $old_file->isDirectory(); |
119
|
|
|
|
120
|
|
|
$file = $this->lfm->setName($item); |
121
|
|
|
|
122
|
|
|
if (!Storage::disk($this->helper->config('disk'))->exists($file->path('storage'))) { |
123
|
|
|
abort(404); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$old_path = $old_file->path(); |
127
|
|
|
|
128
|
|
|
if ($old_file->hasThumb()) { |
129
|
|
|
$new_file = $this->lfm->setName($item)->thumb()->dir($target); |
130
|
|
|
if ($is_directory) { |
131
|
|
|
event(new FolderIsMoving($old_file->path(), $new_file->path())); |
|
|
|
|
132
|
|
|
} else { |
133
|
|
|
event(new FileIsMoving($old_file->path(), $new_file->path())); |
134
|
|
|
} |
135
|
|
|
$this->lfm->setName($item)->thumb()->move($new_file); |
136
|
|
|
} |
137
|
|
|
$new_file = $this->lfm->setName($item)->dir($target); |
138
|
|
|
$this->lfm->setName($item)->move($new_file); |
139
|
|
|
if ($is_directory) { |
140
|
|
|
event(new FolderWasMoving($old_path, $new_file->path())); |
141
|
|
|
} else { |
142
|
|
|
event(new FileWasMoving($old_path, $new_file->path())); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return parent::$success_response; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.