|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\RemoteStorage\Plugin; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\Exception\FileSystemException; |
| 12 | +use Magento\Framework\Filesystem; |
| 13 | +use Magento\Framework\Filesystem\Directory\TargetDirectory; |
| 14 | +use Magento\Catalog\Block\Product\Gallery; |
| 15 | +use Magento\RemoteStorage\Model\Config; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class to copy the image to tmp folder |
| 19 | + */ |
| 20 | +class GalleryImageCopy |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var Filesystem\Directory\WriteInterface |
| 24 | + */ |
| 25 | + private $tmpDirectoryWrite; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var Filesystem\Directory\WriteInterface |
| 29 | + */ |
| 30 | + private $remoteDirectoryWrite; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var array |
| 34 | + */ |
| 35 | + private $tmpFiles = []; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var Config |
| 39 | + */ |
| 40 | + private Config $config; |
| 41 | + |
| 42 | + /** |
| 43 | + * @param Filesystem $filesystem |
| 44 | + * @param TargetDirectory $targetDirectory |
| 45 | + * @param Config $config |
| 46 | + * @throws FileSystemException |
| 47 | + */ |
| 48 | + public function __construct( |
| 49 | + Filesystem $filesystem, |
| 50 | + TargetDirectory $targetDirectory, |
| 51 | + Config $config |
| 52 | + ) { |
| 53 | + $this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP); |
| 54 | + $this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT); |
| 55 | + $this->config = $config; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Copy file from remote server to tmp directory of Magento to get Image width |
| 60 | + * |
| 61 | + * @param Gallery $subject |
| 62 | + * @param callable $proceed |
| 63 | + * @return string|null |
| 64 | + * @throws FileSystemException |
| 65 | + * @throws \Magento\Framework\Exception\RuntimeException |
| 66 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 67 | + */ |
| 68 | + public function aroundGetImageWidth(Gallery $subject, callable $proceed): string |
| 69 | + { |
| 70 | + return $this->copyFileToTmp($subject, $proceed); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Move files from storage to tmp folder |
| 75 | + * |
| 76 | + * @param Gallery $subject |
| 77 | + * @param callable $proceed |
| 78 | + * @return string|null |
| 79 | + * @throws FileSystemException |
| 80 | + * @throws \Magento\Framework\Exception\RuntimeException |
| 81 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 82 | + */ |
| 83 | + private function copyFileToTmp(Gallery $subject, callable $proceed): string |
| 84 | + { |
| 85 | + if ($this->config->IsEnabled()) { |
| 86 | + $filePath = $subject->getCurrentImage()->getPath(); |
| 87 | + if ($this->fileExistsInTmp($filePath)) { |
| 88 | + return $this->tmpFiles[$filePath]; |
| 89 | + } |
| 90 | + $absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath); |
| 91 | + if ($this->remoteDirectoryWrite->isFile($absolutePath)) { |
| 92 | + $this->tmpDirectoryWrite->create(); |
| 93 | + $tmpPath = $this->storeTmpName($filePath); |
| 94 | + $content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath); |
| 95 | + return $this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) |
| 96 | + ? $tmpPath |
| 97 | + : $filePath; |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Store created tmp image path |
| 104 | + * |
| 105 | + * @param string $filePath |
| 106 | + * @return string |
| 107 | + */ |
| 108 | + private function storeTmpName(string $filePath): string |
| 109 | + { |
| 110 | + // phpcs:ignore Magento2.Functions.DiscouragedFunction |
| 111 | + $tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath); |
| 112 | + $this->tmpFiles[$filePath] = $tmpPath; |
| 113 | + return $tmpPath; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Check is file exist in tmp folder |
| 118 | + * |
| 119 | + * @param string $filePath |
| 120 | + * @return bool |
| 121 | + */ |
| 122 | + private function fileExistsInTmp(string $filePath): bool |
| 123 | + { |
| 124 | + return array_key_exists($filePath, $this->tmpFiles); |
| 125 | + } |
| 126 | +} |
0 commit comments