|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Uploaders; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Library\Uploaders\Support\Interfaces\UploaderInterface; |
|
6
|
|
|
use Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleFileNaming; |
|
7
|
|
|
use Backpack\CRUD\app\Library\Uploaders\Support\Traits\HandleRepeatableUploads; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
10
|
|
|
use Illuminate\Support\Facades\Storage; |
|
11
|
|
|
use Illuminate\Support\Str; |
|
12
|
|
|
|
|
13
|
|
|
abstract class Uploader implements UploaderInterface |
|
14
|
|
|
{ |
|
15
|
|
|
use HandleFileNaming; |
|
16
|
|
|
use HandleRepeatableUploads; |
|
17
|
|
|
|
|
18
|
|
|
private string $name; |
|
19
|
|
|
|
|
20
|
|
|
private string $disk = 'public'; |
|
21
|
|
|
|
|
22
|
|
|
private string $path = ''; |
|
23
|
|
|
|
|
24
|
|
|
private bool $handleMultipleFiles = false; |
|
25
|
|
|
|
|
26
|
|
|
private bool $deleteWhenEntryIsDeleted = true; |
|
27
|
|
|
|
|
28
|
|
|
private bool|string $attachedToFakeField = false; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Cloud disks have the ability to generate temporary URLs to files, should we do it? |
|
32
|
|
|
*/ |
|
33
|
|
|
private bool $useTemporaryUrl = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* When using temporary urls, define the time that the url will be valid. |
|
37
|
|
|
*/ |
|
38
|
|
|
private int $temporaryUrlExpirationTimeInMinutes = 1; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Indicates if the upload is relative to a relationship field/column. |
|
42
|
|
|
*/ |
|
43
|
|
|
private bool $isRelationship = false; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct(array $crudObject, array $configuration) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->name = $crudObject['name']; |
|
48
|
|
|
$this->disk = $configuration['disk'] ?? $crudObject['disk'] ?? $this->disk; |
|
49
|
|
|
$this->path = $this->getPathFromConfiguration($crudObject, $configuration); |
|
50
|
|
|
$this->attachedToFakeField = isset($crudObject['fake']) && $crudObject['fake'] ? ($crudObject['store_in'] ?? 'extras') : ($crudObject['store_in'] ?? false); |
|
51
|
|
|
$this->useTemporaryUrl = $configuration['temporaryUrl'] ?? $this->useTemporaryUrl; |
|
52
|
|
|
$this->temporaryUrlExpirationTimeInMinutes = $configuration['temporaryUrlExpirationTime'] ?? $this->temporaryUrlExpirationTimeInMinutes; |
|
53
|
|
|
$this->deleteWhenEntryIsDeleted = $configuration['deleteWhenEntryIsDeleted'] ?? $this->deleteWhenEntryIsDeleted; |
|
54
|
|
|
$this->fileNamer = is_callable($configuration['fileNamer'] ?? null) ? $configuration['fileNamer'] : $this->getFileNameGeneratorInstance($configuration['fileNamer'] ?? null); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/******************************* |
|
58
|
|
|
* Static methods |
|
59
|
|
|
*******************************/ |
|
60
|
|
|
public static function for(array $crudObject, array $definition): UploaderInterface |
|
61
|
|
|
{ |
|
62
|
|
|
return new static($crudObject, $definition); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/******************************* |
|
66
|
|
|
* public methods - event handler methods |
|
67
|
|
|
*******************************/ |
|
68
|
|
|
public function storeUploadedFiles(Model $entry): Model |
|
69
|
|
|
{ |
|
70
|
|
|
if ($this->handleRepeatableFiles) { |
|
71
|
|
|
return $this->handleRepeatableFiles($entry); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if ($this->attachedToFakeField) { |
|
75
|
|
|
$fakeFieldValue = $entry->{$this->attachedToFakeField}; |
|
76
|
|
|
$fakeFieldValue = is_string($fakeFieldValue) ? json_decode($fakeFieldValue, true) : (array) $fakeFieldValue; |
|
77
|
|
|
$fakeFieldValue[$this->getName()] = $this->uploadFiles($entry); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$entry->{$this->attachedToFakeField} = isset($entry->getCasts()[$this->attachedToFakeField]) ? $fakeFieldValue : json_encode($fakeFieldValue); |
|
80
|
|
|
|
|
81
|
|
|
return $entry; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$entry->{$this->getName()} = $this->uploadFiles($entry); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
return $entry; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function retrieveUploadedFiles(Model $entry): Model |
|
90
|
|
|
{ |
|
91
|
|
|
if ($this->handleRepeatableFiles) { |
|
92
|
|
|
return $this->retrieveRepeatableFiles($entry); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->retrieveFiles($entry); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function deleteUploadedFiles(Model $entry): void |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->deleteWhenEntryIsDeleted) { |
|
101
|
|
|
if (! in_array(SoftDeletes::class, class_uses_recursive($entry), true)) { |
|
102
|
|
|
$this->performFileDeletion($entry); |
|
103
|
|
|
|
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($entry->isForceDeleting() === true) { |
|
108
|
|
|
$this->performFileDeletion($entry); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/******************************* |
|
114
|
|
|
* Getters |
|
115
|
|
|
*******************************/ |
|
116
|
|
|
public function getName(): string |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->name; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function getDisk(): string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->disk; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getPath(): string |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->path; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function useTemporaryUrl(): bool |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->useTemporaryUrl; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function getExpirationTimeInMinutes(): int |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->temporaryUrlExpirationTimeInMinutes; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function shouldDeleteFiles(): bool |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->deleteWhenEntryIsDeleted; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function getIdentifier(): string |
|
147
|
|
|
{ |
|
148
|
|
|
if ($this->handleRepeatableFiles) { |
|
149
|
|
|
return $this->repeatableContainerName.'_'.$this->name; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->name; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function canHandleMultipleFiles(): bool |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->handleMultipleFiles; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function getPreviousFiles(Model $entry): mixed |
|
161
|
|
|
{ |
|
162
|
|
|
if (! $this->attachedToFakeField) { |
|
163
|
|
|
return $entry->getOriginal($this->getName()); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$value = $entry->getOriginal($this->attachedToFakeField); |
|
|
|
|
|
|
167
|
|
|
$value = is_string($value) ? json_decode($value, true) : (array) $value; |
|
168
|
|
|
|
|
169
|
|
|
return $value[$this->getName()] ?? null; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/******************************* |
|
173
|
|
|
* Setters - fluently configure the uploader |
|
174
|
|
|
*******************************/ |
|
175
|
|
|
public function multiple(): self |
|
176
|
|
|
{ |
|
177
|
|
|
$this->handleMultipleFiles = true; |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function relationship(bool $isRelationship): self |
|
183
|
|
|
{ |
|
184
|
|
|
$this->isRelationship = $isRelationship; |
|
185
|
|
|
|
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/******************************* |
|
190
|
|
|
* Default implementation functions |
|
191
|
|
|
*******************************/ |
|
192
|
|
|
public function uploadFiles(Model $entry, $values = null) |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
private function retrieveFiles(Model $entry): Model |
|
197
|
|
|
{ |
|
198
|
|
|
$value = $entry->{$this->name}; |
|
199
|
|
|
|
|
200
|
|
|
if ($this->handleMultipleFiles) { |
|
201
|
|
|
if (! isset($entry->getCasts()[$this->name]) && is_string($value)) { |
|
202
|
|
|
$entry->{$this->name} = json_decode($value, true); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $entry; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
if ($this->attachedToFakeField) { |
|
209
|
|
|
$values = $entry->{$this->attachedToFakeField}; |
|
210
|
|
|
$values = is_string($values) ? json_decode($values, true) : (array) $values; |
|
211
|
|
|
|
|
212
|
|
|
$values[$this->name] = isset($values[$this->name]) ? Str::after($values[$this->name], $this->path) : null; |
|
213
|
|
|
$entry->{$this->attachedToFakeField} = json_encode($values); |
|
214
|
|
|
|
|
215
|
|
|
return $entry; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$entry->{$this->name} = Str::after($value, $this->path); |
|
219
|
|
|
|
|
220
|
|
|
return $entry; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
private function deleteFiles(Model $entry) |
|
224
|
|
|
{ |
|
225
|
|
|
$values = $entry->{$this->name}; |
|
226
|
|
|
|
|
227
|
|
|
if ($this->handleMultipleFiles) { |
|
228
|
|
|
// ensure we have an array of values when field is not casted in model. |
|
229
|
|
|
if (! isset($entry->getCasts()[$this->name]) && is_string($values)) { |
|
230
|
|
|
$values = json_decode($values, true); |
|
231
|
|
|
} |
|
232
|
|
|
foreach ($values as $value) { |
|
233
|
|
|
Storage::disk($this->disk)->delete($this->path.$value); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
return; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$values = Str::after($values, $this->path); |
|
240
|
|
|
Storage::disk($this->disk)->delete($this->path.$values); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
private function performFileDeletion(Model $entry) |
|
244
|
|
|
{ |
|
245
|
|
|
if ($this->isRelationship || ! $this->handleRepeatableFiles) { |
|
246
|
|
|
$this->deleteFiles($entry); |
|
247
|
|
|
|
|
248
|
|
|
return; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
$this->deleteRepeatableFiles($entry); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/******************************* |
|
255
|
|
|
* Private helper methods |
|
256
|
|
|
*******************************/ |
|
257
|
|
|
private function getPathFromConfiguration(array $crudObject, array $configuration): string |
|
258
|
|
|
{ |
|
259
|
|
|
$this->path = $configuration['path'] ?? $crudObject['prefix'] ?? $this->path; |
|
260
|
|
|
|
|
261
|
|
|
return empty($this->path) ? $this->path : Str::of($this->path)->finish('/')->value(); |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.