Test Setup Failed
Pull Request — master (#857)
by
unknown
12:21
created

Lfm::ds()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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

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