Test Failed
Push — master ( 241100...2372f3 )
by Stream
08:09 queued 10s
created

Lfm::allowFolderType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
rs 10
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
    public function getPaginationPerPage()
157
    {
158
        return $this->config->get("lfm.paginator.perPage", 30);
159
    }
160
161
    /**
162
     * Check if users are allowed to use their private folders.
163
     *
164
     * @return bool
165
     */
166
    public function allowMultiUser()
167
    {
168
        return $this->config->get('lfm.allow_private_folder') === true;
169
    }
170
171
    /**
172
     * Check if users are allowed to use the shared folder.
173
     * This can be disabled only when allowMultiUser() is true.
174
     *
175
     * @return bool
176
     */
177
    public function allowShareFolder()
178
    {
179
        if (! $this->allowMultiUser()) {
180
            return true;
181
        }
182
183
        return $this->config->get('lfm.allow_shared_folder') === true;
184
    }
185
186
    /**
187
     * Translate file name to make it compatible on Windows.
188
     *
189
     * @param  string  $input  Any string.
190
     * @return string
191
     */
192
    public function translateFromUtf8($input)
193
    {
194
        if ($this->isRunningOnWindows()) {
195
            $input = iconv('UTF-8', mb_detect_encoding($input), $input);
196
        }
197
198
        return $input;
199
    }
200
201
    /**
202
     * Get directory seperator of current operating system.
203
     *
204
     * @return string
205
     */
206
    public function ds()
207
    {
208
        $ds = Lfm::DS;
209
        if ($this->isRunningOnWindows()) {
210
            $ds = '\\';
211
        }
212
213
        return $ds;
214
    }
215
216
    /**
217
     * Check current operating system is Windows or not.
218
     *
219
     * @return bool
220
     */
221
    public function isRunningOnWindows()
222
    {
223
        return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
224
    }
225
226
    /**
227
     * Shorter function of getting localized error message..
228
     *
229
     * @param  mixed  $error_type  Key of message in lang file.
230
     * @param  mixed  $variables   Variables the message needs.
231
     * @return string
232
     */
233
    public function error($error_type, $variables = [])
234
    {
235
        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

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