Skip to content

Commit be69d98

Browse files
Feedback changes
1 parent 07b83ad commit be69d98

File tree

8 files changed

+75
-69
lines changed

8 files changed

+75
-69
lines changed

MediaGalleryIntegration/Plugin/SaveBaseCategoryImageInformation.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
1515
use Magento\MediaGallerySynchronization\Model\CreateAssetFromFile;
1616
use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory;
17+
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
1718

1819
/**
1920
* Save base category image by SaveAssetsInterface.
@@ -49,13 +50,18 @@ class SaveBaseCategoryImageInformation
4950
* @var Storage
5051
*/
5152
private $storage;
53+
/**
54+
* @var GenerateRenditionsInterface
55+
*/
56+
private $generateRenditions;
5257

5358
/**
5459
* @param DeleteAssetsByPathsInterface $deleteAssetsByPath
5560
* @param GetAssetsByPathsInterface $getAssetsByPaths
5661
* @param SplFileInfoFactory $splFileInfoFactory
5762
* @param CreateAssetFromFile $createAssetFromFile
5863
* @param SaveAssetsInterface $saveAsset
64+
* @param GenerateRenditionsInterface $generateRenditions
5965
* @param Storage $storage
6066
*/
6167
public function __construct(
@@ -64,6 +70,7 @@ public function __construct(
6470
SplFileInfoFactory $splFileInfoFactory,
6571
CreateAssetFromFile $createAssetFromFile,
6672
SaveAssetsInterface $saveAsset,
73+
GenerateRenditionsInterface $generateRenditions,
6774
Storage $storage
6875
) {
6976
$this->deleteAssetsByPaths = $deleteAssetsByPath;
@@ -72,6 +79,7 @@ public function __construct(
7279
$this->createAssetFromFile = $createAssetFromFile;
7380
$this->saveAsset = $saveAsset;
7481
$this->storage = $storage;
82+
$this->generateRenditions = $generateRenditions;
7583
}
7684

7785
/**
@@ -93,7 +101,7 @@ public function afterMoveFileFromTmp(ImageUploader $subject, string $imagePath):
93101

94102
$this->saveAsset->execute([$this->createAssetFromFile->execute($file)]);
95103
$this->storage->resizeFile($absolutePath);
96-
104+
$this->generateRenditions->execute([$file]);
97105
return $imagePath;
98106
}
99107
}

MediaGalleryIntegration/Plugin/SaveImageInformation.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Psr\Log\LoggerInterface;
1717
use Magento\Framework\Filesystem;
1818
use Magento\Framework\App\Filesystem\DirectoryList;
19+
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
1920

2021
/**
2122
* Save image information by SaveAssetsInterface.
@@ -58,6 +59,10 @@ class SaveImageInformation
5859
* @var Filesystem
5960
*/
6061
private $filesystem;
62+
/**
63+
* @var GenerateRenditionsInterface
64+
*/
65+
private $generateRenditions;
6166

6267
/**
6368
* @param Filesystem $filesystem
@@ -66,6 +71,7 @@ class SaveImageInformation
6671
* @param SplFileInfoFactory $splFileInfoFactory
6772
* @param CreateAssetFromFile $createAssetFromFile
6873
* @param SaveAssetsInterface $saveAsset
74+
* @param GenerateRenditionsInterface $generateRenditions
6975
* @param Storage $storage
7076
*/
7177
public function __construct(
@@ -75,6 +81,7 @@ public function __construct(
7581
SplFileInfoFactory $splFileInfoFactory,
7682
CreateAssetFromFile $createAssetFromFile,
7783
SaveAssetsInterface $saveAsset,
84+
GenerateRenditionsInterface $generateRenditions,
7885
Storage $storage
7986
) {
8087
$this->log = $log;
@@ -84,25 +91,28 @@ public function __construct(
8491
$this->saveAsset = $saveAsset;
8592
$this->storage = $storage;
8693
$this->filesystem = $filesystem;
94+
$this->generateRenditions = $generateRenditions;
8795
}
8896

8997
/**
9098
* Saves asset to media gallery after save image.
9199
*
92100
* @param Uploader $subject
93101
* @param array $result
94-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
95102
* @return array
103+
* @throws \Magento\Framework\Exception\CouldNotSaveException
104+
* @throws \Magento\Framework\Exception\ValidatorException
105+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
96106
*/
97107
public function afterSave(Uploader $subject, array $result): array
98108
{
99-
$file = $this->splFileInfoFactory->create($result['path'] . '/' . $result['file']);
109+
$file = $this->splFileInfoFactory->create($result['path'] . DIRECTORY_SEPARATOR . $result['file']);
100110
if (!$this->isApplicable($file->getPathName())) {
101111
return $result;
102112
}
103113
$this->saveAsset->execute([$this->createAssetFromFile->execute($file)]);
104-
$this->storage->resizeFile($result['path'] . '/' . $result['file']);
105-
114+
$this->storage->resizeFile($result['path'] . DIRECTORY_SEPARATOR . $result['file']);
115+
$this->generateRenditions->execute([$file]);
106116
return $result;
107117
}
108118

MediaGalleryRenditions/Model/GenerateRenditions.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\MediaGalleryRenditionsApi\Api\GenerateRenditionsInterface;
1212
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
1313
use Magento\MediaGalleryRenditionsApi\Model\ConfigInterface;
14+
use Magento\MediaGallerySynchronization\Model\CreateAssetFromFile;
1415

1516
class GenerateRenditions implements GenerateRenditionsInterface
1617
{
@@ -29,30 +30,43 @@ class GenerateRenditions implements GenerateRenditionsInterface
2930
*/
3031
private $getRenditionPath;
3132

33+
/**
34+
* @var CreateAssetFromFile
35+
*/
36+
private $createAssetFromFile;
37+
3238
/**
3339
* GenerateRenditions constructor.
3440
* @param AdapterFactory $imageFactory
3541
* @param GetRenditionPathInterface $getRenditionPath
42+
* @param CreateAssetFromFile $createAssetFromFile
3643
* @param ConfigInterface $config
3744
*/
3845
public function __construct(
3946
AdapterFactory $imageFactory,
4047
GetRenditionPathInterface $getRenditionPath,
48+
CreateAssetFromFile $createAssetFromFile,
4149
ConfigInterface $config
4250
) {
4351
$this->imageFactory = $imageFactory;
4452
$this->config = $config;
4553
$this->getRenditionPath = $getRenditionPath;
54+
$this->createAssetFromFile = $createAssetFromFile;
4655
}
4756

4857
/**
4958
* @inheritDoc
5059
*/
51-
public function execute(string $path): void
60+
public function execute(array $files): void
5261
{
53-
$isResizeable = $this->isResizeable($path);
54-
if ($isResizeable) {
55-
$renditionImagePath = $this->getRenditionPath->execute($path);
62+
foreach ($files as $file) {
63+
$path = $file->getPath() . DIRECTORY_SEPARATOR . $file->getFileName();
64+
$asset = $this->createAssetFromFile->execute($file);
65+
if (!$this->isResizeable($asset->getHeight(), $asset->getWidth())) {
66+
continue;
67+
}
68+
$renditionDirectoryPath = $this->getRenditionPath->execute($asset);
69+
$renditionImagePath = $renditionDirectoryPath . DIRECTORY_SEPARATOR . $file->getFileName();
5670
$image = $this->imageFactory->create();
5771
$image->open($path);
5872
$image->keepAspectRatio(true);
@@ -64,12 +78,12 @@ public function execute(string $path): void
6478
/**
6579
* Check if image needs to resize or not
6680
*
67-
* @param string $path
81+
* @param int $height
82+
* @param int $width
6883
* @return bool
6984
*/
70-
private function isResizeable(string $path) :bool
85+
private function isResizeable(int $height, int $width) :bool
7186
{
72-
[$width, $height] = getimagesize($path);
7387
return $width > $this->getResizedWidth() || $height > $this->getResizedHeight();
7488
}
7589

MediaGalleryRenditions/Model/GetRenditionPath.php

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Magento\Framework\Exception\LocalizedException;
1414
use Magento\Framework\Filesystem;
1515
use Magento\Framework\Filesystem\DriverInterface;
16-
use Magento\Framework\Filesystem\Io\File;
16+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1717
use Magento\MediaGalleryRenditionsApi\Api\GetRenditionPathInterface;
1818

1919
class GetRenditionPath implements GetRenditionPathInterface
@@ -23,11 +23,6 @@ class GetRenditionPath implements GetRenditionPathInterface
2323
*/
2424
private $directory;
2525

26-
/**
27-
* @var File|mixed|null
28-
*/
29-
private $ioFile;
30-
3126
/**
3227
* @var DriverInterface|mixed|null
3328
*/
@@ -37,65 +32,45 @@ class GetRenditionPath implements GetRenditionPathInterface
3732

3833
/**
3934
* @param Filesystem $filesystem
40-
* @param File|null $ioFile
4135
* @param DriverInterface|null $file
4236
* @throws FileSystemException
4337
*/
4438
public function __construct(
4539
Filesystem $filesystem,
46-
File $ioFile = null,
4740
DriverInterface $file = null
4841
) {
4942
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
50-
$this->ioFile = $ioFile ?: ObjectManager::getInstance()->get(File::class);
5143
$this->file = $file ?: ObjectManager::getInstance()->get(Filesystem\Driver\File::class);
5244
}
5345

5446
/**
5547
* Returns Rendition image path
5648
*
57-
* @param string $path
49+
* @param AssetInterface $asset
5850
* @return string
5951
* @throws LocalizedException
60-
* @throws FileSystemException
6152
*/
62-
public function execute(string $path) :string
53+
public function execute(AssetInterface $asset) :string
6354
{
64-
$realPath = $this->directory->getRelativePath($path);
65-
if (!$this->directory->isFile($realPath) || !$this->directory->isExist($realPath)) {
66-
throw new LocalizedException(__('Directory or File %1 does not exist in media directory.', $realPath));
67-
}
68-
$renditionImageDirectoryPath = $this->getRenditionsImageDirectory($path);
69-
$renditionImageDirectory = $this->directory->getRelativePath($renditionImageDirectoryPath);
70-
if (!$this->directory->isExist($renditionImageDirectory)) {
71-
$this->directory->create($renditionImageDirectory);
72-
}
73-
if (!$this->directory->isExist($renditionImageDirectory)) {
74-
throw new LocalizedException(__(
75-
'Directory %1 does not exist in media directory.',
76-
$renditionImageDirectory
77-
));
78-
}
79-
return $renditionImageDirectoryPath . '/' . $this->ioFile->getPathInfo($path)['basename'];
55+
return $this->getRenditionsImageDirectory($asset->getPath());
8056
}
8157

8258
/**
8359
* Return renditions directory path for file/current directory
8460
*
85-
* @param bool|string $filePath Path to the file
61+
* @param string $filePath Path to the file
8662
* @return string
8763
*/
88-
private function getRenditionsImageDirectory($filePath = false) :string
64+
private function getRenditionsImageDirectory(string $filePath) :string
8965
{
9066
$renditionRootDir = $this->getRenditionsRoot();
9167

9268
if ($filePath) {
93-
$renditionImagePath = $this->getRenditionsPath($filePath, false);
69+
$renditionImagePath = $this->getRenditionsPath($filePath);
9470
if ($renditionImagePath) {
9571
$renditionImageDir = $this->file->getParentDirectory($renditionImagePath);
9672
}
9773
}
98-
9974
return $renditionImageDir ?? $renditionRootDir;
10075
}
10176

@@ -113,23 +88,22 @@ private function getRenditionsRoot() :string
11388
* Renditions path getter
11489
*
11590
* @param string $filePath original file path
116-
* @param bool $checkFile OPTIONAL is it necessary to check file availability
11791
* @return string|false
11892
*/
119-
private function getRenditionsPath($filePath, $checkFile = false)
93+
private function getRenditionsPath($filePath) :?string
12094
{
12195
$mediaRootDir = $this->directory->getAbsolutePath('');
12296
if (strpos($filePath, (string) $mediaRootDir) === 0) {
12397
$relativeFilePath = substr($filePath, strlen($mediaRootDir));
12498
// phpcs:ignore Magento2.Functions.DiscouragedFunction
125-
$renditionPath = $relativeFilePath === basename($filePath)
126-
? $this->getRenditionsRoot() . DIRECTORY_SEPARATOR . $relativeFilePath
127-
: $this->getRenditionsRoot() . $relativeFilePath;
128-
if (!$checkFile || $this->directory->isExist($this->directory->getRelativePath($renditionPath))) {
129-
return $renditionPath;
99+
$renditionPath = $this->getRenditionsRoot();
100+
if ($relativeFilePath === basename($filePath)) {
101+
$renditionPath .= DIRECTORY_SEPARATOR;
130102
}
103+
$renditionPath .= $relativeFilePath;
104+
return $renditionPath;
131105
}
132106

133-
return false;
107+
return null;
134108
}
135109
}

MediaGalleryRenditionsApi/Api/GenerateRenditionsInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ interface GenerateRenditionsInterface
1414
/**
1515
* Generate Renditions image
1616
*
17-
* @param string $path
17+
* @param array $paths
1818
* @throws LocalizedException
1919
*/
20-
public function execute(string $path): void;
20+
public function execute(array $paths): void;
2121
}

MediaGalleryRenditionsApi/Api/GetRenditionPathInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
namespace Magento\MediaGalleryRenditionsApi\Api;
99

1010
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
1112

1213
interface GetRenditionPathInterface
1314
{
1415
/**
1516
* Get Renditions image path
1617
*
17-
* @param string $path
18+
* @param AssetInterface $asset
1819
* @return string
1920
* @throws LocalizedException
2021
*/
21-
public function execute(string $path): string;
22+
public function execute(AssetInterface $asset): string;
2223
}

0 commit comments

Comments
 (0)