1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Core\Content\Media; |
4
|
|
|
|
5
|
|
|
use Shopware\Core\Framework\HttpException; |
6
|
|
|
use Shopware\Core\Framework\Log\Package; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response; |
8
|
|
|
|
9
|
|
|
#[Package('content')] |
10
|
|
|
class MediaException extends HttpException |
11
|
|
|
{ |
12
|
|
|
public const MEDIA_INVALID_CONTENT_LENGTH = 'CONTENT__MEDIA_INVALID_CONTENT_LENGTH'; |
13
|
|
|
public const MEDIA_INVALID_URL = 'CONTENT__MEDIA_INVALID_URL'; |
14
|
|
|
public const MEDIA_ILLEGAL_URL = 'CONTENT__MEDIA_ILLEGAL_URL'; |
15
|
|
|
public const MEDIA_DISABLE_URL_UPLOAD_FEATURE = 'CONTENT__MEDIA_DISABLE_URL_UPLOAD_FEATURE'; |
16
|
|
|
public const MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_READ = 'CONTENT__MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_READ'; |
17
|
|
|
public const MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_WRITE = 'CONTENT__MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_WRITE'; |
18
|
|
|
public const MEDIA_CANNOT_COPY_MEDIA = 'CONTENT__MEDIA_CANNOT_COPY_MEDIA'; |
19
|
|
|
public const MEDIA_FILE_SIZE_LIMIT_EXCEEDED = 'CONTENT__MEDIA_FILE_SIZE_LIMIT_EXCEEDED'; |
20
|
|
|
public const MEDIA_MISSING_FILE_EXTENSION = 'CONTENT__MEDIA_MISSING_FILE_EXTENSION'; |
21
|
|
|
public const MEDIA_ILLEGAL_FILE_NAME = 'CONTENT__MEDIA_ILLEGAL_FILE_NAME'; |
22
|
|
|
public const MEDIA_EMPTY_FILE = 'CONTENT__MEDIA_EMPTY_FILE'; |
23
|
|
|
public const MEDIA_INVALID_FILE = 'CONTENT__MEDIA_INVALID_FILE'; |
24
|
|
|
public const MEDIA_EMPTY_FILE_NAME = 'CONTENT__MEDIA_EMPTY_FILE_NAME'; |
25
|
|
|
public const MEDIA_FOLDER_NOT_FOUND = 'CONTENT__MEDIA_FOLDER_NOT_FOUND'; |
26
|
|
|
public const MEDIA_FOLDER_NAME_NOT_FOUND = 'CONTENT__MEDIA_FOLDER_NAME_NOT_FOUND'; |
27
|
|
|
public const MEDIA_FILE_TYPE_NOT_SUPPORTED = 'CONTENT__MEDIA_FILE_TYPE_NOT_SUPPORTED'; |
28
|
|
|
public const MEDIA_COULD_NOT_RENAME_FILE = 'CONTENT__MEDIA_COULD_NOT_RENAME_FILE'; |
29
|
|
|
public const MEDIA_EMPTY_ID = 'CONTENT__MEDIA_EMPTY_ID'; |
30
|
|
|
public const MEDIA_INVALID_BATCH_SIZE = 'CONTENT__MEDIA_INVALID_BATCH_SIZE'; |
31
|
|
|
public const MEDIA_THUMBNAIL_ASSOCIATION_NOT_LOADED = 'CONTENT__MEDIA_THUMBNAIL_ASSOCIATION_NOT_LOADED'; |
32
|
|
|
public const MEDIA_TYPE_NOT_LOADED = 'CONTENT__MEDIA_TYPE_NOT_LOADED'; |
33
|
|
|
public const MEDIA_FILE_NOT_SUPPORTED_FOR_THUMBNAIL = 'CONTENT__MEDIA_FILE_NOT_SUPPORTED_FOR_THUMBNAIL'; |
34
|
|
|
public const MEDIA_THUMBNAIL_NOT_SAVED = 'CONTENT__MEDIA_THUMBNAIL_NOT_SAVED'; |
35
|
|
|
public const MEDIA_CANNOT_CREATE_IMAGE_HANDLE = 'CONTENT__MEDIA_CANNOT_CREATE_IMAGE_HANDLE'; |
36
|
|
|
public const MEDIA_CONTAINS_NO_THUMBNAILS = 'CONTENT__MEDIA_CONTAINS_NO_THUMBNAILS'; |
37
|
|
|
public const MEDIA_STRATEGY_NOT_FOUND = 'CONTENT__MEDIA_STRATEGY_NOT_FOUND'; |
38
|
|
|
public const MEDIA_INVALID_FILE_SYSTEM_VISIBILITY = 'CONTENT__MEDIA_INVALID_FILE_SYSTEM_VISIBILITY'; |
39
|
|
|
public const MEDIA_FILE_IS_NOT_INSTANCE_OF_FILE_SYSTEM = 'CONTENT__MEDIA_FILE_IS_NOT_INSTANCE_OF_FILE_SYSTEM'; |
40
|
|
|
public const MEDIA_MISSING_URL_PARAMETER = 'CONTENT__MEDIA_MISSING_URL_PARAMETER'; |
41
|
|
|
public const MEDIA_CANNOT_CREATE_TEMP_FILE = 'CONTENT__MEDIA_CANNOT_CREATE_TEMP_FILE'; |
42
|
|
|
public const MEDIA_FILE_NOT_FOUND = 'CONTENT__MEDIA_FILE_NOT_FOUND'; |
43
|
|
|
public const MEDIA_MISSING_FILE = 'CONTENT__MEDIA_MISSING_FILE'; |
44
|
|
|
public const MEDIA_NOT_FOUND = 'CONTENT__MEDIA_NOT_FOUND'; |
45
|
|
|
public const MEDIA_DUPLICATED_FILE_NAME = 'CONTENT__MEDIA_DUPLICATED_FILE_NAME'; |
46
|
|
|
|
47
|
|
|
public static function invalidContentLength(): self |
48
|
|
|
{ |
49
|
|
|
return new self( |
50
|
|
|
Response::HTTP_BAD_REQUEST, |
51
|
|
|
self::MEDIA_INVALID_CONTENT_LENGTH, |
52
|
|
|
'Expected content-length did not match actual size.' |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function invalidUrl(string $url): self |
57
|
|
|
{ |
58
|
|
|
return new self( |
59
|
|
|
Response::HTTP_BAD_REQUEST, |
60
|
|
|
self::MEDIA_INVALID_URL, |
61
|
|
|
'Provided URL "{{ url }}" is invalid.', |
62
|
|
|
['url' => $url] |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function illegalUrl(string $url): self |
67
|
|
|
{ |
68
|
|
|
return new self( |
69
|
|
|
Response::HTTP_BAD_REQUEST, |
70
|
|
|
self::MEDIA_ILLEGAL_URL, |
71
|
|
|
'Provided URL "{{ url }}" is not allowed.', |
72
|
|
|
['url' => $url] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function disableUrlUploadFeature(): self |
77
|
|
|
{ |
78
|
|
|
return new self( |
79
|
|
|
Response::HTTP_BAD_REQUEST, |
80
|
|
|
self::MEDIA_DISABLE_URL_UPLOAD_FEATURE, |
81
|
|
|
'The feature to upload a media via URL is disabled.' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public static function cannotOpenSourceStreamToRead(string $url): self |
86
|
|
|
{ |
87
|
|
|
return new self( |
88
|
|
|
Response::HTTP_BAD_REQUEST, |
89
|
|
|
self::MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_READ, |
90
|
|
|
'Cannot open source stream to read from {{ url }}.', |
91
|
|
|
['url' => $url] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function cannotOpenSourceStreamToWrite(string $fileName): self |
96
|
|
|
{ |
97
|
|
|
return new self( |
98
|
|
|
Response::HTTP_BAD_REQUEST, |
99
|
|
|
self::MEDIA_CANNOT_OPEN_SOURCE_STREAM_TO_WRITE, |
100
|
|
|
'Cannot open source stream to write upload data: {{ fileName }}.', |
101
|
|
|
['fileName' => $fileName] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public static function cannotCopyMedia(): self |
106
|
|
|
{ |
107
|
|
|
return new self( |
108
|
|
|
Response::HTTP_CONFLICT, |
109
|
|
|
self::MEDIA_CANNOT_COPY_MEDIA, |
110
|
|
|
'Error while copying media from source.' |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function fileSizeLimitExceeded(): self |
115
|
|
|
{ |
116
|
|
|
return new self( |
117
|
|
|
Response::HTTP_BAD_REQUEST, |
118
|
|
|
self::MEDIA_FILE_SIZE_LIMIT_EXCEEDED, |
119
|
|
|
'Source file exceeds maximum file size limit.' |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function missingFileExtension(): self |
124
|
|
|
{ |
125
|
|
|
return new self( |
126
|
|
|
Response::HTTP_BAD_REQUEST, |
127
|
|
|
self::MEDIA_MISSING_FILE_EXTENSION, |
128
|
|
|
'No file extension provided. Please use the "extension" query parameter to specify the extension of the uploaded file.' |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public static function illegalFileName(string $filename, string $cause): self |
133
|
|
|
{ |
134
|
|
|
return new self( |
135
|
|
|
Response::HTTP_BAD_REQUEST, |
136
|
|
|
self::MEDIA_ILLEGAL_FILE_NAME, |
137
|
|
|
'Provided filename "{{ fileName }}" is not permitted: {{ cause }}', |
138
|
|
|
['fileName' => $filename, 'cause' => $cause] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public static function mediaNotFound(string $mediaId): self |
143
|
|
|
{ |
144
|
|
|
return new self( |
145
|
|
|
Response::HTTP_NOT_FOUND, |
146
|
|
|
self::MEDIA_NOT_FOUND, |
147
|
|
|
'Media for id {{ mediaId }} not found.', |
148
|
|
|
['mediaId' => $mediaId] |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public static function invalidFile(string $cause): self |
153
|
|
|
{ |
154
|
|
|
return new self( |
155
|
|
|
Response::HTTP_BAD_REQUEST, |
156
|
|
|
self::MEDIA_INVALID_FILE, |
157
|
|
|
'Provided file is invalid: {{ cause }}.', |
158
|
|
|
['cause' => $cause] |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public static function emptyMediaFilename(): self |
163
|
|
|
{ |
164
|
|
|
return new self( |
165
|
|
|
Response::HTTP_BAD_REQUEST, |
166
|
|
|
self::MEDIA_EMPTY_FILE_NAME, |
167
|
|
|
'A valid filename must be provided.' |
168
|
|
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public static function duplicatedMediaFileName(string $fileName, string $fileExtension): self |
172
|
|
|
{ |
173
|
|
|
return new self( |
174
|
|
|
Response::HTTP_CONFLICT, |
175
|
|
|
self::MEDIA_DUPLICATED_FILE_NAME, |
176
|
|
|
'A file with the name "{{ fileName }}.{{ fileExtension }}" already exists.', |
177
|
|
|
['fileName' => $fileName, 'fileExtension' => $fileExtension] |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public static function missingFile(string $mediaId): self |
182
|
|
|
{ |
183
|
|
|
return new self( |
184
|
|
|
Response::HTTP_NOT_FOUND, |
185
|
|
|
self::MEDIA_MISSING_FILE, |
186
|
|
|
'Could not find file for media with id: "{{ mediaId }}"', |
187
|
|
|
['mediaId' => $mediaId] |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public static function mediaFolderIdNotFound(string $folderId): self |
192
|
|
|
{ |
193
|
|
|
return new self( |
194
|
|
|
Response::HTTP_NOT_FOUND, |
195
|
|
|
self::MEDIA_FOLDER_NOT_FOUND, |
196
|
|
|
'Could not find media folder with id: "{{ folderId }}"', |
197
|
|
|
['folderId' => $folderId] |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public static function mediaFolderNameNotFound(string $folderName): self |
202
|
|
|
{ |
203
|
|
|
return new self( |
204
|
|
|
Response::HTTP_NOT_FOUND, |
205
|
|
|
self::MEDIA_FOLDER_NAME_NOT_FOUND, |
206
|
|
|
'Could not find a folder with the name: "{{ folderName }}"', |
207
|
|
|
['folderName' => $folderName] |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public static function fileExtensionNotSupported(string $mediaId, string $extension): self |
212
|
|
|
{ |
213
|
|
|
return new self( |
214
|
|
|
Response::HTTP_BAD_REQUEST, |
215
|
|
|
self::MEDIA_FILE_TYPE_NOT_SUPPORTED, |
216
|
|
|
'The file extension "{{ extension }}" for media object with id {{ mediaId }} is not supported.', |
217
|
|
|
['mediaId' => $mediaId, 'extension' => $extension] |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public static function couldNotRenameFile(string $mediaId, string $oldFileName): self |
222
|
|
|
{ |
223
|
|
|
return new self( |
224
|
|
|
Response::HTTP_CONFLICT, |
225
|
|
|
self::MEDIA_COULD_NOT_RENAME_FILE, |
226
|
|
|
'Could not rename file for media with id: {{ mediaId }}. Rollback to filename: "{{ oldFileName }}"', |
227
|
|
|
['mediaId' => $mediaId, 'oldFileName' => $oldFileName] |
228
|
|
|
); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public static function emptyMediaId(): self |
232
|
|
|
{ |
233
|
|
|
return new self( |
234
|
|
|
Response::HTTP_BAD_REQUEST, |
235
|
|
|
self::MEDIA_EMPTY_ID, |
236
|
|
|
'A media id must be provided.' |
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public static function invalidBatchSize(): self |
241
|
|
|
{ |
242
|
|
|
return new self( |
243
|
|
|
Response::HTTP_BAD_REQUEST, |
244
|
|
|
self::MEDIA_INVALID_BATCH_SIZE, |
245
|
|
|
'Provided batch size is invalid.' |
246
|
|
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public static function thumbnailAssociationNotLoaded(): self |
250
|
|
|
{ |
251
|
|
|
return new self( |
252
|
|
|
Response::HTTP_BAD_REQUEST, |
253
|
|
|
self::MEDIA_THUMBNAIL_ASSOCIATION_NOT_LOADED, |
254
|
|
|
'Thumbnail association not loaded - please pre load media thumbnails.' |
255
|
|
|
); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
public static function mediaTypeNotLoaded(string $mediaId): self |
259
|
|
|
{ |
260
|
|
|
return new self( |
261
|
|
|
Response::HTTP_BAD_REQUEST, |
262
|
|
|
self::MEDIA_TYPE_NOT_LOADED, |
263
|
|
|
'Media type, for id {{ mediaId }}, not loaded', |
264
|
|
|
['mediaId' => $mediaId] |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
public static function thumbnailNotSupported(string $mediaId): self |
269
|
|
|
{ |
270
|
|
|
return new self( |
271
|
|
|
Response::HTTP_BAD_REQUEST, |
272
|
|
|
self::MEDIA_FILE_NOT_SUPPORTED_FOR_THUMBNAIL, |
273
|
|
|
'The file for media object with id {{ mediaId }} is not supported for creating thumbnails.', |
274
|
|
|
['mediaId' => $mediaId] |
275
|
|
|
); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public static function thumbnailCouldNotBeSaved(string $url): self |
279
|
|
|
{ |
280
|
|
|
return new self( |
281
|
|
|
Response::HTTP_CONFLICT, |
282
|
|
|
self::MEDIA_THUMBNAIL_NOT_SAVED, |
283
|
|
|
'Thumbnail could not be saved to location: {{ location }}.', |
284
|
|
|
['location' => $url] |
285
|
|
|
); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
public static function cannotCreateImage(): self |
289
|
|
|
{ |
290
|
|
|
return new self( |
291
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
292
|
|
|
self::MEDIA_CANNOT_CREATE_IMAGE_HANDLE, |
293
|
|
|
'Can not create image handle.' |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public static function mediaContainsNoThumbnails(): self |
298
|
|
|
{ |
299
|
|
|
return new self( |
300
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
301
|
|
|
self::MEDIA_CONTAINS_NO_THUMBNAILS, |
302
|
|
|
'Media contains no thumbnails.' |
303
|
|
|
); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public static function strategyNotFound(string $strategyName): self |
307
|
|
|
{ |
308
|
|
|
return new self( |
309
|
|
|
Response::HTTP_NOT_FOUND, |
310
|
|
|
self::MEDIA_STRATEGY_NOT_FOUND, |
311
|
|
|
'No Strategy with name "{{ strategyName }}" found.', |
312
|
|
|
['strategyName' => $strategyName] |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public static function invalidFilesystemVisibility(): self |
317
|
|
|
{ |
318
|
|
|
return new self( |
319
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
320
|
|
|
self::MEDIA_INVALID_FILE_SYSTEM_VISIBILITY, |
321
|
|
|
'Invalid filesystem visibility.' |
322
|
|
|
); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public static function fileIsNotInstanceOfFileSystem(): self |
326
|
|
|
{ |
327
|
|
|
return new self( |
328
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
329
|
|
|
self::MEDIA_FILE_IS_NOT_INSTANCE_OF_FILE_SYSTEM, |
330
|
|
|
'File is not an instance of FileSystem' |
331
|
|
|
); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public static function missingUrlParameter(): self |
335
|
|
|
{ |
336
|
|
|
return new self( |
337
|
|
|
Response::HTTP_BAD_REQUEST, |
338
|
|
|
self::MEDIA_MISSING_URL_PARAMETER, |
339
|
|
|
'Parameter url is missing.' |
340
|
|
|
); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public static function cannotCreateTempFile(): self |
344
|
|
|
{ |
345
|
|
|
return new self( |
346
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
347
|
|
|
self::MEDIA_CANNOT_CREATE_TEMP_FILE, |
348
|
|
|
'Cannot create a temp file.' |
349
|
|
|
); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public static function fileNotFound(string $path): self |
353
|
|
|
{ |
354
|
|
|
return new self( |
355
|
|
|
Response::HTTP_NOT_FOUND, |
356
|
|
|
self::MEDIA_FILE_NOT_FOUND, |
357
|
|
|
'The file "{{ path }}" does not exist', |
358
|
|
|
['path' => $path] |
359
|
|
|
); |
360
|
|
|
} |
361
|
|
|
} |
362
|
|
|
|