1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Form; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Illuminate\Support\Facades\URL; |
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
9
|
|
|
|
10
|
|
|
trait UploadField |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Upload directory. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $directory = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* File name. |
21
|
|
|
* |
22
|
|
|
* @var null |
23
|
|
|
*/ |
24
|
|
|
protected $name = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Storage instance. |
28
|
|
|
* |
29
|
|
|
* @var \Illuminate\Filesystem\Filesystem |
30
|
|
|
*/ |
31
|
|
|
protected $storage = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* If use unique name to store upload file. |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $useUniqueName = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* If use sequence name to store upload file. |
42
|
|
|
* |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
protected $useSequenceName = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $removable = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize the storage instance. |
54
|
|
|
* |
55
|
|
|
* @return void. |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
protected function initStorage() |
58
|
|
|
{ |
59
|
|
|
$this->disk(config('admin.upload.disk')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set default options form image field. |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
protected function setupDefaultOptions() |
68
|
|
|
{ |
69
|
|
|
$defaultOptions = [ |
70
|
|
|
'overwriteInitial' => false, |
71
|
|
|
'initialPreviewAsData' => true, |
72
|
|
|
'browseLabel' => trans('admin.browse'), |
73
|
|
|
'showRemove' => false, |
74
|
|
|
'showUpload' => false, |
75
|
|
|
// 'initialCaption' => $this->initialCaption($this->value), |
76
|
|
|
'deleteExtraData' => [ |
77
|
|
|
$this->formatName($this->column) => static::FILE_DELETE_FLAG, |
|
|
|
|
78
|
|
|
static::FILE_DELETE_FLAG => '', |
79
|
|
|
'_token' => csrf_token(), |
80
|
|
|
'_method' => 'PUT', |
81
|
|
|
], |
82
|
|
|
]; |
83
|
|
|
|
84
|
|
|
if ($this->form instanceof Form) { |
85
|
|
|
$defaultOptions['deleteUrl'] = $this->form->resource() . '/' . $this->form->model()->getKey(); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->options($defaultOptions); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set preview options form image field. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function setupPreviewOptions() |
97
|
|
|
{ |
98
|
|
|
if (!$this->removable) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->options([ |
103
|
|
|
//'initialPreview' => $this->preview(), |
104
|
|
|
'initialPreviewConfig' => $this->initialPreviewConfig(), |
|
|
|
|
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Allow use to remove file. |
110
|
|
|
* |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function removable() |
114
|
|
|
{ |
115
|
|
|
$this->removable = true; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set options for file-upload plugin. |
122
|
|
|
* |
123
|
|
|
* @param array $options |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
|
|
public function options($options = []) |
128
|
|
|
{ |
129
|
|
|
$this->options = array_merge($options, $this->options); |
|
|
|
|
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Set disk for storage. |
136
|
|
|
* |
137
|
|
|
* @param string $disk Disks defined in `config/filesystems.php`. |
138
|
|
|
* |
139
|
|
|
* @throws \Exception |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function disk($disk) |
144
|
|
|
{ |
145
|
|
|
try { |
146
|
|
|
$this->storage = Storage::disk($disk); |
|
|
|
|
147
|
|
|
} catch (\Exception $exception) { |
148
|
|
|
if (!array_key_exists($disk, config('filesystems.disks'))) { |
149
|
|
|
admin_error( |
150
|
|
|
'Config error.', |
151
|
|
|
"Disk [$disk] not configured, please add a disk config in `config/filesystems.php`." |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
throw $exception; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Specify the directory and name for upload file. |
165
|
|
|
* |
166
|
|
|
* @param string $directory |
167
|
|
|
* @param null|string $name |
168
|
|
|
* |
169
|
|
|
* @return $this |
170
|
|
|
*/ |
171
|
|
|
public function move($directory, $name = null) |
172
|
|
|
{ |
173
|
|
|
$this->dir($directory); |
174
|
|
|
|
175
|
|
|
$this->name($name); |
|
|
|
|
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Specify the directory upload file. |
182
|
|
|
* |
183
|
|
|
* @param string $dir |
184
|
|
|
* |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function dir($dir) |
188
|
|
|
{ |
189
|
|
|
if ($dir) { |
190
|
|
|
$this->directory = $dir; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Set name of store name. |
198
|
|
|
* |
199
|
|
|
* @param string|callable $name |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
|
|
public function name($name) |
204
|
|
|
{ |
205
|
|
|
if ($name) { |
206
|
|
|
$this->name = $name; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Use unique name for store upload file. |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
|
|
public function uniqueName() |
218
|
|
|
{ |
219
|
|
|
$this->useUniqueName = true; |
220
|
|
|
|
221
|
|
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get store name of upload file. |
226
|
|
|
* |
227
|
|
|
* @param UploadedFile $file |
228
|
|
|
* |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
protected function getStoreName(UploadedFile $file) |
232
|
|
|
{ |
233
|
|
|
if ($this->useUniqueName) { |
234
|
|
|
return $this->generateUniqueName($file); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if ($this->useSequenceName) { |
238
|
|
|
return $this->generateSequenceName($file); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if ($this->name instanceof \Closure) { |
242
|
|
|
return $this->name->call($this, $file); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
if (is_string($this->name)) { |
246
|
|
|
return $this->name; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $file->getClientOriginalName(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Get directory for store file. |
254
|
|
|
* |
255
|
|
|
* @return mixed|string |
256
|
|
|
*/ |
257
|
|
|
public function getDirectory() |
258
|
|
|
{ |
259
|
|
|
if ($this->directory instanceof \Closure) { |
260
|
|
|
return call_user_func($this->directory, $this->form); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $this->directory ?: $this->defaultDirectory(); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Upload file and delete original file. |
268
|
|
|
* |
269
|
|
|
* @param UploadedFile $file |
270
|
|
|
* |
271
|
|
|
* @return mixed |
272
|
|
|
*/ |
273
|
|
|
protected function upload(UploadedFile $file) |
274
|
|
|
{ |
275
|
|
|
$this->renameIfExists($file); |
276
|
|
|
|
277
|
|
|
return $this->storage->putFileAs($this->getDirectory(), $file, $this->name); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* If name already exists, rename it. |
282
|
|
|
* |
283
|
|
|
* @param $file |
284
|
|
|
* |
285
|
|
|
* @return void |
286
|
|
|
*/ |
287
|
|
|
public function renameIfExists(UploadedFile $file) |
288
|
|
|
{ |
289
|
|
|
if ($this->storage->exists("{$this->getDirectory()}/$this->name")) { |
290
|
|
|
$this->name = $this->generateUniqueName($file); |
|
|
|
|
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get file visit url. |
296
|
|
|
* |
297
|
|
|
* @param $path |
298
|
|
|
* |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
public function objectUrl($path) |
302
|
|
|
{ |
303
|
|
|
if (URL::isValidUrl($path)) { |
304
|
|
|
return $path; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
if ($this->storage) { |
308
|
|
|
return $this->storage->url($path); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
return Storage::disk(config('admin.upload.disk'))->url($path); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Generate a unique name for uploaded file. |
316
|
|
|
* |
317
|
|
|
* @param UploadedFile $file |
318
|
|
|
* |
319
|
|
|
* @return string |
320
|
|
|
*/ |
321
|
|
|
protected function generateUniqueName(UploadedFile $file) |
322
|
|
|
{ |
323
|
|
|
return md5(uniqid()) . '.' . $file->getClientOriginalExtension(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Generate a sequence name for uploaded file. |
328
|
|
|
* |
329
|
|
|
* @param UploadedFile $file |
330
|
|
|
* |
331
|
|
|
* @return string |
332
|
|
|
*/ |
333
|
|
|
protected function generateSequenceName(UploadedFile $file) |
334
|
|
|
{ |
335
|
|
|
$index = 1; |
336
|
|
|
$extension = $file->getClientOriginalExtension(); |
337
|
|
|
$originalName = $file->getClientOriginalName(); |
338
|
|
|
$newName = $originalName . '_' . $index . '.' . $extension; |
339
|
|
|
|
340
|
|
|
while ($this->storage->exists("{$this->getDirectory()}/$newName")) { |
341
|
|
|
++$index; |
342
|
|
|
$newName = $originalName . '_' . $index . '.' . $extension; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
return $newName; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Destroy original files. |
350
|
|
|
* |
351
|
|
|
* @return void. |
|
|
|
|
352
|
|
|
*/ |
353
|
|
|
public function destroy() |
354
|
|
|
{ |
355
|
|
|
if ($this->storage->exists($this->original)) { |
356
|
|
|
$this->storage->delete($this->original); |
|
|
|
|
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: