Skip to content

Commit 1ab7166

Browse files
MC-38112: Add synchronization mechanism between local storage and remote storage (#357)
1 parent d553c3c commit 1ab7166

File tree

30 files changed

+423
-448
lines changed

30 files changed

+423
-448
lines changed

app/code/Magento/AwsS3/Driver/AwsS3.php

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\AwsS3\Driver;
99

1010
use Exception;
11+
use Generator;
1112
use League\Flysystem\AdapterInterface;
1213
use League\Flysystem\Config;
1314
use Magento\Framework\Exception\FileSystemException;
@@ -165,7 +166,7 @@ private function createDirectoryRecursively(string $path): bool
165166
$this->createDirectoryRecursively($parentDir);
166167
}
167168

168-
return (bool)$this->adapter->createDir(rtrim($path, '/'), new Config([]));
169+
return (bool)$this->adapter->createDir(rtrim($path, '/'), new Config(self::CONFIG));
169170
}
170171

171172
/**
@@ -205,8 +206,16 @@ public function deleteDirectory($path): bool
205206
public function filePutContents($path, $content, $mode = null): int
206207
{
207208
$path = $this->normalizeRelativePath($path);
209+
$config = self::CONFIG;
208210

209-
return $this->adapter->write($path, $content, new Config(self::CONFIG))['size'];
211+
if (false !== ($imageSize = @getimagesizefromstring($content))) {
212+
$config['Metadata'] = [
213+
'image-width' => $imageSize[0],
214+
'image-height' => $imageSize[1]
215+
];
216+
}
217+
218+
return $this->adapter->write($path, $content, new Config($config))['size'];
210219
}
211220

212221
/**
@@ -461,16 +470,6 @@ public function getMetadata(string $path): array
461470
throw new FileSystemException(__('Cannot gather meta info! %1', [$this->getWarningMessage()]));
462471
}
463472

464-
$extra = [
465-
'image-width' => 0,
466-
'image-height' => 0
467-
];
468-
469-
if (isset($metaInfo['image-width'], $metaInfo['image-height'])) {
470-
$extra['image-width'] = $metaInfo['image-width'];
471-
$extra['image-height'] = $metaInfo['image-height'];
472-
}
473-
474473
return [
475474
'path' => $metaInfo['path'],
476475
'dirname' => $metaInfo['dirname'],
@@ -480,7 +479,10 @@ public function getMetadata(string $path): array
480479
'timestamp' => $metaInfo['timestamp'],
481480
'size' => $metaInfo['size'],
482481
'mimetype' => $metaInfo['mimetype'],
483-
'extra' => $extra
482+
'extra' => [
483+
'image-width' => $metaInfo['metadata']['image-width'] ?? 0,
484+
'image-height' => $metaInfo['metadata']['image-height'] ?? 0
485+
]
484486
];
485487
}
486488

@@ -489,21 +491,23 @@ public function getMetadata(string $path): array
489491
*/
490492
public function search($pattern, $path): array
491493
{
492-
return $this->glob(rtrim($path, '/') . '/' . ltrim($pattern, '/'));
494+
return iterator_to_array(
495+
$this->glob(rtrim($path, '/') . '/' . ltrim($pattern, '/')),
496+
false
497+
);
493498
}
494499

495500
/**
496501
* Emulate php glob function for AWS S3 storage
497502
*
498503
* @param string $pattern
499-
* @return array
504+
* @return Generator
500505
* @throws FileSystemException
501506
*/
502-
private function glob(string $pattern): array
507+
private function glob(string $pattern): Generator
503508
{
504-
$directoryContent = [];
505-
506509
$patternFound = preg_match('(\*|\?|\[.+\])', $pattern, $parentPattern, PREG_OFFSET_CAPTURE);
510+
507511
if ($patternFound) {
508512
// phpcs:ignore Magento2.Functions.DiscouragedFunction
509513
$parentDirectory = \dirname(substr($pattern, 0, $parentPattern[0][1] + 1));
@@ -512,21 +516,19 @@ private function glob(string $pattern): array
512516
$searchPattern = $this->getSearchPattern($pattern, $parentPattern, $parentDirectory, $index);
513517

514518
if ($this->isDirectory($parentDirectory . '/')) {
515-
$directoryContent = $this->getDirectoryContent($parentDirectory, $searchPattern, $leftover, $index);
519+
yield from $this->getDirectoryContent($parentDirectory, $searchPattern, $leftover, $index);
516520
}
517521
} elseif ($this->isDirectory($pattern) || $this->isFile($pattern)) {
518-
$directoryContent[] = $pattern;
522+
yield $pattern;
519523
}
520-
521-
return $directoryContent;
522524
}
523525

524526
/**
525527
* @inheritDoc
526528
*/
527529
public function symlink($source, $destination, DriverInterface $targetDriver = null): bool
528530
{
529-
throw new FileSystemException(__('Method %1 is not supported', __METHOD__));
531+
return $this->copy($source, $destination, $targetDriver);
530532
}
531533

532534
/**
@@ -850,31 +852,25 @@ private function getSearchPattern(string $pattern, array $parentPattern, string
850852
* @param string $searchPattern
851853
* @param string $leftover
852854
* @param int|bool $index
853-
* @return array
855+
* @return Generator
854856
* @throws FileSystemException
855857
*/
856858
private function getDirectoryContent(
857859
string $parentDirectory,
858860
string $searchPattern,
859861
string $leftover,
860862
$index
861-
): array {
863+
): Generator {
862864
$items = $this->readDirectory($parentDirectory . '/');
863865
$directoryContent = [];
864866
foreach ($items as $item) {
865867
if (preg_match('/' . $searchPattern . '$/', $item)
866868
// phpcs:ignore Magento2.Functions.DiscouragedFunction
867869
&& strpos(basename($item), '.') !== 0) {
868870
if ($index === false || \strlen($leftover) === $index + 1) {
869-
$directoryContent[] = $this->isDirectory($item)
870-
? rtrim($item, '/') . '/'
871-
: $item;
871+
yield $this->isDirectory($item) ? rtrim($item, '/') . '/' : $item;
872872
} elseif (strlen($leftover) > $index + 1) {
873-
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
874-
$directoryContent = array_merge(
875-
$directoryContent,
876-
$this->glob("{$parentDirectory}/{$item}" . substr($leftover, $index))
877-
);
873+
yield from $this->glob("{$parentDirectory}/{$item}" . substr($leftover, $index));
878874
}
879875
}
880876
}

app/code/Magento/AwsS3/etc/adminhtml/system.xml

Lines changed: 0 additions & 35 deletions
This file was deleted.

app/code/Magento/Backup/Model/Fs/Collection.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ class Collection extends \Magento\Framework\Data\Collection\Filesystem
4040
*/
4141
protected $_backup = null;
4242

43-
/**
44-
* @var \Magento\Framework\Filesystem
45-
*/
46-
protected $_filesystem;
47-
4843
/**
4944
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
5045
* @param \Magento\Backup\Helper\Data $backupData

app/code/Magento/Config/Model/Config/Reader/Source/Deployed/DocumentRoot.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,26 @@
66
namespace Magento\Config\Model\Config\Reader\Source\Deployed;
77

88
use Magento\Framework\App\DeploymentConfig;
9-
use Magento\Framework\App\ObjectManager;
10-
use Magento\Framework\Config\DocumentRoot as BaseDocumentRoot;
9+
use Magento\Framework\App\Filesystem\DirectoryList;
1110

1211
/**
1312
* Document root detector.
1413
*
1514
* @api
1615
* @since 101.0.0
1716
*
18-
* @deprecated Use new implementation
19-
* @see \Magento\Framework\Config\DocumentRoot
17+
* @deprecated Magento always uses the pub directory
18+
* @see DirectoryList::PUB
2019
*/
2120
class DocumentRoot
2221
{
23-
/**
24-
* @var BaseDocumentRoot
25-
*/
26-
private $documentRoot;
27-
2822
/**
2923
* @param DeploymentConfig $config
30-
* @param BaseDocumentRoot $documentRoot
3124
*
3225
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3326
*/
34-
public function __construct(DeploymentConfig $config, BaseDocumentRoot $documentRoot = null)
27+
public function __construct(DeploymentConfig $config)
3528
{
36-
$this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(BaseDocumentRoot::class);
3729
}
3830

3931
/**
@@ -44,7 +36,7 @@ public function __construct(DeploymentConfig $config, BaseDocumentRoot $document
4436
*/
4537
public function getPath()
4638
{
47-
return $this->documentRoot->getPath();
39+
return DirectoryList::PUB;
4840
}
4941

5042
/**
@@ -55,6 +47,6 @@ public function getPath()
5547
*/
5648
public function isPub()
5749
{
58-
return $this->documentRoot->isPub();
50+
return true;
5951
}
6052
}

app/code/Magento/MediaGalleryRenditions/etc/config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
<width>1000</width>
1414
<height>1000</height>
1515
</media_gallery_renditions>
16+
<media_storage_configuration>
17+
<allowed_resources>
18+
<renditions_folder>.renditions</renditions_folder>
19+
</allowed_resources>
20+
</media_storage_configuration>
1621
</system>
1722
</default>
1823
</config>

app/code/Magento/MediaGallerySynchronization/Model/CreateAssetFromFile.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,24 +82,25 @@ public function execute(string $path): AssetInterface
8282
* SPL file info is not compatible with remote storages and must not be used.
8383
*/
8484
$file = $this->getFileInfo->execute($absolutePath);
85+
[$width, $height] = getimagesize($absolutePath);
8586
$meta = [
8687
'size' => $file->getSize(),
8788
'extension' => $file->getExtension(),
8889
'basename' => $file->getBasename(),
90+
'extra' => [
91+
'image-width' => $width,
92+
'image-height' => $height
93+
]
8994
];
9095
}
9196

92-
[$width, $height] = getimagesizefromstring(
93-
$this->getMediaDirectory()->readFile($absolutePath)
94-
);
95-
9697
return $this->assetFactory->create(
9798
[
9899
'id' => null,
99100
'path' => $path,
100101
'title' => $meta['basename'],
101-
'width' => $width,
102-
'height' => $height,
102+
'width' => $meta['extra']['image-width'],
103+
'height' => $meta['extra']['image-height'],
103104
'hash' => $this->getHash($path),
104105
'size' => $meta['size'],
105106
'contentType' => 'image/' . $meta['extension'],

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ class Storage extends AbstractModel
8282
*/
8383
protected $_databaseFactory;
8484

85+
/**
86+
* @var Filesystem
87+
*
88+
* @deprecated
89+
*/
90+
protected $filesystem;
91+
8592
/**
8693
* @var Filesystem\Directory\ReadInterface
8794
*/
@@ -122,6 +129,8 @@ public function __construct(
122129
$this->_fileFlag = $fileFlag;
123130
$this->_fileFactory = $fileFactory;
124131
$this->_databaseFactory = $databaseFactory;
132+
$this->filesystem = $filesystem;
133+
125134
$this->localMediaDirectory = $filesystem->getDirectoryRead(
126135
DirectoryList::MEDIA,
127136
Filesystem\DriverPool::FILE

app/code/Magento/RemoteStorage/Console/Command/RemoteStorageDisableCommand.php

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)