|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SimpleImageManager\Managers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\UploadedFile; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use SimpleImageManager\Contracts\ImageManagerInterface; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractImageManager implements ImageManagerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use HasSrcSet; |
|
13
|
|
|
use CanCreate; |
|
14
|
|
|
use CanDelete; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Use specific disk. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string|null |
|
20
|
|
|
*/ |
|
21
|
|
|
public ?string $disk = null; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Add prefix to files. Can be directory ot just filename prefix. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public string $prefix = ''; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Save original file. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array|null |
|
34
|
|
|
*/ |
|
35
|
|
|
public ?array $original = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Formats configurations list for creation. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
public array $formats = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Files extensions lists what should not be updated/cropped. Like svg or gif. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
public array $immutableExtensions = []; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
18 |
|
public function __construct(array $configs) |
|
53
|
|
|
{ |
|
54
|
18 |
|
$this->setDisk($configs['disk'] ?? null); |
|
55
|
|
|
|
|
56
|
17 |
|
if (array_key_exists('original', $configs)) { |
|
57
|
17 |
|
$this->setOriginal($configs['original']); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
17 |
|
if (array_key_exists('formats', $configs)) { |
|
61
|
17 |
|
$this->setFormats($configs['formats']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
17 |
|
if (array_key_exists('deletedFormats', $configs)) { |
|
65
|
17 |
|
$this->setDeletedFormats($configs['deletedFormats']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
17 |
|
if (array_key_exists('prefix', $configs)) { |
|
69
|
6 |
|
$this->setPrefix((string)$configs['prefix']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
17 |
|
if (array_key_exists('truncateDir', $configs)) { |
|
73
|
17 |
|
$this->truncateDir($configs['truncateDir']); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @deprecated */ |
|
77
|
17 |
|
if (array_key_exists('immutable_extensions', $configs)) { |
|
78
|
|
|
$this->setImmutableExtensions($configs['immutable_extensions']); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
17 |
|
if (array_key_exists('immutableExtensions', $configs)) { |
|
82
|
17 |
|
$this->setImmutableExtensions($configs['immutableExtensions']); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string|null $disk |
|
88
|
|
|
* |
|
89
|
|
|
* @return $this |
|
90
|
|
|
*/ |
|
91
|
18 |
|
public function setDisk(?string $disk = null): static |
|
92
|
|
|
{ |
|
93
|
18 |
|
if (empty($disk) || !is_string($disk)) { |
|
94
|
1 |
|
throw new \InvalidArgumentException("Driver configuration has not key 'disk'"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
17 |
|
$this->disk = $disk; |
|
98
|
|
|
|
|
99
|
17 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param array|null $original |
|
104
|
|
|
* |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
17 |
|
public function setOriginal(?array $original = null): static |
|
108
|
|
|
{ |
|
109
|
17 |
|
$this->original = $original; |
|
110
|
|
|
|
|
111
|
17 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $formats |
|
116
|
|
|
* |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
17 |
|
public function setFormats(array $formats = []): static |
|
120
|
|
|
{ |
|
121
|
17 |
|
$this->formats = $formats; |
|
122
|
|
|
|
|
123
|
17 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param array $immutableExtensions |
|
128
|
|
|
* |
|
129
|
|
|
* @return $this |
|
130
|
|
|
*/ |
|
131
|
17 |
|
public function setImmutableExtensions(array $immutableExtensions = []): static |
|
132
|
|
|
{ |
|
133
|
17 |
|
$this->immutableExtensions = $immutableExtensions; |
|
134
|
|
|
|
|
135
|
17 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @param ?string $prefix |
|
140
|
|
|
* |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
6 |
|
public function setPrefix(?string $prefix = null): static |
|
144
|
|
|
{ |
|
145
|
6 |
|
$this->prefix = (string)$prefix; |
|
146
|
|
|
|
|
147
|
6 |
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @inheritDoc |
|
152
|
|
|
*/ |
|
153
|
9 |
|
public function upload(UploadedFile $image, ?string $fileName = null, ?string $oldFile = null): string |
|
154
|
|
|
{ |
|
155
|
9 |
|
if ($oldFile) { |
|
156
|
1 |
|
$this->delete($oldFile); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
9 |
|
$newFileName = $this->makeFileName($fileName); |
|
160
|
|
|
|
|
161
|
|
|
// Clear extension if exists |
|
162
|
9 |
|
if (Str::endsWith($newFileName, ".{$image->extension()}")) { |
|
163
|
|
|
$newFileName = Str::beforeLast($newFileName, ".{$image->extension()}"); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
9 |
|
$this->ensureDirectoryExists($newFileName); |
|
167
|
|
|
|
|
168
|
9 |
|
$newFileExt = ".{$image->extension()}"; |
|
169
|
|
|
|
|
170
|
9 |
|
if ($this->original) { |
|
171
|
9 |
|
$this->createOriginalFile($image, $newFileName, $newFileExt); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
9 |
|
$this->createFormats($image, $newFileName, $newFileExt); |
|
175
|
|
|
|
|
176
|
9 |
|
return "{$newFileName}{$newFileExt}"; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @inheritDoc |
|
181
|
|
|
*/ |
|
182
|
7 |
|
public function delete(string $fileName): bool |
|
183
|
|
|
{ |
|
184
|
7 |
|
$filesToDelete = $this->filesToDelete($fileName); |
|
185
|
|
|
|
|
186
|
7 |
|
if (empty($filesToDelete)) { |
|
187
|
1 |
|
return false; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
6 |
|
$isDeleted = $this->storage()->delete($filesToDelete); |
|
191
|
|
|
|
|
192
|
6 |
|
$this->truncateDirectory($fileName); |
|
193
|
|
|
|
|
194
|
6 |
|
return $isDeleted; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @inheritDoc |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public function deleteSingle(string $fileName, ?string $format = null): bool |
|
201
|
|
|
{ |
|
202
|
1 |
|
if (!$fileName) { |
|
203
|
1 |
|
return false; |
|
204
|
|
|
} |
|
205
|
1 |
|
if ($format) { |
|
206
|
1 |
|
[$name, $extension] = $this->explodeFilename($fileName); |
|
207
|
|
|
|
|
208
|
1 |
|
$fileName = "{$name}-{$format}.{$extension}"; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
1 |
|
return $this->storage()->delete($fileName); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @inheritDoc |
|
217
|
|
|
*/ |
|
218
|
10 |
|
public function path(string $fileName, ?string $format = null): ?string |
|
219
|
|
|
{ |
|
220
|
10 |
|
if (!$fileName) { |
|
221
|
1 |
|
return null; |
|
222
|
|
|
} |
|
223
|
9 |
|
if ($format) { |
|
224
|
3 |
|
[$name, $extension] = $this->explodeFilename($fileName); |
|
225
|
3 |
|
if (!in_array(".{$extension}", $this->immutableExtensions)) { |
|
226
|
2 |
|
$fileName = "{$name}-{$format}.{$extension}"; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
9 |
|
return $this->storage()->path($fileName); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @inheritDoc |
|
235
|
|
|
*/ |
|
236
|
9 |
|
public function url(string $fileName, ?string $format = null): ?string |
|
237
|
|
|
{ |
|
238
|
9 |
|
if (!$fileName) { |
|
239
|
1 |
|
return null; |
|
240
|
|
|
} |
|
241
|
8 |
|
if ($format) { |
|
242
|
2 |
|
[$name, $extension] = $this->explodeFilename($fileName); |
|
243
|
2 |
|
if (!in_array(".{$extension}", $this->immutableExtensions)) { |
|
244
|
1 |
|
$fileName = "{$name}-{$format}.{$extension}"; |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
8 |
|
return $this->storage()->url($fileName); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Returns name and extension of filename. |
|
253
|
|
|
* |
|
254
|
|
|
* @param string $fileName |
|
255
|
|
|
* @return array |
|
256
|
|
|
*/ |
|
257
|
7 |
|
protected function explodeFilename(string $fileName): array |
|
258
|
|
|
{ |
|
259
|
7 |
|
$extension = pathinfo($fileName, PATHINFO_EXTENSION); |
|
260
|
|
|
|
|
261
|
7 |
|
$name = Str::beforeLast($fileName, ".{$extension}"); |
|
262
|
|
|
|
|
263
|
7 |
|
return [$name, $extension]; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @return \Illuminate\Contracts\Filesystem\Filesystem |
|
268
|
|
|
*/ |
|
269
|
12 |
|
public function storage(): \Illuminate\Contracts\Filesystem\Filesystem |
|
270
|
|
|
{ |
|
271
|
12 |
|
return Storage::disk($this->disk); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
} |
|
278
|
|
|
|