/
Standard.php
543 lines (475 loc) · 17.8 KB
/
Standard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2015-2018
* @package Controller
* @subpackage Common
*/
namespace Aimeos\Controller\Common\Media;
/**
* Common media controller methods
*
* @package Controller
* @subpackage Common
*/
class Standard
implements \Aimeos\Controller\Common\Media\Iface
{
private $context;
/**
* Initializes the object
*
* @param \Aimeos\MShop\Context\Item\Iface $context Context object
*/
public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
{
$this->context = $context;
}
/**
* Stores the uploaded file and adds the references to the media item
*
* {inheritDoc}
*
* @param \Aimeos\MShop\Media\Item\Iface $item Media item to add the file references to
* @param \Psr\Http\Message\UploadedFileInterface Uploaded file
* @param string $fsname Name of the file system to store the files at
*/
public function add( \Aimeos\MShop\Media\Item\Iface $item, \Psr\Http\Message\UploadedFileInterface $file, $fsname = 'fs-media' )
{
$this->checkFileUpload( $file );
$media = $this->getMediaFile( $file->getStream() );
if( $media instanceof \Aimeos\MW\Media\Image\Iface )
{
$this->scaleImage( $media, 'files' );
$mimetype = $this->getMimeType( $media, 'files' );
$filepath = $this->getFilePath( $file->getClientFilename(), 'files', $mimetype );
$this->storeFile( $media->save( null, $mimetype ), $fsname, $filepath, $item->getUrl() );
$item->setUrl( $filepath );
$this->scaleImage( $media, 'preview' );
$mimeprev = $this->getMimeType( $media, 'preview' );
$filepath = $this->getFilePath( $file->getClientFilename(), 'preview', $mimeprev );
$this->storeFile( $media->save( null, $mimetype ), $fsname, $filepath, $item->getPreview() );
$item->setPreview( $filepath );
}
else
{
$mimetype = $media->getMimeType();
$item->setPreview( $this->getMimeIcon( $mimetype ) );
$filepath = $this->getFilePath( $file->getClientFilename(), 'files', $mimetype );
$this->storeFile( $media->save(), $fsname, $filepath, $item->getPreview() );
$item->setUrl( $filepath );
}
$item->getLabel() ?: $item->setLabel( basename( $file->getClientFilename() ) );
$item->setMimeType( $mimetype );
}
/**
* Deletes the files of the media item
*
* {inheritDoc}
*
* @param \Aimeos\MShop\Media\Item\Iface $item Media item whose files should be deleted
* @param string $fsname Name of the file system to delete the files from
*/
public function delete( \Aimeos\MShop\Media\Item\Iface $item, $fsname = 'fs-media' )
{
$manager = \Aimeos\MShop\Factory::createManager( $this->context, 'media' );
$search = $manager->createSearch()->setSlice( 0, 2 );
$search->setConditions( $search->compare( '==', 'media.url', $item->getUrl() ) );
if( count( $manager->searchItems( $search ) ) > 1 ) {
return $this;
}
$fs = $this->context->getFilesystemManager()->get( $fsname );
$path = $item->getUrl();
if( $path !== '' && $fs->has( $path ) ) {
$fs->rm( $path );
}
$item->setUrl( '' );
try
{
$path = $item->getPreview();
if( $path !== '' && $fs->has( $path ) ) {
$fs->rm( $path );
}
}
catch( \Exception $e ) { ; } // Can be a mime icon with relative path
$item->setPreview( '' );
}
/**
* Rescales the files (original and preview) referenced by the media item
*
* The height/width configuration for scaling and which one should be scaled is used from
* - controller/common/media/standard/<files|preview>/maxheight
* - controller/common/media/standard/<files|preview>/maxwidth
* - controller/common/media/standard/<files|preview>/scale
*
* @param \Aimeos\MShop\Media\Item\Iface $item Media item whose files should be scaled
* @param string $fsname Name of the file system to rescale the files from
* @return void
*/
public function scale( \Aimeos\MShop\Media\Item\Iface $item, $fsname = 'fs-media' )
{
$path = $item->getUrl();
$config = $this->context->getConfig();
$media = $this->getMediaFile( $this->getFileContent( $path, $fsname ) );
if( !( $media instanceof \Aimeos\MW\Media\Image\Iface ) ) {
return;
}
$this->scaleImage( $media, 'files' );
$mime = $this->getMimeType( $media, 'files' );
$filepath = ( substr( $path, 0, 6 ) !== 'files/' ? $this->getFilePath( $path, 'files', $mime ) : $path );
$this->storeFile( $media->save( null, $mime ), $fsname, $filepath, $path );
$item->setUrl( $filepath );
$this->scaleImage( $media, 'preview' );
$mime = $this->getMimeType( $media, 'preview' );
$filepath = ( substr( $path, 0, 8 ) !== 'preview/' ? $this->getFilePath( $path, 'preview', $mime ) : $path );
$this->storeFile( $media->save( null, $mime ), $fsname, $filepath, $item->getPreview() ); $item->setPreview( $filepath );
}
/**
* Checks if an error during upload occured
*
* @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file
* @throws \Aimeos\Controller\Common\Exception If an error occured during upload
*/
protected function checkFileUpload( \Psr\Http\Message\UploadedFileInterface $file )
{
if( $file->getError() !== UPLOAD_ERR_OK )
{
switch( $file->getError() )
{
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new \Aimeos\Controller\Common\Exception( 'The uploaded file exceeds the max. allowed filesize' );
case UPLOAD_ERR_PARTIAL:
throw new \Aimeos\Controller\Common\Exception( 'The uploaded file was only partially uploaded' );
case UPLOAD_ERR_NO_FILE:
throw new \Aimeos\Controller\Common\Exception( 'No file was uploaded' );
case UPLOAD_ERR_NO_TMP_DIR:
throw new \Aimeos\Controller\Common\Exception( 'Temporary folder is missing' );
case UPLOAD_ERR_CANT_WRITE:
throw new \Aimeos\Controller\Common\Exception( 'Failed to write file to disk' );
case UPLOAD_ERR_EXTENSION:
throw new \Aimeos\Controller\Common\Exception( 'File upload stopped by extension' );
default:
throw new \Aimeos\Controller\Common\Exception( 'Unknown upload error' );
}
}
}
/**
* Returns the file content of the file or URL
*
* @param string $path Path to the file or URL
* @param string $fsname File system name the file is located at
* @return string File content
* @throws \Aimeos\Controller\Common\Exception If no file is found
*/
protected function getFileContent( $path, $fsname )
{
if( $path !== '' )
{
if( preg_match( '#^[a-zA-Z]{1,10}://#', $path ) === 1 )
{
if( ( $content = @file_get_contents( $path ) ) === false )
{
$msg = sprintf( 'Downloading file "%1$s" failed', $path );
throw new \Aimeos\Controller\Common\Exception( $msg );
}
return $content;
}
$fs = $this->context->getFilesystemManager()->get( $fsname );
if( $fs->has( $path ) !== false ) {
return $fs->read( $path );
}
}
throw new \Aimeos\Controller\Common\Exception( sprintf( 'File "%1$s" not found', $path ) );
}
/**
* Creates a new file path from the given arguments and random values
*
* @param string $filename Original file name, can contain the path as well
* @param string $type File type, i.e. "files" or "preview"
* @param string $mimetype Mime type of the file
* @return string New file name including the file path
*/
protected function getFilePath( $filename, $type, $mimetype )
{
/** controller/common/media/standard/extensions
* Available files extensions for mime types of uploaded files
*
* Uploaded files should have the right file extension (e.g. ".jpg" for
* JPEG images) so files are recognized correctly if downloaded by users.
* The extension of the uploaded file can't be trusted and only its mime
* type can be determined automatically. This configuration setting
* provides the file extensions for the configured mime types. You can
* add more mime type / file extension combinations if required.
*
* @param array Associative list of mime types as keys and file extensions as values
* @since 2018.04
* @category Developer
*/
$list = $this->context->getConfig()->get( 'controller/common/media/standard/extensions', [] );
$ext = '';
if( isset( $list[$mimetype] ) ) {
$ext = '.' . $list[$mimetype];
}
$filename = md5( $filename . getmypid() . microtime( true ) );
return "${type}/${filename[0]}/${filename[1]}/${filename}${ext}";
}
/**
* Returns the media object for the given file name
*
* @param string $file Path to the file or file content
* @return \Aimeos\MW\Media\Iface Media object
*/
protected function getMediaFile( $file )
{
/** controller/common/media/standard/options
* Options used for processing the uploaded media files
*
* When uploading a file, a preview image for that file is generated if
* possible (especially for images). You can configure certain options
* for the generated images, namely the implementation of the scaling
* algorithm and the quality of the resulting images with
*
* array(
* 'image' => array(
* 'name' => 'Imagick',
* 'quality' => 75,
* )
* )
*
* @param array Multi-dimendional list of configuration options
* @since 2016.01
* @category Developer
* @category User
*/
$options = $this->context->getConfig()->get( 'controller/common/media/standard/options', [] );
return \Aimeos\MW\Media\Factory::get( $file, $options );
}
/**
* Returns the relative path to the mime icon for the given mime type.
*
* @param string $mimetype Mime type like "image/png"
* @return string Relative path to the mime icon
*/
protected function getMimeIcon( $mimetype )
{
$config = $this->context->getConfig();
/** controller/common/media/standard/mimeicon/directory
* Directory that contains the icons for the different mime types
*
* If no preview image can be generated from an uploaded file, an icon
* for its mime type is displayed instead. The directory for the mime
* icons is structured by the general mime type (e.g. "image") as
* sub-directory and the specific name of the mime type (e.g. "jpeg")
* as file name.
*
* Avoid leading and trailing slashes for the upload directory string!
*
* @param string Path or URL to the base directory
* @since 2016.01
* @category Developer
*/
if( ( $mimedir = $config->get( 'controller/common/media/standard/mimeicon/directory' ) ) == null ) {
return '';
}
/** controller/common/media/standard/mimeicon/extension
* File extension of the mime icon images
*
* If you would like to use different mime icons that are available in
* another file format, you have to change the file extension for the
* mime icons to the actual ones.
*
* Note: The configured file extension needs a leading dot!
*
* @param string File extension including a leading dot, e.g ".jpg"
* @since 2016.01
* @category Developer
*/
$ext = $config->get( 'controller/common/media/standard/mimeicon/extension', '.png' );
return $mimedir . DIRECTORY_SEPARATOR . $mimetype . $ext;
}
/**
* Returns the mime type for the new image
*
* @param \Aimeos\MW\Media\Image\Iface $media Media object
* @param string $type Type of the image like "preview" or "files"
* @return string New mime type
* @throws \Aimeos\Controller\Common\Exception If no mime types are configured
*/
protected function getMimeType( \Aimeos\MW\Media\Image\Iface $media, $type )
{
$mimetype = $media->getMimetype();
$config = $this->context->getConfig();
/** controller/common/media/standard/files/allowedtypes
* A list of image mime types that are allowed for uploaded image files
*
* The list of allowed image types must be explicitly configured for the
* uploaded image files. Trying to upload and store an image file not
* available in the list of allowed mime types will result in an exception.
*
* @param array List of image mime types
* @since 2016.01
* @category Developer
* @category User
*/
/** controller/common/media/standard/preview/allowedtypes
* A list of image mime types that are allowed for preview image files
*
* The list of allowed image types must be explicitly configured for the
* preview image files. Trying to create a preview image whose mime type
* is not available in the list of allowed mime types will result in an
* exception.
*
* @param array List of image mime types
* @since 2016.01
* @category Developer
* @category User
*/
$default = array( 'image/jpeg', 'image/png', 'image/gif' );
$allowed = $config->get( 'controller/common/media/standard/' . $type . '/allowedtypes', $default );
if( in_array( $mimetype, $allowed ) === false )
{
if( ( $defaulttype = reset( $allowed ) ) === false ) {
throw new \Aimeos\Controller\Common\Exception( sprintf( 'No allowed image types configured for "%1$s"', $type ) );
}
return $defaulttype;
}
return $mimetype;
}
/**
* Scales the image according to the configuration settings
*
* @param \Aimeos\MW\Media\Image\Iface $media Media object
* @param string $type Type of the image like "preview" or "files"
* @param \Aimeos\MW\Media\Image\Iface Scaled media object
*/
protected function scaleImage( \Aimeos\MW\Media\Image\Iface $media, $type )
{
$config = $this->context->getConfig();
/** controller/common/media/standard/files/maxwidth
* Maximum width of the uploaded images
*
* The uploaded image files are scaled down if their width exceeds the
* configured width of pixels. If the image width in smaller than the
* configured one, no scaling happens. In case of a value of null or if
* no configuration for that option is available, the image width isn't
* scaled at all.
*
* The width/height ratio of the image is always kept.
*
* @param integer|null Width in pixel or null for no scaling
* @since 2016.01
* @category Developer
* @category User
*/
/** controller/common/media/standard/preview/maxwidth
* Maximum width of the preview images
*
* The preview image files are created with the configured width in
* pixel. If the original image width in smaller than the one configured
* for the preview image, the width of the original image is used. In
* case of a value of null or if no configuration for that option is
* available, the width of the preview image is the same as the width of
* the original image.
*
* The width/height ratio of the preview image is always the same as for
* the original image.
*
* @param integer|null Width in pixel or null for no scaling
* @since 2016.01
* @category Developer
* @category User
*/
$maxwidth = $config->get( 'controller/common/media/standard/' . $type . '/maxwidth', null );
/** controller/common/media/standard/files/maxheight
* Maximum height of the uploaded images
*
* The uploaded image files are scaled down if their height exceeds the
* configured height of pixels. If the image height in smaller than the
* configured one, no scaling happens. In case of a value of null or if
* no configuration for that option is available, the image width isn't
* scaled at all.
*
* The width/height ratio of the image is always kept.
*
* @param integer|null Height in pixel or null for no scaling
* @since 2016.01
* @category Developer
* @category User
*/
/** controller/common/media/standard/preview/maxheight
* Maximum height of the preview images
*
* The preview image files are created with the configured width in
* pixel. If the original image height in smaller than the one configured
* for the preview image, the height of the original image is used. In
* case of a value of null or if no configuration for that option is
* available, the height of the preview image is the same as the height
* of the original image.
*
* The width/height ratio of the preview image is always the same as for
* the original image.
*
* @param integer|null Height in pixel or null for no scaling
* @since 2016.01
* @category Developer
* @category User
*/
$maxheight = $config->get( 'controller/common/media/standard/' . $type . '/maxheight', null );
/** controller/common/media/standard/files/force-size
* Force exact image size for the uploaded images
*
* This configuration forces the output image to have exact the width
* and height specified in maxwidth / maxheight configuration options.
*
* With Imagick algorithm implementation, the image will be cropped with
* center gravity, in the default implementation the image will be
* stretched.
*
* @param integer Zero to disable the feature, one to enable it
* @since 2018.10
* @category Developer
* @category User
*/
/** controller/common/media/standard/preview/force-size
* Force exact image size for the preview images
*
* This configuration forces the output image to have exact the width
* and height specified in maxwidth / maxheight configuration options.
*
* With Imagick algorithm implementation, the image will be cropped with
* center gravity, in the default implementation the image will be
* stretched.
*
* @param integer Zero to disable the feature, one to enable it
* @since 2018.10
* @category Developer
* @category User
*/
$fit = $config->get( 'controller/common/media/standard/' . $type . '/force-size', null ) ? false : true;
if( $maxheight || $maxwidth ) {
return $media->scale( $maxwidth, $maxheight, $fit );
}
return $media;
}
/**
* Stores the file content
*
* @param string $content File content
* @param string $fsname Name of the file system to store the files at
* @param string $filepath Path of the new file
* @param string $oldpath Path of the old file
*/
protected function storeFile( $content, $fsname, $filepath, $oldpath )
{
$fs = $this->context->getFilesystemManager()->get( $fsname );
try
{
if( $oldpath !== '' && $oldpath !== $filepath && $fs->has( $oldpath ) ) {
$fs->rm( $oldpath );
}
}
catch( \Aimeos\MW\Filesystem\Exception $e ) {} // continue if removing file fails
$fs->write( $filepath, $content );
}
}