|
| 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\Exception\RuntimeException; |
| 13 | +use Magento\Framework\Filesystem; |
| 14 | +use Magento\Framework\Filesystem\Directory\TargetDirectory; |
| 15 | +use Magento\Framework\Image\Adapter\AbstractAdapter; |
| 16 | +use Magento\RemoteStorage\Model\Config; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | +use Magento\Catalog\Model\Product\Option\Type\File\ExistingValidate as Subject; |
| 19 | + |
| 20 | +/** |
| 21 | + * @see AbstractAdapter |
| 22 | + */ |
| 23 | +class ExistingValidate |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @var Filesystem\Directory\WriteInterface |
| 27 | + */ |
| 28 | + private $tmpDirectoryWrite; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Filesystem\Directory\WriteInterface |
| 32 | + */ |
| 33 | + private $remoteDirectoryWrite; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + private $tmpFiles = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var bool |
| 42 | + */ |
| 43 | + private $isEnabled; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var LoggerInterface |
| 47 | + */ |
| 48 | + private $logger; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param Filesystem $filesystem |
| 52 | + * @param TargetDirectory $targetDirectory |
| 53 | + * @param Config $config |
| 54 | + * @param LoggerInterface $logger |
| 55 | + * @throws FileSystemException |
| 56 | + * @throws RuntimeException |
| 57 | + */ |
| 58 | + public function __construct( |
| 59 | + Filesystem $filesystem, |
| 60 | + TargetDirectory $targetDirectory, |
| 61 | + Config $config, |
| 62 | + LoggerInterface $logger |
| 63 | + ) { |
| 64 | + $this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP); |
| 65 | + $this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT); |
| 66 | + $this->isEnabled = $config->isEnabled(); |
| 67 | + $this->logger = $logger; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Copies file from the remote server to the tmp directory |
| 72 | + * |
| 73 | + * @param Subject $subject |
| 74 | + * @param $value |
| 75 | + * @param string|null $originalName |
| 76 | + * @return array |
| 77 | + * @throws FileSystemException |
| 78 | + * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
| 79 | + */ |
| 80 | + public function beforeIsValid(Subject $subject, $value, string $originalName = null) |
| 81 | + { |
| 82 | + if ($this->isEnabled) { |
| 83 | + $value = $this->copyFileToTmp($value); |
| 84 | + } |
| 85 | + return [$value, $originalName]; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Remove created tmp files |
| 90 | + */ |
| 91 | + public function __destruct() |
| 92 | + { |
| 93 | + try { |
| 94 | + foreach ($this->tmpFiles as $key => $tmpFile) { |
| 95 | + $this->tmpDirectoryWrite->delete($tmpFile); |
| 96 | + unset($this->tmpFiles[$key]); |
| 97 | + } |
| 98 | + } catch (\Exception $e) { |
| 99 | + $this->logger->error($e->getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Move files from storage to tmp folder |
| 105 | + * |
| 106 | + * @param string $filePath |
| 107 | + * @return string |
| 108 | + * @throws FileSystemException |
| 109 | + */ |
| 110 | + private function copyFileToTmp(string $filePath): string |
| 111 | + { |
| 112 | + if (isset($this->tmpFiles[$filePath])) { |
| 113 | + return $this->tmpFiles[$filePath]; |
| 114 | + } |
| 115 | + |
| 116 | + $absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath); |
| 117 | + if ($this->remoteDirectoryWrite->isFile($absolutePath)) { |
| 118 | + $this->tmpDirectoryWrite->create(); |
| 119 | + $tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath); |
| 120 | + $content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath); |
| 121 | + if ($this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) >= 0) { |
| 122 | + $filePath = $tmpPath; |
| 123 | + $this->tmpFiles[$tmpPath] = $tmpPath; |
| 124 | + } |
| 125 | + } |
| 126 | + return $filePath; |
| 127 | + } |
| 128 | +} |
0 commit comments