tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP); $this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT); $this->isEnabled = $config->isEnabled(); $this->logger = $logger; } /** * Removes created tmp files */ public function __destruct() { try { foreach ($this->tmpFiles as $key => $tmpFile) { $this->tmpDirectoryWrite->delete($tmpFile); unset($this->tmpFiles[$key]); } } catch (\Exception $e) { $this->logger->error($e->getMessage()); } } /** * Moves file from the remote storage to tmp folder * * @param string $filePath * @return string * @throws FileSystemException */ public function copy(string $filePath): string { if (!$this->isEnabled) { return $filePath; } if (isset($this->tmpFiles[$filePath])) { return $this->tmpFiles[$filePath]; } $absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath); if ($this->remoteDirectoryWrite->isFile($absolutePath)) { $this->tmpDirectoryWrite->create(); // phpcs:ignore Magento2.Functions.DiscouragedFunction $tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath); $content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath); if ($this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) >= 0) { $filePath = $tmpPath; $this->tmpFiles[$tmpPath] = $tmpPath; } } return $filePath; } }