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->getAttributeName()] = $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->getAttributeName()} = $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 getAttributeName(): string |
122
|
|
|
{ |
123
|
|
|
return Str::afterLast($this->name, '.'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getDisk(): string |
127
|
|
|
{ |
128
|
|
|
return $this->disk; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getPath(): string |
132
|
|
|
{ |
133
|
|
|
return $this->path; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function useTemporaryUrl(): bool |
137
|
|
|
{ |
138
|
|
|
return $this->useTemporaryUrl; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getExpirationTimeInMinutes(): int |
142
|
|
|
{ |
143
|
|
|
return $this->temporaryUrlExpirationTimeInMinutes; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function shouldDeleteFiles(): bool |
147
|
|
|
{ |
148
|
|
|
return $this->deleteWhenEntryIsDeleted; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getIdentifier(): string |
152
|
|
|
{ |
153
|
|
|
if ($this->handleRepeatableFiles) { |
154
|
|
|
return $this->repeatableContainerName.'_'.$this->name; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->name; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function canHandleMultipleFiles(): bool |
161
|
|
|
{ |
162
|
|
|
return $this->handleMultipleFiles; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function isRelationship(): bool |
166
|
|
|
{ |
167
|
|
|
return $this->isRelationship; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getPreviousFiles(Model $entry): mixed |
171
|
|
|
{ |
172
|
|
|
if (! $this->attachedToFakeField) { |
173
|
|
|
return $this->getOriginalValue($entry); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$value = $this->getOriginalValue($entry, $this->attachedToFakeField); |
177
|
|
|
$value = is_string($value) ? json_decode($value, true) : (array) $value; |
178
|
|
|
|
179
|
|
|
return $value[$this->getAttributeName()] ?? null; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/******************************* |
183
|
|
|
* Setters - fluently configure the uploader |
184
|
|
|
*******************************/ |
185
|
|
|
public function multiple(): self |
186
|
|
|
{ |
187
|
|
|
$this->handleMultipleFiles = true; |
188
|
|
|
|
189
|
|
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function relationship(bool $isRelationship): self |
193
|
|
|
{ |
194
|
|
|
$this->isRelationship = $isRelationship; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/******************************* |
200
|
|
|
* Default implementation functions |
201
|
|
|
*******************************/ |
202
|
|
|
public function uploadFiles(Model $entry, $values = null) |
|
|
|
|
203
|
|
|
{ |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
private function retrieveFiles(Model $entry): Model |
207
|
|
|
{ |
208
|
|
|
$value = $entry->{$this->getAttributeName()}; |
209
|
|
|
|
210
|
|
|
if ($this->handleMultipleFiles) { |
211
|
|
|
if (! isset($entry->getCasts()[$this->getName()]) && is_string($value)) { |
212
|
|
|
$entry->{$this->getAttributeName()} = json_decode($value, true); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $entry; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
if ($this->attachedToFakeField) { |
219
|
|
|
$values = $entry->{$this->attachedToFakeField}; |
220
|
|
|
$values = is_string($values) ? json_decode($values, true) : (array) $values; |
221
|
|
|
|
222
|
|
|
$values[$this->getAttributeName()] = isset($values[$this->getAttributeName()]) ? Str::after($values[$this->getAttributeName()], $this->path) : null; |
223
|
|
|
$entry->{$this->attachedToFakeField} = json_encode($values); |
224
|
|
|
|
225
|
|
|
return $entry; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$entry->{$this->getAttributeName()} = Str::after($value, $this->path); |
229
|
|
|
|
230
|
|
|
return $entry; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
private function deleteFiles(Model $entry) |
234
|
|
|
{ |
235
|
|
|
$values = $entry->{$this->name}; |
236
|
|
|
|
237
|
|
|
if ($this->handleMultipleFiles) { |
238
|
|
|
// ensure we have an array of values when field is not casted in model. |
239
|
|
|
if (! isset($entry->getCasts()[$this->name]) && is_string($values)) { |
240
|
|
|
$values = json_decode($values, true); |
241
|
|
|
} |
242
|
|
|
foreach ($values as $value) { |
243
|
|
|
Storage::disk($this->disk)->delete($this->path.$value); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$values = Str::after($values, $this->path); |
250
|
|
|
Storage::disk($this->disk)->delete($this->path.$values); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
private function performFileDeletion(Model $entry) |
254
|
|
|
{ |
255
|
|
|
if (! $this->handleRepeatableFiles) { |
256
|
|
|
$this->deleteFiles($entry); |
257
|
|
|
|
258
|
|
|
return; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$this->deleteRepeatableFiles($entry); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/******************************* |
265
|
|
|
* Private helper methods |
266
|
|
|
*******************************/ |
267
|
|
|
private function getPathFromConfiguration(array $crudObject, array $configuration): string |
268
|
|
|
{ |
269
|
|
|
$this->path = $configuration['path'] ?? $crudObject['prefix'] ?? $this->path; |
270
|
|
|
|
271
|
|
|
return empty($this->path) ? $this->path : Str::of($this->path)->finish('/')->value(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
private function getOriginalValue(Model $entry, $field = null) |
275
|
|
|
{ |
276
|
|
|
$previousValue = $entry->getOriginal($field ?? $this->getAttributeName()); |
277
|
|
|
|
278
|
|
|
if (! $previousValue) { |
279
|
|
|
return $previousValue; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (method_exists($entry, 'translationEnabled') && $entry->translationEnabled()) { |
283
|
|
|
return $previousValue[$entry->getLocale()] ?? null; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $previousValue; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|