Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

AdminFileManagerController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A postCreateDirectory() 0 7 2
A getDeleteFile() 0 7 2
A postUpload() 0 17 4
A getDeleteDirectory() 0 6 1
A cbInit() 0 2 1
A getIndex() 0 15 6
1
<?php
2
3
namespace Crocodicstudio\Crudbooster\Modules\FileManagerModule;
4
5
use Crocodicstudio\Crudbooster\controllers\CBController;
6
use Crocodicstudio\Crudbooster\Modules\ModuleGenerator\ControllerGenerator\FieldDetector;
7
use Illuminate\Support\Facades\Storage;
8
use Illuminate\Support\Facades\Request;
9
10
class AdminFileManagerController extends CBController
11
{
12
    public function cbInit()
13
    {
14
    }
15
16
    public function getIndex()
17
    {
18
        $path = request('path') ? base64_decode(request('path')) : '';
0 ignored issues
show
Bug introduced by
It seems like request('path') can also be of type array; however, parameter $data of base64_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

18
        $path = request('path') ? base64_decode(/** @scrutinizer ignore-type */ request('path')) : '';
Loading history...
19
20
        if (strpos($path, '..') || $path == '.' || strpos($path, '/.')) {
21
            return redirect()->route('AdminFileManagerControllerGetIndex');
22
        }
23
24
        $currentPath = $path ?: 'uploads';
25
        $currentPath = trim($currentPath, '/');
26
27
        $directories = Storage::directories($currentPath);
28
        $files = Storage::files($currentPath);
29
30
        return view('CbFileManager::index', ['files' => $files, 'directories' => $directories, 'currentPath' => $currentPath]);
31
    }
32
33
    public function postCreateDirectory()
34
    {
35
        $path = (base64_decode(request('path'))) ?: 'uploads';
0 ignored issues
show
Bug introduced by
It seems like request('path') can also be of type array; however, parameter $data of base64_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

35
        $path = (base64_decode(/** @scrutinizer ignore-type */ request('path'))) ?: 'uploads';
Loading history...
36
        $name = str_slug(request('name'), '_');
0 ignored issues
show
Bug introduced by
It seems like request('name') can also be of type array; however, parameter $title of str_slug() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

36
        $name = str_slug(/** @scrutinizer ignore-type */ request('name'), '_');
Loading history...
37
        Storage::makeDirectory($path.'/'.$name);
38
39
        backWithMsg('The directory has been created!');
40
    }
41
42
    public function postUpload()
43
    {
44
        $path = request('path') ? base64_decode(request('path')) : 'uploads';
0 ignored issues
show
Bug introduced by
It seems like request('path') can also be of type array; however, parameter $data of base64_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

44
        $path = request('path') ? base64_decode(/** @scrutinizer ignore-type */ request('path')) : 'uploads';
Loading history...
45
        $file = Request::file('userfile');
46
        if (! $file) {
47
            return null;
48
        }
49
50
        $fileName = $file->getClientOriginalName();
51
52
        if (! FieldDetector::isUploadField($file->getClientOriginalExtension())) {
53
            backWithMsg('The file '.$fileName.' type is not allowed!', 'warning');
54
        }
55
56
        Storage::putFileAs($path, $file, $fileName);
57
58
        backWithMsg('The file '.$fileName.' has been uploaded!');
59
    }
60
61
    public function getDeleteDirectory($dir)
62
    {
63
        $dir = base64_decode($dir);
64
        Storage::deleteDirectory($dir);
65
66
        backWithMsg('The directory has been deleted!');
67
    }
68
69
    public function getDeleteFile($file)
70
    {
71
        $file = base64_decode($file);
72
        if(Storage::delete($file))
73
            backWithMsg('The file has been deleted!');
74
75
        backWithMsg('The file did not deleted!', 'warning');
76
    }
77
}
78