Skip to content

Commit 9728fe3

Browse files
authored
Merge pull request #7020 from magento-arcticfoxes/B2B-1789-test
B2B-1789: The first Request For Storefront Image After Deleting Local File System Cached Images Results In Magento Placeholder Image When Using S3
2 parents d1ba944 + 93811f8 commit 9728fe3

File tree

7 files changed

+140
-24
lines changed

7 files changed

+140
-24
lines changed

app/code/Magento/Backend/Test/Mftf/Helper/CurlHelpers.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ public function assertImageContentIsEqual(
5757
): void {
5858
$cookie = $this->getCookie($cookieName);
5959
$imageContent = $this->getCurlResponse($url, $cookie, $postBody);
60-
// Must make request twice until bug is resolved: B2B-1789
61-
$imageContent = $this->getCurlResponse($url, $cookie, $postBody);
6260
// md5() here is not for cryptographic use.
6361
// phpcs:ignore Magento2.Security.InsecureFunction
6462
$imageContentMD5 = md5($imageContent);

app/code/Magento/MediaStorage/App/Media.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,18 @@ public function launch(): ResponseInterface
216216
*/
217217
private function createLocalCopy(): void
218218
{
219-
$this->syncFactory->create(['directory' => $this->directoryPub])
220-
->synchronize($this->relativeFileName);
219+
$synchronizer = $this->syncFactory->create(['directory' => $this->directoryPub]);
220+
$synchronizer->synchronize($this->relativeFileName);
221221

222222
if ($this->directoryPub->isReadable($this->relativeFileName)) {
223223
return;
224224
}
225225

226226
if ($this->mediaUrlFormat === CatalogMediaConfig::HASH) {
227227
$this->imageResize->resizeFromImageName($this->getOriginalImage($this->relativeFileName));
228+
if (!$this->directoryPub->isReadable($this->relativeFileName)) {
229+
$synchronizer->synchronize($this->relativeFileName);
230+
}
228231
}
229232
}
230233

app/code/Magento/MediaStorage/Model/File/Storage/Synchronization.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\MediaStorage\Model\File\Storage;
79

810
use Magento\Framework\Exception\FileSystemException;
911
use Magento\Framework\Filesystem\Directory\WriteInterface as DirectoryWrite;
1012
use Magento\Framework\Filesystem\File\WriteInterface;
11-
use Magento\MediaStorage\Service\ImageResize;
12-
use Magento\MediaStorage\Model\File\Storage\Database;
1313

1414
/**
15-
* Class Synchronization
15+
* Synchronize files from Db storage to local file system
1616
*/
1717
class Synchronization
1818
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\MediaStorage\Model\File\Storage;
9+
10+
use Magento\Framework\ObjectManagerInterface;
11+
12+
/**
13+
* Factory class for @see \Magento\MediaStorage\Model\File\Storage\Synchronization
14+
*/
15+
class SynchronizationFactory
16+
{
17+
/**
18+
* Object Manager instance
19+
*
20+
* @var ObjectManagerInterface
21+
*/
22+
private $objectManager = null;
23+
24+
/**
25+
* Instance name to create
26+
*
27+
* @var string
28+
*/
29+
private $instanceName = null;
30+
31+
/**
32+
* Factory constructor
33+
*
34+
* @param ObjectManagerInterface $objectManager
35+
* @param string $instanceName
36+
*/
37+
public function __construct(
38+
ObjectManagerInterface $objectManager,
39+
string $instanceName = '\\Magento\\MediaStorage\\Model\\File\\Storage\\Synchronization'
40+
) {
41+
$this->objectManager = $objectManager;
42+
$this->instanceName = $instanceName;
43+
}
44+
45+
/**
46+
* Create class instance with specified parameters
47+
*
48+
* @param array $data
49+
*/
50+
public function create(array $data = [])
51+
{
52+
return $this->objectManager->create($this->instanceName, $data);
53+
}
54+
}

app/code/Magento/RemoteStorage/Plugin/MediaStorage.php renamed to app/code/Magento/RemoteStorage/Model/File/Storage/Synchronization.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\RemoteStorage\Plugin;
8+
namespace Magento\RemoteStorage\Model\File\Storage;
99

1010
use Magento\Framework\App\Filesystem\DirectoryList;
1111
use Magento\Framework\Exception\FileSystemException;
1212
use Magento\Framework\Exception\RuntimeException;
13-
use Magento\Framework\Exception\ValidatorException;
1413
use Magento\Framework\Filesystem\Directory\WriteInterface;
15-
use Magento\MediaStorage\Model\File\Storage\Synchronization;
1614
use Magento\RemoteStorage\Driver\DriverPool as RemoteDriverPool;
1715
use Magento\Framework\Filesystem\DriverPool as LocalDriverPool;
1816
use Magento\RemoteStorage\Model\Config;
1917
use Magento\RemoteStorage\Filesystem;
2018

2119
/**
22-
* Modifies the base URL.
20+
* Synchronize files from remote to local file system.
2321
*/
24-
class MediaStorage
22+
class Synchronization
2523
{
2624
/**
2725
* @var bool
@@ -47,21 +45,21 @@ class MediaStorage
4745
public function __construct(Config $config, Filesystem $filesystem)
4846
{
4947
$this->isEnabled = $config->isEnabled();
50-
$this->remoteDirectory = $filesystem->getDirectoryWrite(DirectoryList::PUB, RemoteDriverPool::REMOTE);
51-
$this->localDirectory = $filesystem->getDirectoryWrite(DirectoryList::PUB, LocalDriverPool::FILE);
48+
$this->remoteDirectory = $filesystem->getDirectoryWrite(
49+
DirectoryList::PUB, RemoteDriverPool::REMOTE
50+
);
51+
$this->localDirectory = $filesystem->getDirectoryWrite(
52+
DirectoryList::PUB, LocalDriverPool::FILE
53+
);
5254
}
5355

5456
/**
55-
* Download remote file
56-
*
57-
* @param Synchronization $subject
57+
* Synchronize files from remote to local file system.
5858
* @param string $relativeFileName
59-
* @return null
60-
* @throws FileSystemException
61-
* @throws ValidatorException
62-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
59+
* @return void
60+
* @throws \LogicException
6361
*/
64-
public function beforeSynchronize(Synchronization $subject, string $relativeFileName): void
62+
public function synchronize($relativeFileName)
6563
{
6664
if ($this->isEnabled && $this->remoteDirectory->isExist($relativeFileName)) {
6765
$file = $this->localDirectory->openFile($relativeFileName, 'w');
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\File\Storage;
9+
10+
use Magento\RemoteStorage\Model\File\Storage\Synchronization;
11+
use Magento\MediaStorage\Model\File\Storage\SynchronizationFactory as MediaSynchronizationFactory;
12+
use Magento\RemoteStorage\Model\Config;
13+
use Magento\Framework\ObjectManagerInterface;
14+
15+
/**
16+
* This is a plugin to Magento\MediaStorage\Model\File\Storage\SynchronizationFactory.
17+
*/
18+
class SynchronizationFactory
19+
{
20+
/**
21+
* Object Manager instance
22+
*
23+
* @var ObjectManagerInterface
24+
*/
25+
private $objectManager = null;
26+
27+
/**
28+
* @var Config
29+
*/
30+
private $config;
31+
32+
/**
33+
* Factory constructor
34+
*
35+
* @param ObjectManagerInterface $objectManager
36+
* @param Config $config
37+
*/
38+
public function __construct(ObjectManagerInterface $objectManager, Config $config)
39+
{
40+
$this->objectManager = $objectManager;
41+
$this->config = $config;
42+
}
43+
44+
/**
45+
* Create remote synchronization instance if remote storage is enabled.
46+
* Otherwise, defer creation to MediaSynchronizationFactory
47+
*
48+
* @param MediaSynchronizationFactory $subject
49+
* @param callable $proceed
50+
* @param array $data
51+
* @return mixed
52+
* @throws \Magento\Framework\Exception\FileSystemException
53+
* @throws \Magento\Framework\Exception\RuntimeException
54+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
55+
*/
56+
public function aroundCreate(MediaSynchronizationFactory $subject, callable $proceed, array $data = [])
57+
{
58+
if ($this->config->isEnabled()) {
59+
return $this->objectManager->create(Synchronization::class, $data);
60+
}
61+
return $proceed($data);
62+
}
63+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
<argument name="filesystem" xsi:type="object">fullRemoteFilesystem</argument>
7676
</arguments>
7777
</type>
78-
<type name="Magento\MediaStorage\Model\File\Storage\Synchronization">
79-
<plugin name="remoteMedia" type="Magento\RemoteStorage\Plugin\MediaStorage" />
78+
<type name="Magento\MediaStorage\Model\File\Storage\SynchronizationFactory">
79+
<plugin name="remoteMediaStorageSynchronizationFactory" type="Magento\RemoteStorage\Plugin\File\Storage\SynchronizationFactory" />
8080
</type>
8181
<type name="Magento\Framework\Data\Collection\Filesystem">
8282
<arguments>

0 commit comments

Comments
 (0)