Test Setup Failed
Push — master ( bb7426...5928d1 )
by Stream
59s queued 11s
created

src/Lfm.php (1 issue)

Severity
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 UniSharp\LaravelFilemanager\Middlewares\CreateDefaultFolder;
9
use UniSharp\LaravelFilemanager\Middlewares\MultiUser;
10
11
class Lfm
12
{
13
    const PACKAGE_NAME = 'laravel-filemanager';
14
    const DS = '/';
15
16
    protected $config;
17
    protected $request;
18
19
    public function __construct(Config $config = null, Request $request = null)
20
    {
21
        $this->config = $config;
22
        $this->request = $request;
23
    }
24
25
    public function getStorage($storage_path)
26
    {
27
        return new LfmStorageRepository($storage_path, $this);
28
    }
29
30
    public function input($key)
31
    {
32
        return $this->translateFromUtf8($this->request->input($key));
33
    }
34
35
    public function config($key)
36
    {
37
        return $this->config->get('lfm.' . $key);
38
    }
39
40
    /**
41
     * Get only the file name.
42
     *
43
     * @param  string  $path  Real path of a file.
44
     * @return string
45
     */
46
    public function getNameFromPath($path)
47
    {
48
        return pathinfo($path, PATHINFO_BASENAME);
49
    }
50
51
    public function allowFolderType($type)
52
    {
53
        if ($type == 'user') {
54
            return $this->allowMultiUser();
55
        } else {
56
            return $this->allowShareFolder();
57
        }
58
    }
59
60
    public function getCategoryName()
61
    {
62
        $type = $this->currentLfmType();
63
64
        return $this->config->get('lfm.folder_categories.' . $type . '.folder_name', 'files');
65
    }
66
67
    /**
68
     * Get current lfm type.
69
     *
70
     * @return string
71
     */
72
    public function currentLfmType()
73
    {
74
        $lfm_type = 'file';
75
76
        $request_type = lcfirst(str_singular($this->input('type') ?: ''));
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

76
        $request_type = lcfirst(/** @scrutinizer ignore-deprecated */ str_singular($this->input('type') ?: ''));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
77
        $available_types = array_keys($this->config->get('lfm.folder_categories') ?: []);
78
79
        if (in_array($request_type, $available_types)) {
80
            $lfm_type = $request_type;
81
        }
82
83
        return $lfm_type;
84
    }
85
86
    public function getDisplayMode()
87
    {
88
        $type_key = $this->currentLfmType();
89
        $startup_view = $this->config->get('lfm.folder_categories.' . $type_key . '.startup_view');
90
91
        $view_type = 'grid';
92
        $target_display_type = $this->input('show_list') ?: $startup_view;
93
94
        if (in_array($target_display_type, ['list', 'grid'])) {
95
            $view_type = $target_display_type;
96
        }
97
98
        return $view_type;
99
    }
100
101
    public function getUserSlug()
102
    {
103
        $config = $this->config->get('lfm.user_folder_name');
104
105
        if (is_callable($config)) {
106
            return call_user_func($config);
107
        }
108
109
        if (class_exists($config)) {
110
            return app()->make($config)->userField();
111
        }
112
113
        return empty(auth()->user()) ? '' : auth()->user()->$config;
114
    }
115
116
    public function getRootFolder($type = null)
117
    {
118
        if (is_null($type)) {
119
            $type = 'share';
120
            if ($this->allowFolderType('user')) {
121
                $type = 'user';
122
            }
123
        }
124
125
        if ($type === 'user') {
126
            $folder = $this->getUserSlug();
127
        } else {
128
            $folder = $this->config->get('lfm.shared_folder_name');
129
        }
130
131
        // the slash is for url, dont replace it with directory seperator
132
        return '/' . $folder;
133
    }
134
135
    public function getThumbFolderName()
136
    {
137
        return $this->config->get('lfm.thumb_folder_name');
138
    }
139
140
    public function getFileIcon($ext)
141
    {
142
        return $this->config->get("lfm.file_icon_array.{$ext}", 'fa-file-o');
143
    }
144
145
    public function getFileType($ext)
146
    {
147
        return $this->config->get("lfm.file_type_array.{$ext}", 'File');
148
    }
149
150
    public function availableMimeTypes()
151
    {
152
        return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.valid_mime');
153
    }
154
155
    public function maxUploadSize()
156
    {
157
        return $this->config->get('lfm.folder_categories.' . $this->currentLfmType() . '.max_size');
158
    }
159
160
    /**
161
     * Check if users are allowed to use their private folders.
162
     *
163
     * @return bool
164
     */
165
    public function allowMultiUser()
166
    {
167
        return $this->config->get('lfm.allow_multi_user') === true;
168
    }
169
170
    /**
171
     * Check if users are allowed to use the shared folder.
172
     * This can be disabled only when allowMultiUser() is true.
173
     *
174
     * @return bool
175
     */
176
    public function allowShareFolder()
177
    {
178
        if (! $this->allowMultiUser()) {
179
            return true;
180
        }
181
182
        return $this->config->get('lfm.allow_share_folder') === true;
183
    }
184
185
    /**
186
     * Translate file name to make it compatible on Windows.
187
     *
188
     * @param  string  $input  Any string.
189
     * @return string
190
     */
191
    public function translateFromUtf8($input)
192
    {
193
        if ($this->isRunningOnWindows()) {
194
            $input = iconv('UTF-8', mb_detect_encoding($input), $input);
195
        }
196
197
        return $input;
198
    }
199
200
    /**
201
     * Get directory seperator of current operating system.
202
     *
203
     * @return string
204
     */
205
    public function ds()
206
    {
207
        $ds = Lfm::DS;
208
        if ($this->isRunningOnWindows()) {
209
            $ds = '\\';
210
        }
211
212
        return $ds;
213
    }
214
215
    /**
216
     * Check current operating system is Windows or not.
217
     *
218
     * @return bool
219
     */
220
    public function isRunningOnWindows()
221
    {
222
        return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
223
    }
224
225
    /**
226
     * Shorter function of getting localized error message..
227
     *
228
     * @param  mixed  $error_type  Key of message in lang file.
229
     * @param  mixed  $variables   Variables the message needs.
230
     * @return string
231
     */
232
    public function error($error_type, $variables = [])
233
    {
234
        throw new \Exception(trans(self::PACKAGE_NAME . '::lfm.error-' . $error_type, $variables));
235
    }
236
237
    /**
238
     * Generates routes of this package.
239
     *
240
     * @return void
241
     */
242
    public static function routes()
243
    {
244
        $middleware = [ CreateDefaultFolder::class, MultiUser::class ];
245
        $as = 'unisharp.lfm.';
246
        $namespace = '\\UniSharp\\LaravelFilemanager\\Controllers\\';
247
248
        Route::group(compact('middleware', 'as', 'namespace'), function () {
249
250
            // display main layout
251
            Route::get('/', [
252
                'uses' => 'LfmController@show',
253
                'as' => 'show',
254
            ]);
255
256
            // display integration error messages
257
            Route::get('/errors', [
258
                'uses' => 'LfmController@getErrors',
259
                'as' => 'getErrors',
260
            ]);
261
262
            // upload
263
            Route::any('/upload', [
264
                'uses' => 'UploadController@upload',
265
                'as' => 'upload',
266
            ]);
267
268
            // list images & files
269
            Route::get('/jsonitems', [
270
                'uses' => 'ItemsController@getItems',
271
                'as' => 'getItems',
272
            ]);
273
274
            Route::get('/move', [
275
                'uses' => 'ItemsController@move',
276
                'as' => 'move',
277
            ]);
278
279
            Route::get('/domove', [
280
                'uses' => 'ItemsController@domove',
281
                'as' => 'domove'
282
            ]);
283
284
            // folders
285
            Route::get('/newfolder', [
286
                'uses' => 'FolderController@getAddfolder',
287
                'as' => 'getAddfolder',
288
            ]);
289
290
            // list folders
291
            Route::get('/folders', [
292
                'uses' => 'FolderController@getFolders',
293
                'as' => 'getFolders',
294
            ]);
295
296
            // crop
297
            Route::get('/crop', [
298
                'uses' => 'CropController@getCrop',
299
                'as' => 'getCrop',
300
            ]);
301
            Route::get('/cropimage', [
302
                'uses' => 'CropController@getCropimage',
303
                'as' => 'getCropimage',
304
            ]);
305
            Route::get('/cropnewimage', [
306
                'uses' => 'CropController@getNewCropimage',
307
                'as' => 'getCropimage',
308
            ]);
309
310
            // rename
311
            Route::get('/rename', [
312
                'uses' => 'RenameController@getRename',
313
                'as' => 'getRename',
314
            ]);
315
316
            // scale/resize
317
            Route::get('/resize', [
318
                'uses' => 'ResizeController@getResize',
319
                'as' => 'getResize',
320
            ]);
321
            Route::get('/doresize', [
322
                'uses' => 'ResizeController@performResize',
323
                'as' => 'performResize',
324
            ]);
325
326
            // download
327
            Route::get('/download', [
328
                'uses' => 'DownloadController@getDownload',
329
                'as' => 'getDownload',
330
            ]);
331
332
            // delete
333
            Route::get('/delete', [
334
                'uses' => 'DeleteController@getDelete',
335
                'as' => 'getDelete',
336
            ]);
337
338
            Route::get('/demo', 'DemoController@index');
339
        });
340
    }
341
}
342