forked from UniSharp/laravel-filemanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLfm.php
386 lines (322 loc) · 10.4 KB
/
Lfm.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
namespace UniSharp\LaravelFilemanager;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use UniSharp\LaravelFilemanager\Middlewares\CreateDefaultFolder;
use UniSharp\LaravelFilemanager\Middlewares\MultiUser;
class Lfm
{
const PACKAGE_NAME = 'laravel-filemanager';
const DS = '/';
protected $config;
protected $request;
public function __construct(Config $config = null, Request $request = null)
{
$this->config = $config;
$this->request = $request;
}
public function getStorage($storage_path)
{
return new LfmStorageRepository($storage_path, $this);
}
public function input($key)
{
return $this->translateFromUtf8($this->request->input($key));
}
public function config($key)
{
return $this->config->get('lfm.' . $key);
}
/**
* Get only the file name.
*
* @param string $path Real path of a file.
* @return string
*/
public function getNameFromPath($path)
{
return $this->utf8Pathinfo($path, 'basename');
}
public function utf8Pathinfo($path, $part_name)
{
// XXX: all locale work-around for issue: utf8 file name got emptified
// if there's no '/', we're probably dealing with just a filename
// so just put an 'a' in front of it
if (strpos($path, '/') === false) {
$path_parts = pathinfo('a' . $path);
} else {
$path = str_replace('/', '/a', $path);
$path_parts = pathinfo($path);
}
return substr($path_parts[$part_name], 1);
}
public function allowFolderType($type)
{
if ($type == 'user') {
return $this->allowMultiUser();
} else {
return $this->allowShareFolder();
}
}
public function getCategoryName()
{
$type = $this->currentLfmType();
return $this->config->get('lfm.folder_categories.' . $type . '.folder_name', 'files');
}
/**
* Get current lfm type.
*
* @return string
*/
public function currentLfmType()
{
$lfm_type = 'file';
$request_type = lcfirst(Str::singular($this->input('type') ?: ''));
$available_types = array_keys($this->config->get('lfm.folder_categories') ?: []);
if (in_array($request_type, $available_types)) {
$lfm_type = $request_type;
}
return $lfm_type;
}
public function getDisplayMode()
{
$type_key = $this->currentLfmType();
$startup_view = $this->config->get('lfm.folder_categories.' . $type_key . '.startup_view');
$view_type = 'grid';
$target_display_type = $this->input('show_list') ?: $startup_view;
if (in_array($target_display_type, ['list', 'grid'])) {
$view_type = $target_display_type;
}
return $view_type;
}
public function getUserSlug()
{
$config = $this->config->get('lfm.private_folder_name');
if (is_callable($config)) {
return call_user_func($config);
}
if (class_exists($config)) {
return app()->make($config)->userField();
}
return empty(auth()->user()) ? '' : auth()->user()->$config;
}
public function getRootFolder($type = null)
{
if (is_null($type)) {
$type = 'share';
if ($this->allowFolderType('user')) {
$type = 'user';
}
}
if ($type === 'user') {
$folder = $this->getUserSlug();
} else {
$folder = $this->config->get('lfm.shared_folder_name');
}
// the slash is for url, dont replace it with directory seperator
return '/' . $folder;
}
public function getThumbFolderName()
{
return $this->config->get('lfm.thumb_folder_name');
}
public function getFileType($ext)
{
return $this->config->get("lfm.file_type_array.{$ext}", 'File');
}
public function availableMimeTypes()
{
return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.valid_mime');
}
public function shouldCreateCategoryThumb()
{
return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.thumb');
}
public function categoryThumbWidth()
{
return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.thumb_width');
}
public function categoryThumbHeight()
{
return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.thumb_height');
}
public function maxUploadSize()
{
return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.max_size');
}
public function getPaginationPerPage()
{
return $this->config->get("lfm.paginator.perPage", 30);
}
/**
* Check if users are allowed to use their private folders.
*
* @return bool
*/
public function allowMultiUser()
{
$type_key = $this->currentLfmType();
if ($this->config->has('lfm.folder_categories.' . $type_key . '.allow_private_folder')) {
return $this->config->get('lfm.folder_categories.' . $type_key . '.allow_private_folder') === true;
}
return $this->config->get('lfm.allow_private_folder') === true;
}
/**
* Check if users are allowed to use the shared folder.
* This can be disabled only when allowMultiUser() is true.
*
* @return bool
*/
public function allowShareFolder()
{
if (! $this->allowMultiUser()) {
return true;
}
$type_key = $this->currentLfmType();
if ($this->config->has('lfm.folder_categories.' . $type_key . '.allow_shared_folder')) {
return $this->config->get('lfm.folder_categories.' . $type_key . '.allow_shared_folder') === true;
}
return $this->config->get('lfm.allow_shared_folder') === true;
}
/**
* Translate file name to make it compatible on Windows.
*
* @param string $input Any string.
* @return string
*/
public function translateFromUtf8($input)
{
if ($this->isRunningOnWindows()) {
$input = iconv('UTF-8', mb_detect_encoding($input), $input);
}
return $input;
}
/**
* Get directory seperator of current operating system.
*
* @return string
*/
public function ds()
{
$ds = Lfm::DS;
if ($this->isRunningOnWindows()) {
$ds = '\\';
}
return $ds;
}
/**
* Check current operating system is Windows or not.
*
* @return bool
*/
public function isRunningOnWindows()
{
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
/**
* Shorter function of getting localized error message..
*
* @param mixed $error_type Key of message in lang file.
* @param mixed $variables Variables the message needs.
* @return string
*/
public function error($error_type, $variables = [])
{
throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-' . $error_type, $variables));
}
/**
* Generates routes of this package.
*
* @return void
*/
public static function routes()
{
$middleware = [ CreateDefaultFolder::class, MultiUser::class ];
$as = 'unisharp.lfm.';
$namespace = '\\UniSharp\\LaravelFilemanager\\Controllers\\';
Route::group(compact('middleware', 'as', 'namespace'), function () {
// display main layout
Route::get('/', [
'uses' => 'LfmController@show',
'as' => 'show',
]);
// display integration error messages
Route::get('/errors', [
'uses' => 'LfmController@getErrors',
'as' => 'getErrors',
]);
// upload
Route::any('/upload', [
'uses' => 'UploadController@upload',
'as' => 'upload',
]);
// list images & files
Route::get('/jsonitems', [
'uses' => 'ItemsController@getItems',
'as' => 'getItems',
]);
Route::get('/move', [
'uses' => 'ItemsController@move',
'as' => 'move',
]);
Route::get('/domove', [
'uses' => 'ItemsController@doMove',
'as' => 'doMove'
]);
// folders
Route::get('/newfolder', [
'uses' => 'FolderController@getAddfolder',
'as' => 'getAddfolder',
]);
// list folders
Route::get('/folders', [
'uses' => 'FolderController@getFolders',
'as' => 'getFolders',
]);
// crop
Route::get('/crop', [
'uses' => 'CropController@getCrop',
'as' => 'getCrop',
]);
Route::get('/cropimage', [
'uses' => 'CropController@getCropImage',
'as' => 'getCropImage',
]);
Route::get('/cropnewimage', [
'uses' => 'CropController@getNewCropImage',
'as' => 'getNewCropImage',
]);
// rename
Route::get('/rename', [
'uses' => 'RenameController@getRename',
'as' => 'getRename',
]);
// scale/resize
Route::get('/resize', [
'uses' => 'ResizeController@getResize',
'as' => 'getResize',
]);
Route::get('/doresize', [
'uses' => 'ResizeController@performResize',
'as' => 'performResize',
]);
Route::get('/doresizenew', [
'uses' => 'ResizeController@performResizeNew',
'as' => 'performResizeNew',
]);
// download
Route::get('/download', [
'uses' => 'DownloadController@getDownload',
'as' => 'getDownload',
]);
// delete
Route::get('/delete', [
'uses' => 'DeleteController@getDelete',
'as' => 'getDelete',
]);
Route::get('/demo', 'DemoController@index');
});
}
}