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