1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Elfinder; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use ArrayObject; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Filesystem\Filesystem; |
10
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
11
|
|
|
|
12
|
|
|
class Options extends ArrayObject |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* $request. |
16
|
|
|
* |
17
|
|
|
* @var \Illuminate\Http\Request |
18
|
|
|
*/ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* $files. |
23
|
|
|
* |
24
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
25
|
|
|
*/ |
26
|
|
|
protected $files; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* $request. |
30
|
|
|
* |
31
|
|
|
* @var \Illuminate\Contracts\Routing\UrlGenerator |
32
|
|
|
*/ |
33
|
|
|
protected $urlGenerator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* $config. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $config; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* $options. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $options; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* __construct. |
51
|
|
|
* |
52
|
|
|
* @param \Illuminate\Http\Request $request |
53
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
54
|
|
|
* @param \Illuminate\Contracts\Routing\UrlGenerator $urlGenerator |
55
|
|
|
* @param \Recca0120\Elfinder\LaravelSession $session |
56
|
|
|
* @param array $config |
57
|
|
|
*/ |
58
|
2 |
|
public function __construct(Request $request, Filesystem $files, UrlGenerator $urlGenerator, LaravelSession $session, $config = []) |
|
|
|
|
59
|
|
|
{ |
60
|
2 |
|
$this->request = $request; |
61
|
2 |
|
$this->files = $files; |
62
|
2 |
|
$this->urlGenerator = $urlGenerator; |
63
|
2 |
|
$this->config = $config; |
64
|
2 |
|
$this->options = Arr::get($this->config, 'options', []); |
|
|
|
|
65
|
|
|
|
66
|
2 |
|
parent::__construct(array_merge($this->options, [ |
67
|
2 |
|
'roots' => $this->getRoots(), |
68
|
2 |
|
'session' => $session, |
69
|
2 |
|
])); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* getRoots. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
2 |
|
protected function getRoots() |
78
|
|
|
{ |
79
|
2 |
|
$accessControl = Arr::get($this->config, 'accessControl'); |
80
|
2 |
|
$roots = Arr::get($this->options, 'roots', []); |
81
|
2 |
|
$user = $this->request->user(); |
82
|
|
|
|
83
|
2 |
|
return array_filter(array_map(function ($disk) use ($user, $accessControl) { |
84
|
1 |
|
$disk['driver'] = empty($disk['driver']) === true ? 'LocalFileSystem' : $disk['driver']; |
85
|
1 |
|
$disk['autoload'] = true; |
86
|
|
|
|
87
|
1 |
|
if (empty($disk['path']) === false && ($disk['path'] instanceof Closure) === true) { |
88
|
|
|
$disk['path'] = call_user_func($disk['path']); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return call_user_func_array([$this, 'create'.$disk['driver']], [$disk, $user, $accessControl]); |
92
|
2 |
|
}, $roots)); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
protected function createLocalFileSystem($disk, $user, $accessControl = null) |
96
|
|
|
{ |
97
|
1 |
|
if (strpos($disk['path'], '{user_id}') !== -1) { |
98
|
1 |
|
if (is_null($user) === true) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$userId = $user->id; |
103
|
1 |
|
$disk['path'] = str_replace('{user_id}', $userId, $disk['path']); |
104
|
1 |
|
$disk['URL'] = str_replace('{user_id}', $userId, $disk['URL']); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
if ($this->files->exists($disk['path']) === false) { |
108
|
1 |
|
$this->files->makeDirectory($disk['path'], 0755, true); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
$disk['URL'] = $this->urlGenerator->to($disk['URL']); |
112
|
|
|
|
113
|
1 |
|
return array_merge([ |
114
|
1 |
|
'mimeDetect' => 'internal', |
115
|
1 |
|
'utf8fix' => true, |
116
|
1 |
|
'tmbCrop' => false, |
117
|
1 |
|
'tmbBgColor' => 'transparent', |
118
|
1 |
|
'accessControl' => $accessControl, |
119
|
1 |
|
], $disk); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|