Passed
Push — master ( 658ed1...dfdd7b )
by Aimeos
05:32
created

Standard   F

Complexity

Total Complexity 73

Size/Duplication

Total Lines 682
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 161
dl 0
loc 682
rs 2.56
c 0
b 0
f 0
wmc 73

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A storeFile() 0 13 5
A getFilePath() 0 22 3
A addImages() 0 33 6
A getMediaFile() 0 25 1
B checkFileUpload() 0 21 9
A addPropertyTypes() 0 15 4
A scale() 0 10 2
B delete() 0 29 8
A getMimeType() 0 44 3
A add() 0 19 3
A store() 0 4 1
B createPreviews() 0 34 10
A getMimeIcon() 0 39 2
A copy() 0 36 6
A getContext() 0 3 1
A scaleImage() 0 112 3
A getFileContent() 0 23 5

How to fix   Complexity   

Complex Class

Complex classes like Standard often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Standard, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Media;
12
13
14
/**
15
 * Common media controller methods
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	implements \Aimeos\Controller\Common\Media\Iface
22
{
23
	private $context;
24
	private $types = [];
25
26
27
	/**
28
	 * Initializes the object
29
	 *
30
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
31
	 */
32
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
33
	{
34
		$this->context = $context;
35
	}
36
37
38
	/**
39
	 * Stores the uploaded file and adds the references to the media item
40
	 *
41
	 * {inheritDoc}
42
	 *
43
	 * @param \Aimeos\MShop\Media\Item\Iface $item Media item to add the file references to
44
	 * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file
45
	 * @param string $fsname Name of the file system to store the files at
46
	 * @return \Aimeos\MShop\Media\Item\Iface Added media item
47
	 */
48
	public function add( \Aimeos\MShop\Media\Item\Iface $item, \Psr\Http\Message\UploadedFileInterface $file, $fsname = 'fs-media' )
49
	{
50
		$this->checkFileUpload( $file );
51
		$media = $this->getMediaFile( $file->getStream() );
52
53
		if( $media instanceof \Aimeos\MW\Media\Image\Iface )
54
		{
55
			$item = $this->addImages( $item, $media, md5( $file->getClientFilename() ), $fsname );
56
		}
57
		else
58
		{
59
			$mimetype = $media->getMimeType();
60
			$filepath = $this->getFilePath( $file->getClientFilename(), 'files', $mimetype );
61
62
			$this->store( $filepath, $media->save(), $fsname );
63
			$item->setUrl( $filepath )->setPreview( $this->getMimeIcon( $mimetype ) )->setMimeType( $mimetype );
64
		}
65
66
		return $item->setLabel( $item->getLabel() ?: basename( $file->getClientFilename() ) );
67
	}
68
69
70
	/**
71
	 * Copies the media item and the referenced files
72
	 *
73
	 * @param \Aimeos\MShop\Media\Item\Iface $item Media item whose files should be copied
74
	 * @param string $fsname Name of the file system to delete the files from
75
	 * @return \Aimeos\MShop\Media\Item\Iface Copied media item with new files
76
	 */
77
	public function copy( \Aimeos\MShop\Media\Item\Iface $item, $fsname = 'fs-media' )
78
	{
79
		$manager = \Aimeos\MShop::create( $this->context, 'media' );
80
81
		$search = $manager->createSearch()->setSlice( 0, 1 );
82
		$search->setConditions( $search->compare( '==', 'media.url', $item->getUrl() ) );
83
84
		if( count( $manager->searchItems( $search ) ) > 0 ) {
85
			return $item;
86
		}
87
88
		$fs = $this->context->getFilesystemManager()->get( $fsname );
89
		$path = $item->getUrl();
90
91
		if( $fs->has( $path ) )
92
		{
93
			$newPath = $this->getFilePath( $path, 'files', $item->getMimeType() );
94
			$fs->copy( $path, $newPath );
95
			$item->setUrl( $newPath );
96
		}
97
98
		foreach( $item->getPreviews() as $preview )
99
		{
100
			if( $fs->has( $preview ) )
101
			{
102
				try
103
				{
104
					$newPath = $this->getFilePath( $preview, 'preview', pathinfo( $preview, PATHINFO_EXTENSION ) );
105
					$fs->copy( $preview, $newPath );
106
					$item->setPreview( $newPath );
107
				}
108
				catch( \Aimeos\MW\Filesystem\Exception $e ) {} // mime icons can't be copied
109
			}
110
		}
111
112
		return $item;
113
	}
114
115
116
	/**
117
	 * Deletes the files of the media item
118
	 *
119
	 * {inheritDoc}
120
	 *
121
	 * @param \Aimeos\MShop\Media\Item\Iface $item Media item whose files should be deleted
122
	 * @param string $fsname Name of the file system to delete the files from
123
	 */
124
	public function delete( \Aimeos\MShop\Media\Item\Iface $item, $fsname = 'fs-media' )
125
	{
126
		$manager = \Aimeos\MShop::create( $this->context, 'media' );
127
		$search = $manager->createSearch()->setSlice( 0, 2 );
128
		$search->setConditions( $search->compare( '==', 'media.url', $item->getUrl() ) );
129
130
		if( count( $manager->searchItems( $search ) ) > 1 ) {
131
			return $item->setUrl( '' )->setPreview( '' );
132
		}
133
134
		$fs = $this->context->getFilesystemManager()->get( $fsname );
135
		$path = $item->getUrl();
136
137
		if( $path !== '' && $fs->has( $path ) ) {
138
			$fs->rm( $path );
139
		}
140
141
		foreach( $item->getPreviews() as $preview )
142
		{
143
			try
144
			{
145
				if( $preview !== '' && $fs->has( $preview ) ) {
146
					$fs->rm( $preview );
147
				}
148
			}
149
			catch( \Exception $e ) { ; } // Can be a mime icon with relative path
150
		}
151
152
		return $item->setUrl( '' )->setPreview( '' )->deletePropertyItems( $item->getPropertyItems() );
153
	}
154
155
156
	/**
157
	 * Rescales the files (original and preview) referenced by the media item
158
	 *
159
	 * The height/width configuration for scaling and which one should be scaled is used from
160
	 * - controller/common/media/standard/<files|preview>/maxheight
161
	 * - controller/common/media/standard/<files|preview>/maxwidth
162
	 * - controller/common/media/standard/<files|preview>/scale
163
	 *
164
	 * @param \Aimeos\MShop\Media\Item\Iface $item Media item whose files should be scaled
165
	 * @param string $fsname Name of the file system to rescale the files from
166
	 * @return \Aimeos\MShop\Media\Item\Iface Rescaled media item
167
	 */
168
	public function scale( \Aimeos\MShop\Media\Item\Iface $item, $fsname = 'fs-media' )
169
	{
170
		$path = $item->getUrl();
171
		$media = $this->getMediaFile( $this->getFileContent( $path, $fsname ) );
172
173
		if( !( $media instanceof \Aimeos\MW\Media\Image\Iface ) ) {
174
			return $item;
175
		}
176
177
		return $this->addImages( $item, $media, $path, $fsname );
178
	}
179
180
181
	/**
182
	 * Adds original image and preview images to the media item
183
	 *
184
	 * @param \Aimeos\MShop\Media\Item\Iface $item Media item which will contains the image URLs afterwards
185
	 * @param \Aimeos\MW\Media\Image\Iface $media Image object to scale
186
	 * @param string $path Path to the file or URL, empty or random for uploaded files
187
	 * @param string $fsname File system name the file is located at
188
	 * @return \Aimeos\MShop\Media\Item\Iface Updated media item with URLs
189
	 */
190
	protected function addImages( \Aimeos\MShop\Media\Item\Iface $item, \Aimeos\MW\Media\Image\Iface $media, $path, $fsname )
191
	{
192
		$mime = $this->getMimeType( $media, 'files' );
193
		$mediaFile = $this->scaleImage( $media, 'files' );
194
195
		// Don't overwrite original files that are stored in linked directories
196
		$filepath = ( strncmp( $path, 'files/', 6 ) ? $this->getFilePath( $path, 'files', $mime ) : $path );
197
198
		$this->store( $filepath, $mediaFile->save( null, $mime ), $fsname );
199
		$item = $item->setUrl( $filepath )->setMimeType( $mime );
200
		unset( $mediaFile );
201
202
203
		$mediaFiles = $this->createPreviews( $media );
204
		$mime = $this->getMimeType( $media, 'preview' );
205
		$manager = \Aimeos\MShop::create( $this->context, 'media' );
206
207
		foreach( $mediaFiles as $type => $mediaFile )
208
		{
209
			if( ( $propItem = current( $item->getPropertyItems( $type ) ) ) === false ) {
210
				$propItem = $manager->createPropertyItem()->setType( $type );
211
			}
212
213
			$path = (string) $propItem->getValue();
214
			// Don't try to overwrite mime icons that are stored in another directory
215
			$filepath = ( strncmp( $path, 'preview/', 8 ) ? $this->getFilePath( $path, '', $mime ) : $path );
216
217
			$this->store( $filepath, $mediaFile->save( null, $mime ), $fsname );
218
			$item->addPropertyItem( $propItem->setValue( $filepath ) );
219
			unset( $mediaFile );
220
		}
221
222
		return $item->setPreview( current( $item->getPreviews() ) ?: '' );
223
	}
224
225
226
	/**
227
	 * Adds the used property types if necessary
228
	 *
229
	 * @param integer[] List of image widths
0 ignored issues
show
Bug introduced by
The type Aimeos\Controller\Common\Media\List was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
230
	 */
231
	protected function addPropertyTypes( $types )
232
	{
233
		foreach( $types as $type )
234
		{
235
			if( !isset( $this->types[$type] ) )
236
			{
237
				$manager = \Aimeos\MShop::create( $this->context, 'media/property/type' );
238
239
				try {
240
					$item = $manager->findItem( $type, [], 'media' );
241
				} catch( \Aimeos\MShop\Exception $e ) {
242
					$item = $manager->createItem()->setDomain( 'media' )->setCode( $type )->setLabel( $type . 'px width' );
243
				}
244
245
				$this->types[$type] = $manager->saveItem( $item )->getCode();
246
			}
247
		}
248
	}
249
250
251
	/**
252
	 * Checks if an error during upload occured
253
	 *
254
	 * @param \Psr\Http\Message\UploadedFileInterface $file Uploaded file
255
	 * @throws \Aimeos\Controller\Common\Exception If an error occured during upload
256
	 */
257
	protected function checkFileUpload( \Psr\Http\Message\UploadedFileInterface $file )
258
	{
259
		if( $file->getError() !== UPLOAD_ERR_OK )
260
		{
261
			switch( $file->getError() )
262
			{
263
				case UPLOAD_ERR_INI_SIZE:
264
				case UPLOAD_ERR_FORM_SIZE:
265
					throw new \Aimeos\Controller\Common\Exception( 'The uploaded file exceeds the max. allowed filesize' );
266
				case UPLOAD_ERR_PARTIAL:
267
					throw new \Aimeos\Controller\Common\Exception( 'The uploaded file was only partially uploaded' );
268
				case UPLOAD_ERR_NO_FILE:
269
					throw new \Aimeos\Controller\Common\Exception( 'No file was uploaded' );
270
				case UPLOAD_ERR_NO_TMP_DIR:
271
					throw new \Aimeos\Controller\Common\Exception( 'Temporary folder is missing' );
272
				case UPLOAD_ERR_CANT_WRITE:
273
					throw new \Aimeos\Controller\Common\Exception( 'Failed to write file to disk' );
274
				case UPLOAD_ERR_EXTENSION:
275
					throw new \Aimeos\Controller\Common\Exception( 'File upload stopped by extension' );
276
				default:
277
					throw new \Aimeos\Controller\Common\Exception( 'Unknown upload error' );
278
			}
279
		}
280
	}
281
282
283
	/**
284
	 * Creates scaled images according to the configuration settings
285
	 *
286
	 * @param \Aimeos\MW\Media\Image\Iface $media Media object
287
	 * @return \Aimeos\MW\Media\Image\Iface[] Associative list of image width as keys and scaled media object as values
288
	 */
289
	protected function createPreviews( \Aimeos\MW\Media\Image\Iface $media )
290
	{
291
		$list = [];
292
		$config = $this->context->getConfig();
293
294
		foreach( $config->get( 'controller/common/media/standard/previews', [] ) as $entry )
295
		{
296
			$maxwidth = ( isset( $entry['maxwidth'] ) ? (int) $entry['maxwidth'] : null );
297
			$maxheight = ( isset( $entry['maxheight'] ) ? (int) $entry['maxheight'] : null );
298
			$fit = ( isset( $entry['force-size'] ) ? (bool) $entry['force-size'] : false );
299
300
			if( $maxheight || $maxwidth )
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxwidth of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
Bug Best Practice introduced by
The expression $maxheight of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
301
			{
302
				$image = $media->scale( $maxwidth, $maxheight, !$fit );
303
				$list[$image->getWidth()] = $image;
304
			}
305
		}
306
307
		if( empty( $list ) )
308
		{
309
			$maxwidth = $config->get( 'controller/common/media/standard/preview/maxwidth', null );
310
			$maxheight = $config->get( 'controller/common/media/standard/preview/maxheight', null );
311
			$fit = (bool) $config->get( 'controller/common/media/standard/preview/force-size', false );
312
313
			if( $maxheight || $maxwidth )
314
			{
315
				$image = $media->scale( $maxwidth, $maxheight, !$fit );
316
				$list[$image->getWidth()] = $image;
317
			}
318
319
		}
320
321
		$this->addPropertyTypes( array_keys( $list ) );
322
		return $list;
323
	}
324
325
326
	/**
327
	 * Returns the context item
328
	 *
329
	 * @return \Aimeos\MShop\Context\Item\Iface Context item
330
	 */
331
	protected function getContext()
332
	{
333
		return $this->context;
334
	}
335
336
337
	/**
338
	 * Returns the file content of the file or URL
339
	 *
340
	 * @param string $path Path to the file or URL
341
	 * @param string $fsname File system name the file is located at
342
	 * @return string File content
343
	 * @throws \Aimeos\Controller\Common\Exception If no file is found
344
	 */
345
	protected function getFileContent( $path, $fsname )
346
	{
347
		if( $path !== '' )
348
		{
349
			if( preg_match( '#^[a-zA-Z]{1,10}://#', $path ) === 1 )
350
			{
351
				if( ( $content = @file_get_contents( $path ) ) === false )
352
				{
353
					$msg = sprintf( 'Downloading file "%1$s" failed', $path );
354
					throw new \Aimeos\Controller\Common\Exception( $msg );
355
				}
356
357
				return $content;
358
			}
359
360
			$fs = $this->context->getFilesystemManager()->get( $fsname );
361
362
			if( $fs->has( $path ) !== false ) {
363
				return $fs->read( $path );
364
			}
365
		}
366
367
		throw new \Aimeos\Controller\Common\Exception( sprintf( 'File "%1$s" not found', $path ) );
368
	}
369
370
371
	/**
372
	 * Creates a new file path from the given arguments and random values
373
	 *
374
	 * @param string $filename Original file name, can contain the path as well
375
	 * @param string $type File type, i.e. "files" or "preview"
376
	 * @param string $mimeext Mime type or extension of the file
377
	 * @return string New file name including the file path
378
	 * @deprecated 2020.01 $type will be removed
379
	 */
380
	protected function getFilePath( $filename, $type, $mimeext )
381
	{
382
		/** controller/common/media/standard/extensions
383
		 * Available files extensions for mime types of uploaded files
384
		 *
385
		 * Uploaded files should have the right file extension (e.g. ".jpg" for
386
		 * JPEG images) so files are recognized correctly if downloaded by users.
387
		 * The extension of the uploaded file can't be trusted and only its mime
388
		 * type can be determined automatically. This configuration setting
389
		 * provides the file extensions for the configured mime types. You can
390
		 * add more mime type / file extension combinations if required.
391
		 *
392
		 * @param array Associative list of mime types as keys and file extensions as values
393
		 * @since 2018.04
394
		 * @category Developer
395
		 */
396
		$list = $this->context->getConfig()->get( 'controller/common/media/standard/extensions', [] );
397
398
		$filename = md5( $filename . getmypid() . microtime( true ) );
399
		$ext = isset( $list[$mimeext] ) ? '.' . $list[$mimeext] : ( ctype_alpha( $mimeext ) ? '.' . $mimeext : '' );
400
401
		return "${type}/${filename[0]}/${filename[1]}/${filename}${ext}";
402
	}
403
404
405
	/**
406
	 * Returns the media object for the given file name
407
	 *
408
	 * @param string $file Path to the file or file content
409
	 * @return \Aimeos\MW\Media\Iface Media object
410
	 */
411
	protected function getMediaFile( $file )
412
	{
413
		/** controller/common/media/standard/options
414
		 * Options used for processing the uploaded media files
415
		 *
416
		 * When uploading a file, a preview image for that file is generated if
417
		 * possible (especially for images). You can configure certain options
418
		 * for the generated images, namely the implementation of the scaling
419
		 * algorithm and the quality of the resulting images with
420
		 *
421
		 *  array(
422
		 *  	'image' => array(
423
		 *  		'name' => 'Imagick',
424
		 *  		'quality' => 75,
425
		 *  	)
426
		 *  )
427
		 *
428
		 * @param array Multi-dimendional list of configuration options
429
		 * @since 2016.01
430
		 * @category Developer
431
		 * @category User
432
		 */
433
		$options = $this->context->getConfig()->get( 'controller/common/media/standard/options', [] );
434
435
		return \Aimeos\MW\Media\Factory::get( $file, $options );
436
	}
437
438
439
	/**
440
	 * Returns the relative path to the mime icon for the given mime type.
441
	 *
442
	 * @param string $mimetype Mime type like "image/png"
443
	 * @return string Relative path to the mime icon
444
	 */
445
	protected function getMimeIcon( $mimetype )
446
	{
447
		$config = $this->context->getConfig();
448
449
		/** controller/common/media/standard/mimeicon/directory
450
		 * Directory that contains the icons for the different mime types
451
		 *
452
		 * If no preview image can be generated from an uploaded file, an icon
453
		 * for its mime type is displayed instead. The directory for the mime
454
		 * icons is structured by the general mime type (e.g. "image") as
455
		 * sub-directory and the specific name of the mime type (e.g. "jpeg")
456
		 * as file name.
457
		 *
458
		 * Avoid leading and trailing slashes for the upload directory string!
459
		 *
460
		 * @param string Path or URL to the base directory
461
		 * @since 2016.01
462
		 * @category Developer
463
		 */
464
		if( ( $mimedir = $config->get( 'controller/common/media/standard/mimeicon/directory' ) ) == null ) {
465
			return '';
466
		}
467
468
		/** controller/common/media/standard/mimeicon/extension
469
		 * File extension of the mime icon images
470
		 *
471
		 * If you would like to use different mime icons that are available in
472
		 * another file format, you have to change the file extension for the
473
		 * mime icons to the actual ones.
474
		 *
475
		 * Note: The configured file extension needs a leading dot!
476
		 *
477
		 * @param string File extension including a leading dot, e.g ".jpg"
478
		 * @since 2016.01
479
		 * @category Developer
480
		 */
481
		$ext = $config->get( 'controller/common/media/standard/mimeicon/extension', '.png' );
482
483
		return $mimedir . DIRECTORY_SEPARATOR . $mimetype . $ext;
484
	}
485
486
487
	/**
488
	 * Returns the mime type for the new image
489
	 *
490
	 * @param \Aimeos\MW\Media\Image\Iface $media Media object
491
	 * @param string $type Type of the image like "preview" or "files"
492
	 * @return string New mime type
493
	 * @throws \Aimeos\Controller\Common\Exception If no mime types are configured
494
	 */
495
	protected function getMimeType( \Aimeos\MW\Media\Image\Iface $media, $type )
496
	{
497
		$mimetype = $media->getMimetype();
498
		$config = $this->context->getConfig();
499
500
		/** controller/common/media/standard/files/allowedtypes
501
		 * A list of image mime types that are allowed for uploaded image files
502
		 *
503
		 * The list of allowed image types must be explicitly configured for the
504
		 * uploaded image files. Trying to upload and store an image file not
505
		 * available in the list of allowed mime types will result in an exception.
506
		 *
507
		 * @param array List of image mime types
508
		 * @since 2016.01
509
		 * @category Developer
510
		 * @category User
511
		 */
512
513
		/** controller/common/media/standard/preview/allowedtypes
514
		 * A list of image mime types that are allowed for preview image files
515
		 *
516
		 * The list of allowed image types must be explicitly configured for the
517
		 * preview image files. Trying to create a preview image whose mime type
518
		 * is not available in the list of allowed mime types will result in an
519
		 * exception.
520
		 *
521
		 * @param array List of image mime types
522
		 * @since 2016.01
523
		 * @category Developer
524
		 * @category User
525
		 */
526
		$default = array( 'image/jpeg', 'image/png', 'image/gif', 'image/svg+xml' );
527
		$allowed = $config->get( 'controller/common/media/standard/' . $type . '/allowedtypes', $default );
528
529
		if( in_array( $mimetype, $allowed ) === false )
530
		{
531
			if( ( $defaulttype = reset( $allowed ) ) === false ) {
532
				throw new \Aimeos\Controller\Common\Exception( sprintf( 'No allowed image types configured for "%1$s"', $type ) );
533
			}
534
535
			return $defaulttype;
536
		}
537
538
		return $mimetype;
539
	}
540
541
542
	/**
543
	 * Scales the image according to the configuration settings
544
	 *
545
	 * @param \Aimeos\MW\Media\Image\Iface $media Media object
546
	 * @param string $type Type of the image like "preview" or "files"
547
	 * @return \Aimeos\MW\Media\Image\Iface Scaled media object
548
	 * @deprecated 2020.01 Will scale main file only
549
	 */
550
	protected function scaleImage( \Aimeos\MW\Media\Image\Iface $media, $type )
551
	{
552
		$config = $this->context->getConfig();
553
554
		/** controller/common/media/standard/files/maxwidth
555
		 * Maximum width of the uploaded images
556
		 *
557
		 * The uploaded image files are scaled down if their width exceeds the
558
		 * configured width of pixels. If the image width in smaller than the
559
		 * configured one, no scaling happens. In case of a value of null or if
560
		 * no configuration for that option is available, the image width isn't
561
		 * scaled at all.
562
		 *
563
		 * The width/height ratio of the image is always kept.
564
		 *
565
		 * @param integer|null Width in pixel or null for no scaling
566
		 * @since 2016.01
567
		 * @category Developer
568
		 * @category User
569
		 */
570
571
		/** controller/common/media/standard/preview/maxwidth
572
		 * Maximum width of the preview images
573
		 *
574
		 * The preview image files are created with the configured width in
575
		 * pixel. If the original image width in smaller than the one configured
576
		 * for the preview image, the width of the original image is used. In
577
		 * case of a value of null or if no configuration for that option is
578
		 * available, the width of the preview image is the same as the width of
579
		 * the original image.
580
		 *
581
		 * The width/height ratio of the preview image is always the same as for
582
		 * the original image.
583
		 *
584
		 * @param integer|null Width in pixel or null for no scaling
585
		 * @since 2016.01
586
		 * @category Developer
587
		 * @category User
588
		 */
589
		$maxwidth = $config->get( 'controller/common/media/standard/' . $type . '/maxwidth', null );
590
591
		/** controller/common/media/standard/files/maxheight
592
		 * Maximum height of the uploaded images
593
		 *
594
		 * The uploaded image files are scaled down if their height exceeds the
595
		 * configured height of pixels. If the image height in smaller than the
596
		 * configured one, no scaling happens. In case of a value of null or if
597
		 * no configuration for that option is available, the image width isn't
598
		 * scaled at all.
599
		 *
600
		 * The width/height ratio of the image is always kept.
601
		 *
602
		 * @param integer|null Height in pixel or null for no scaling
603
		 * @since 2016.01
604
		 * @category Developer
605
		 * @category User
606
		 */
607
608
		/** controller/common/media/standard/preview/maxheight
609
		 * Maximum height of the preview images
610
		 *
611
		 * The preview image files are created with the configured width in
612
		 * pixel. If the original image height in smaller than the one configured
613
		 * for the preview image, the height of the original image is used. In
614
		 * case of a value of null or if no configuration for that option is
615
		 * available, the height of the preview image is the same as the height
616
		 * of the original image.
617
		 *
618
		 * The width/height ratio of the preview image is always the same as for
619
		 * the original image.
620
		 *
621
		 * @param integer|null Height in pixel or null for no scaling
622
		 * @since 2016.01
623
		 * @category Developer
624
		 * @category User
625
		 */
626
		$maxheight = $config->get( 'controller/common/media/standard/' . $type . '/maxheight', null );
627
628
		/** controller/common/media/standard/files/force-size
629
		 * Force exact image size for the uploaded images
630
		 *
631
		 * This configuration forces the output image to have exact the width
632
		 * and height specified in maxwidth / maxheight configuration options
633
		 * by scaling and cropping the original image.
634
		 *
635
		 * @param integer True to enable the feature, false to disable it
636
		 * @since 2018.10
637
		 * @category Developer
638
		 * @category User
639
		 * @see controller/common/media/standard/preview/force-size
640
		 */
641
642
		/** controller/common/media/standard/preview/force-size
643
		 * Force exact image size for the preview images
644
		 *
645
		 * This configuration forces the output image to have exact the width
646
		 * and height specified in maxwidth / maxheight configuration options
647
		 * by scaling and cropping the original image.
648
		 *
649
		 * @param integer True to enable the feature, false to disable it
650
		 * @since 2018.10
651
		 * @category Developer
652
		 * @category User
653
		 * @see controller/common/media/standard/files/force-size
654
		 */
655
		$fit = (bool) $config->get( 'controller/common/media/standard/' . $type . '/force-size', false );
656
657
		if( $maxheight || $maxwidth ) {
658
			return $media->scale( $maxwidth, $maxheight, !$fit );
659
		}
660
661
		return $media;
662
	}
663
664
665
	/**
666
	 * Stores the file content
667
	 *
668
	 * @param string $filepath Path of the new file
669
	 * @param string $content File content
670
	 * @param string $fsname Name of the file system to store the files at
671
	 * @return \Aimeos\Controller\Common\Media\Iface Self object for fluent interface
672
	 */
673
	protected function store( $filepath, $content, $fsname )
674
	{
675
		$this->context->getFilesystemManager()->get( $fsname )->write( $filepath, $content );
676
		return $this;
677
	}
678
679
680
	/**
681
	 * Stores the file content
682
	 *
683
	 * @param string $content File content
684
	 * @param string $fsname Name of the file system to store the files at
685
	 * @param string $filepath Path of the new file
686
	 * @param string $oldpath Path of the old file
687
	 * @deprecated 2020.01
688
	 */
689
	protected function storeFile( $content, $fsname, $filepath, $oldpath )
690
	{
691
		$fs = $this->context->getFilesystemManager()->get( $fsname );
692
693
		try
694
		{
695
			if( $oldpath !== '' && $oldpath !== $filepath && $fs->has( $oldpath ) ) {
696
				$fs->rm( $oldpath );
697
			}
698
		}
699
		catch( \Aimeos\MW\Filesystem\Exception $e ) {} // continue if removing file fails
700
701
		$fs->write( $filepath, $content );
702
	}
703
}
704