Skip to content

Commit fdce1bb

Browse files
committed
B2B-2037: [AWS S3] [Integration Tests]: Investigate Test Failures in MediaGallerySynchronization & MediaGallerySynchronizationMetadata modules
1 parent 8f882b8 commit fdce1bb

File tree

10 files changed

+274
-183
lines changed

10 files changed

+274
-183
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\MediaGalleryMetadata\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
12+
/**
13+
* Wrapper for the exif_read_data php function
14+
*/
15+
class ExifReader
16+
{
17+
/**
18+
* Returns result of exif_read_data function
19+
*
20+
* @param string $filePath
21+
* @return array|false
22+
* @throws LocalizedException
23+
*/
24+
public function get(string $filePath)
25+
{
26+
if (!is_callable('exif_read_data')) {
27+
throw new LocalizedException(
28+
__('exif_read_data() must be enabled in php configuration')
29+
);
30+
}
31+
32+
return exif_read_data($filePath);
33+
}
34+
}

app/code/Magento/MediaGalleryMetadata/Model/Jpeg/Segment/ReadExif.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\MediaGalleryMetadata\Model\Jpeg\Segment;
99

10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\MediaGalleryMetadata\Model\ExifReader;
1012
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface;
1113
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterfaceFactory;
1214
use Magento\MediaGalleryMetadataApi\Model\FileInterface;
@@ -28,13 +30,20 @@ class ReadExif implements ReadMetadataInterface
2830
*/
2931
private $metadataFactory;
3032

33+
/**
34+
* @var ExifReader
35+
*/
36+
private $exifReader;
37+
3138
/**
3239
* @param MetadataInterfaceFactory $metadataFactory
3340
*/
3441
public function __construct(
35-
MetadataInterfaceFactory $metadataFactory
42+
MetadataInterfaceFactory $metadataFactory,
43+
ExifReader $exifReader = null
3644
) {
3745
$this->metadataFactory = $metadataFactory;
46+
$this->exifReader = $exifReader ?? ObjectManager::getInstance()->get(ExifReader::class);
3847
}
3948

4049
/**
@@ -72,7 +81,7 @@ private function getExifData(string $filePath): MetadataInterface
7281
$description = null;
7382
$keywords = null;
7483

75-
$data = exif_read_data($filePath);
84+
$data = $this->exifReader->get($filePath);
7685

7786
if (!empty($data)) {
7887
$title = isset($data['DocumentName']) ? $data['DocumentName'] : null;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ 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] = getimagesizefromstring($absolutePath);
85+
[$width, $height] = getimagesize($absolutePath);
8686
$meta = [
8787
'size' => $file->getSize(),
8888
'extension' => $file->getExtension(),

app/code/Magento/RemoteStorage/Driver/Adapter/MetadataProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function getMetadata(string $path): array
104104
'visibility' => $meta->visibility(),
105105
'mimetype' => $meta->mimeType(),
106106
'dirname' => dirname($meta->path()),
107-
'basename' => basename($meta->path()),
107+
'basename' => basename($meta->path(), '.' . pathinfo($path, PATHINFO_EXTENSION)),
108108
];
109109
$extraMetadata = $meta->extraMetadata();
110110
if (isset($extraMetadata['Metadata']['image-width']) && isset($extraMetadata['Metadata']['image-height'])) {
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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;
9+
10+
use Magento\Framework\App\Filesystem\DirectoryList;
11+
use Magento\Framework\Exception\FileSystemException;
12+
use Magento\Framework\Exception\RuntimeException;
13+
use Magento\Framework\Filesystem;
14+
use Magento\Framework\Filesystem\Directory\TargetDirectory;
15+
use Psr\Log\LoggerInterface;
16+
17+
/**
18+
* Copies file from remote to local tmp path
19+
*/
20+
class TmpFileCopier
21+
{
22+
/**
23+
* @var Filesystem\Directory\WriteInterface
24+
*/
25+
private $tmpDirectoryWrite;
26+
27+
/**
28+
* @var Filesystem\Directory\WriteInterface
29+
*/
30+
private $remoteDirectoryWrite;
31+
32+
/**
33+
* @var array
34+
*/
35+
private $tmpFiles = [];
36+
37+
/**
38+
* @var bool
39+
*/
40+
private $isEnabled;
41+
42+
/**
43+
* @var LoggerInterface
44+
*/
45+
private $logger;
46+
47+
/**
48+
* @param Filesystem $filesystem
49+
* @param TargetDirectory $targetDirectory
50+
* @param Config $config
51+
* @param LoggerInterface $logger
52+
* @throws FileSystemException
53+
* @throws RuntimeException
54+
*/
55+
public function __construct(
56+
Filesystem $filesystem,
57+
TargetDirectory $targetDirectory,
58+
Config $config,
59+
LoggerInterface $logger
60+
) {
61+
$this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP);
62+
$this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
63+
$this->isEnabled = $config->isEnabled();
64+
$this->logger = $logger;
65+
}
66+
67+
/**
68+
* Removes created tmp files
69+
*/
70+
public function __destruct()
71+
{
72+
try {
73+
foreach ($this->tmpFiles as $key => $tmpFile) {
74+
$this->tmpDirectoryWrite->delete($tmpFile);
75+
unset($this->tmpFiles[$key]);
76+
}
77+
} catch (\Exception $e) {
78+
$this->logger->error($e->getMessage());
79+
}
80+
}
81+
82+
/**
83+
* Moves file from the remote storage to tmp folder
84+
*
85+
* @param string $filePath
86+
* @return string
87+
* @throws FileSystemException
88+
*/
89+
public function copy(string $filePath): string
90+
{
91+
if (!$this->isEnabled) {
92+
return $filePath;
93+
}
94+
95+
if (isset($this->tmpFiles[$filePath])) {
96+
return $this->tmpFiles[$filePath];
97+
}
98+
99+
$absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath);
100+
if ($this->remoteDirectoryWrite->isFile($absolutePath)) {
101+
$this->tmpDirectoryWrite->create();
102+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
103+
$tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath);
104+
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
105+
if ($this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) >= 0) {
106+
$filePath = $tmpPath;
107+
$this->tmpFiles[$tmpPath] = $tmpPath;
108+
}
109+
}
110+
111+
return $filePath;
112+
}
113+
}

app/code/Magento/RemoteStorage/Plugin/ExistingValidate.php

Lines changed: 8 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,28 @@
77

88
namespace Magento\RemoteStorage\Plugin;
99

10-
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Catalog\Model\Product\Option\Type\File\ExistingValidate as Subject;
1111
use Magento\Framework\Exception\FileSystemException;
12-
use Magento\Framework\Exception\RuntimeException;
13-
use Magento\Framework\Filesystem;
14-
use Magento\Framework\Filesystem\Directory\TargetDirectory;
1512
use Magento\Framework\Image\Adapter\AbstractAdapter;
16-
use Magento\RemoteStorage\Model\Config;
17-
use Psr\Log\LoggerInterface;
18-
use Magento\Catalog\Model\Product\Option\Type\File\ExistingValidate as Subject;
13+
use Magento\RemoteStorage\Model\TmpFileCopier;
1914

2015
/**
2116
* @see AbstractAdapter
2217
*/
2318
class ExistingValidate
2419
{
2520
/**
26-
* @var Filesystem\Directory\WriteInterface
27-
*/
28-
private $tmpDirectoryWrite;
29-
30-
/**
31-
* @var Filesystem\Directory\WriteInterface
32-
*/
33-
private $remoteDirectoryWrite;
34-
35-
/**
36-
* @var array
37-
*/
38-
private $tmpFiles = [];
39-
40-
/**
41-
* @var bool
42-
*/
43-
private $isEnabled;
44-
45-
/**
46-
* @var LoggerInterface
21+
* @var TmpFileCopier
4722
*/
48-
private $logger;
23+
private $tmpFileCopier;
4924

5025
/**
51-
* @param Filesystem $filesystem
52-
* @param TargetDirectory $targetDirectory
53-
* @param Config $config
54-
* @param LoggerInterface $logger
55-
* @throws FileSystemException
56-
* @throws RuntimeException
26+
* @param TmpFileCopier $tmpFileCopier
5727
*/
5828
public function __construct(
59-
Filesystem $filesystem,
60-
TargetDirectory $targetDirectory,
61-
Config $config,
62-
LoggerInterface $logger
29+
TmpFileCopier $tmpFileCopier
6330
) {
64-
$this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP);
65-
$this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
66-
$this->isEnabled = $config->isEnabled();
67-
$this->logger = $logger;
31+
$this->tmpFileCopier = $tmpFileCopier;
6832
}
6933

7034
/**
@@ -79,51 +43,6 @@ public function __construct(
7943
*/
8044
public function beforeIsValid(Subject $subject, $value, string $originalName = null)
8145
{
82-
if ($this->isEnabled) {
83-
$value = $this->copyFileToTmp($value);
84-
}
85-
return [$value, $originalName];
86-
}
87-
88-
/**
89-
* Remove created tmp files
90-
*/
91-
public function __destruct()
92-
{
93-
try {
94-
foreach ($this->tmpFiles as $key => $tmpFile) {
95-
$this->tmpDirectoryWrite->delete($tmpFile);
96-
unset($this->tmpFiles[$key]);
97-
}
98-
} catch (\Exception $e) {
99-
$this->logger->error($e->getMessage());
100-
}
101-
}
102-
103-
/**
104-
* Move files from storage to tmp folder
105-
*
106-
* @param string $filePath
107-
* @return string
108-
* @throws FileSystemException
109-
*/
110-
private function copyFileToTmp(string $filePath): string
111-
{
112-
if (isset($this->tmpFiles[$filePath])) {
113-
return $this->tmpFiles[$filePath];
114-
}
115-
116-
$absolutePath = $this->remoteDirectoryWrite->getAbsolutePath($filePath);
117-
if ($this->remoteDirectoryWrite->isFile($absolutePath)) {
118-
$this->tmpDirectoryWrite->create();
119-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
120-
$tmpPath = $this->tmpDirectoryWrite->getAbsolutePath() . basename($filePath);
121-
$content = $this->remoteDirectoryWrite->getDriver()->fileGetContents($filePath);
122-
if ($this->tmpDirectoryWrite->getDriver()->filePutContents($tmpPath, $content) >= 0) {
123-
$filePath = $tmpPath;
124-
$this->tmpFiles[$tmpPath] = $tmpPath;
125-
}
126-
}
127-
return $filePath;
46+
return [$this->tmpFileCopier->copy($value), $originalName];
12847
}
12948
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Filesystem;
9+
10+
use Magento\Framework\Exception\FileSystemException;
11+
use Magento\MediaGallerySynchronization\Model\Filesystem\GetFileInfo as Subject;
12+
use Magento\RemoteStorage\Model\TmpFileCopier;
13+
14+
/**
15+
* Copies file from the remote server to the tmp directory if remote storage is enabled
16+
*/
17+
class GetFileInfo
18+
{
19+
/**
20+
* @var TmpFileCopier
21+
*/
22+
private $tmpFileCopier;
23+
24+
/**
25+
* @param TmpFileCopier $tmpFileCopier
26+
*/
27+
public function __construct(
28+
TmpFileCopier $tmpFileCopier
29+
) {
30+
$this->tmpFileCopier = $tmpFileCopier;
31+
}
32+
33+
/**
34+
* Copies file from the remote server to the tmp directory
35+
*
36+
* @param Subject $subject
37+
* @param string $path
38+
* @return array
39+
* @throws FileSystemException
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
41+
*/
42+
public function beforeExecute(Subject $subject, string $path)
43+
{
44+
return [$this->tmpFileCopier->copy($path)];
45+
}
46+
}

0 commit comments

Comments
 (0)