|
| 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\Model\File; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\App\ObjectManager; |
| 12 | +use Magento\Framework\Exception\FileSystemException; |
| 13 | +use Magento\Framework\File\Mime; |
| 14 | +use Magento\Framework\Filesystem; |
| 15 | +use Magento\Framework\Filesystem\Directory\TargetDirectory; |
| 16 | +use Magento\Framework\Filesystem\DriverPool; |
| 17 | +use Magento\RemoteStorage\Model\Config; |
| 18 | + |
| 19 | +/** |
| 20 | + * Uploader class for cases when remote storage is enabled and the uploaded file is located on remote storage |
| 21 | + */ |
| 22 | +class Uploader extends \Magento\Framework\File\Uploader |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var Filesystem\Directory\WriteInterface |
| 26 | + */ |
| 27 | + private $tmpDirectoryWrite; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var Filesystem\Directory\WriteInterface |
| 31 | + */ |
| 32 | + private $remoteDirectoryWrite; |
| 33 | + |
| 34 | + /** |
| 35 | + * Copies file to the tmp directory if remote storage is enabled and tmp file is located on remote storage |
| 36 | + * |
| 37 | + * {@inheritDoc} |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + $fileId, |
| 41 | + Mime $fileMime = null, |
| 42 | + DirectoryList $directoryList = null, |
| 43 | + DriverPool $driverPool = null, |
| 44 | + TargetDirectory $targetDirectory = null, |
| 45 | + Filesystem $filesystem = null |
| 46 | + ) { |
| 47 | + $targetDirectory = $targetDirectory ?: ObjectManager::getInstance()->get(TargetDirectory::class); |
| 48 | + $filesystem = $filesystem ?: ObjectManager::getInstance()->get(FileSystem::class); |
| 49 | + $config = ObjectManager::getInstance()->get(Config::class); |
| 50 | + |
| 51 | + if ($config->isEnabled() && isset($fileId['tmp_name'])) { |
| 52 | + $this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP); |
| 53 | + $this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT); |
| 54 | + |
| 55 | + $fileId['tmp_name'] = $this->copyFileToTmp($fileId['tmp_name']); |
| 56 | + } |
| 57 | + |
| 58 | + parent::__construct($fileId, $fileMime, $directoryList, $driverPool, $targetDirectory, $filesystem); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Moves file from the remote storage to the tmp folder |
| 63 | + * |
| 64 | + * @param string $filePath |
| 65 | + * @return string |
| 66 | + * @throws FileSystemException |
| 67 | + */ |
| 68 | + private function copyFileToTmp(string $filePath): string |
| 69 | + { |
| 70 | + $absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath); |
| 71 | + if ($this->remoteDirectoryWrite->isFile($absolutePath)) { |
| 72 | + $this->tmpDirectoryWrite->create(); |
| 73 | + $tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath); |
| 74 | + $content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath); |
| 75 | + $this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content); |
| 76 | + |
| 77 | + return $tmpPath; |
| 78 | + } |
| 79 | + |
| 80 | + return $filePath; |
| 81 | + } |
| 82 | +} |
0 commit comments