Completed
Push — master ( cf309a...0d017d )
by Stream
09:43
created

Lfm   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 323
Duplicated Lines 0 %

Importance

Changes 6
Bugs 4 Features 1
Metric Value
wmc 37
eloc 115
c 6
b 4
f 1
dl 0
loc 323
rs 9.44

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getDisplayMode() 0 13 3
A getStorage() 0 3 1
A allowFolderType() 0 6 2
A getCategoryName() 0 5 1
A getNameFromPath() 0 3 1
A getThumbFolderName() 0 3 1
A currentLfmType() 0 12 4
A getUserSlug() 0 13 4
A config() 0 3 1
A __construct() 0 4 1
A input() 0 3 1
A getRootFolder() 0 17 4
A routes() 0 97 1
A availableMimeTypes() 0 3 1
A ds() 0 8 2
A getFileType() 0 3 1
A maxUploadSize() 0 3 1
A error() 0 3 1
A translateFromUtf8() 0 7 2
A allowMultiUser() 0 3 1
A allowShareFolder() 0 7 2
A isRunningOnWindows() 0 3 1
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
Bug introduced by
The method input() 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 ignore-call  annotation

33
        return $this->translateFromUtf8($this->request->/** @scrutinizer ignore-call */ input($key));

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...
34
    }
35
36
    public function config($key)
37
    {
38
        return $this->config->get('lfm.' . $key);
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

38
        return $this->config->/** @scrutinizer ignore-call */ get('lfm.' . $key);

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 pathinfo($path, PATHINFO_BASENAME);
50
    }
51
52
    public function allowFolderType($type)
53
    {
54
        if ($type == 'user') {
55
            return $this->allowMultiUser();
56
        } else {
57
            return $this->allowShareFolder();
58
        }
59
    }
60
61
    public function getCategoryName()
62
    {
63
        $type = $this->currentLfmType();
64
65
        return $this->config->get('lfm.folder_categories.' . $type . '.folder_name', 'files');
66
    }
67
68
    /**
69
     * Get current lfm type.
70
     *
71
     * @return string
72
     */
73
    public function currentLfmType()
74
    {
75
        $lfm_type = 'file';
76
77
        $request_type = lcfirst(Str::singular($this->input('type') ?: ''));
78
        $available_types = array_keys($this->config->get('lfm.folder_categories') ?: []);
79
80
        if (in_array($request_type, $available_types)) {
81
            $lfm_type = $request_type;
82
        }
83
84
        return $lfm_type;
85
    }
86
87
    public function getDisplayMode()
88
    {
89
        $type_key = $this->currentLfmType();
90
        $startup_view = $this->config->get('lfm.folder_categories.' . $type_key . '.startup_view');
91
92
        $view_type = 'grid';
93
        $target_display_type = $this->input('show_list') ?: $startup_view;
94
95
        if (in_array($target_display_type, ['list', 'grid'])) {
96
            $view_type = $target_display_type;
97
        }
98
99
        return $view_type;
100
    }
101
102
    public function getUserSlug()
103
    {
104
        $config = $this->config->get('lfm.private_folder_name');
105
106
        if (is_callable($config)) {
107
            return call_user_func($config);
108
        }
109
110
        if (class_exists($config)) {
111
            return app()->make($config)->userField();
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

111
            return /** @scrutinizer ignore-call */ app()->make($config)->userField();
Loading history...
112
        }
113
114
        return empty(auth()->user()) ? '' : auth()->user()->$config;
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

114
        return empty(/** @scrutinizer ignore-call */ auth()->user()) ? '' : auth()->user()->$config;
Loading history...
115
    }
116
117
    public function getRootFolder($type = null)
118
    {
119
        if (is_null($type)) {
120
            $type = 'share';
121
            if ($this->allowFolderType('user')) {
122
                $type = 'user';
123
            }
124
        }
125
126
        if ($type === 'user') {
127
            $folder = $this->getUserSlug();
128
        } else {
129
            $folder = $this->config->get('lfm.shared_folder_name');
130
        }
131
132
        // the slash is for url, dont replace it with directory seperator
133
        return '/' . $folder;
134
    }
135
136
    public function getThumbFolderName()
137
    {
138
        return $this->config->get('lfm.thumb_folder_name');
139
    }
140
141
    public function getFileType($ext)
142
    {
143
        return $this->config->get("lfm.file_type_array.{$ext}", 'File');
144
    }
145
146
    public function availableMimeTypes()
147
    {
148
        return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.valid_mime');
149
    }
150
151
    public function maxUploadSize()
152
    {
153
        return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.max_size');
154
    }
155
156
    /**
157
     * Check if users are allowed to use their private folders.
158
     *
159
     * @return bool
160
     */
161
    public function allowMultiUser()
162
    {
163
        return $this->config->get('lfm.allow_private_folder') === true;
164
    }
165
166
    /**
167
     * Check if users are allowed to use the shared folder.
168
     * This can be disabled only when allowMultiUser() is true.
169
     *
170
     * @return bool
171
     */
172
    public function allowShareFolder()
173
    {
174
        if (! $this->allowMultiUser()) {
175
            return true;
176
        }
177
178
        return $this->config->get('lfm.allow_shared_folder') === true;
179
    }
180
181
    /**
182
     * Translate file name to make it compatible on Windows.
183
     *
184
     * @param  string  $input  Any string.
185
     * @return string
186
     */
187
    public function translateFromUtf8($input)
188
    {
189
        if ($this->isRunningOnWindows()) {
190
            $input = iconv('UTF-8', mb_detect_encoding($input), $input);
191
        }
192
193
        return $input;
194
    }
195
196
    /**
197
     * Get directory seperator of current operating system.
198
     *
199
     * @return string
200
     */
201
    public function ds()
202
    {
203
        $ds = Lfm::DS;
204
        if ($this->isRunningOnWindows()) {
205
            $ds = '\\';
206
        }
207
208
        return $ds;
209
    }
210
211
    /**
212
     * Check current operating system is Windows or not.
213
     *
214
     * @return bool
215
     */
216
    public function isRunningOnWindows()
217
    {
218
        return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
219
    }
220
221
    /**
222
     * Shorter function of getting localized error message..
223
     *
224
     * @param  mixed  $error_type  Key of message in lang file.
225
     * @param  mixed  $variables   Variables the message needs.
226
     * @return string
227
     */
228
    public function error($error_type, $variables = [])
229
    {
230
        throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-' . $error_type, $variables));
0 ignored issues
show
Bug introduced by
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 ignore-call  annotation

230
        throw new \Exception(/** @scrutinizer ignore-call */ trans(self::PACKAGE_NAME . '::lfm.error-' . $error_type, $variables));
Loading history...
231
    }
232
233
    /**
234
     * Generates routes of this package.
235
     *
236
     * @return void
237
     */
238
    public static function routes()
239
    {
240
        $middleware = [ CreateDefaultFolder::class, MultiUser::class ];
241
        $as = 'unisharp.lfm.';
242
        $namespace = '\\UniSharp\\LaravelFilemanager\\Controllers\\';
243
244
        Route::group(compact('middleware', 'as', 'namespace'), function () {
245
246
            // display main layout
247
            Route::get('/', [
248
                'uses' => 'LfmController@show',
249
                'as' => 'show',
250
            ]);
251
252
            // display integration error messages
253
            Route::get('/errors', [
254
                'uses' => 'LfmController@getErrors',
255
                'as' => 'getErrors',
256
            ]);
257
258
            // upload
259
            Route::any('/upload', [
260
                'uses' => 'UploadController@upload',
261
                'as' => 'upload',
262
            ]);
263
264
            // list images & files
265
            Route::get('/jsonitems', [
266
                'uses' => 'ItemsController@getItems',
267
                'as' => 'getItems',
268
            ]);
269
270
            Route::get('/move', [
271
                'uses' => 'ItemsController@move',
272
                'as' => 'move',
273
            ]);
274
275
            Route::get('/domove', [
276
                'uses' => 'ItemsController@domove',
277
                'as' => 'domove'
278
            ]);
279
280
            // folders
281
            Route::get('/newfolder', [
282
                'uses' => 'FolderController@getAddfolder',
283
                'as' => 'getAddfolder',
284
            ]);
285
286
            // list folders
287
            Route::get('/folders', [
288
                'uses' => 'FolderController@getFolders',
289
                'as' => 'getFolders',
290
            ]);
291
292
            // crop
293
            Route::get('/crop', [
294
                'uses' => 'CropController@getCrop',
295
                'as' => 'getCrop',
296
            ]);
297
            Route::get('/cropimage', [
298
                'uses' => 'CropController@getCropimage',
299
                'as' => 'getCropimage',
300
            ]);
301
            Route::get('/cropnewimage', [
302
                'uses' => 'CropController@getNewCropimage',
303
                'as' => 'getCropimage',
304
            ]);
305
306
            // rename
307
            Route::get('/rename', [
308
                'uses' => 'RenameController@getRename',
309
                'as' => 'getRename',
310
            ]);
311
312
            // scale/resize
313
            Route::get('/resize', [
314
                'uses' => 'ResizeController@getResize',
315
                'as' => 'getResize',
316
            ]);
317
            Route::get('/doresize', [
318
                'uses' => 'ResizeController@performResize',
319
                'as' => 'performResize',
320
            ]);
321
322
            // download
323
            Route::get('/download', [
324
                'uses' => 'DownloadController@getDownload',
325
                'as' => 'getDownload',
326
            ]);
327
328
            // delete
329
            Route::get('/delete', [
330
                'uses' => 'DeleteController@getDelete',
331
                'as' => 'getDelete',
332
            ]);
333
334
            Route::get('/demo', 'DemoController@index');
335
        });
336
    }
337
}
338