Skip to content

Commit b650743

Browse files
committed
B2B-2028: [AWS S3] [Integration Tests]: Investigate Test Failures in CustomerCustomAttributes module
1 parent 177254d commit b650743

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}

app/code/Magento/RemoteStorage/etc/di.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProvider"/>
1212
<preference for="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactoryInterface" type="Magento\RemoteStorage\Driver\Adapter\MetadataProviderFactory"/>
1313
<preference for="Magento\Framework\Filesystem\DriverPool" type="Magento\RemoteStorage\Driver\DriverPool"/>
14+
<preference for="Magento\Framework\File\Uploader" type="Magento\RemoteStorage\Model\File\Uploader"/>
1415
<virtualType name="remoteWriteFactory" type="Magento\Framework\Filesystem\Directory\WriteFactory">
1516
<arguments>
1617
<argument name="driverPool" xsi:type="object">Magento\RemoteStorage\Driver\DriverPool</argument>
@@ -99,6 +100,9 @@
99100
<type name="Magento\Framework\Image\Adapter\AbstractAdapter">
100101
<plugin name="remoteImageFile" type="Magento\RemoteStorage\Plugin\Image" sortOrder="10"/>
101102
</type>
103+
<type name="Magento\Framework\Archive\Zip">
104+
<plugin name="remoteZipArchive" type="Magento\RemoteStorage\Plugin\Zip" sortOrder="10"/>
105+
</type>
102106
<type name="Magento\Catalog\Model\Category\FileInfo">
103107
<arguments>
104108
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>

0 commit comments

Comments
 (0)