Skip to content

Commit

Permalink
Merge pull request #7161 from magento-arcticfoxes/B2B-2022
Browse files Browse the repository at this point in the history
B2B-2022 :[AWS S3] [Integration Tests]: Investigate Test Failures in Catalog module
  • Loading branch information
avattam06 authored Nov 4, 2021
2 parents 6b196b5 + 3300561 commit fa1824e
Show file tree
Hide file tree
Showing 27 changed files with 898 additions and 199 deletions.
6 changes: 3 additions & 3 deletions app/code/Magento/Catalog/Block/Product/Gallery.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class Gallery extends \Magento\Framework\View\Element\Template
{
/**
* Core registry
* Framework class for Core Registry
*
* @var \Magento\Framework\Registry
*/
Expand Down Expand Up @@ -122,9 +122,9 @@ public function getImageFile()
public function getImageWidth()
{
$file = $this->getCurrentImage()->getPath();

$fileStat = $this->getMediaDirectory()->stat($file);
if ($this->_filesystem->getDirectoryRead(DirectoryList::MEDIA)->isFile($file)) {
$size = getimagesize($file);
$size = $fileStat['size'];
if (isset($size[0])) {
if ($size[0] > 600) {
return 600;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,33 @@

namespace Magento\Catalog\Model\Product\Option\Type\File;

use Magento\Framework\ObjectManagerInterface;

/**
* Class ValidateFactory. Creates Validator with type "ExistingValidate"
*/
class ValidateFactory
{
/**
* @var ObjectManagerInterface
*/
private ObjectManagerInterface $objectManager;

/**
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* Main factory method
*
* @return \Zend_Validate
* @return ExistingValidate
*/
public function create()
{
return new ExistingValidate();
return $this->objectManager->create(ExistingValidate::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ protected function parseExtensionsString($extensions)
}

/**
* Adds required validators to th $object
*
* @param \Zend_File_Transfer_Adapter_Http|\Zend_Validate $object
* @param \Magento\Catalog\Model\Product\Option $option
* @param array $fileFullPath
Expand Down Expand Up @@ -193,10 +195,12 @@ protected function isImage($fileInfo)
if (!$this->rootDirectory->isReadable($this->rootDirectory->getRelativePath($fileInfo))) {
return false;
}
$imageInfo = getimagesize($fileInfo);
if (!$imageInfo) {

$fileContent = $this->rootDirectory->readFile($fileInfo);
if (empty($fileContent) || !getimagesizefromstring($fileContent)) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public function __construct(
}

/**
* Saves file
*
* @param ImageContentInterface $imageContent
* @return string
* @throws \Magento\Framework\Exception\InputException
*/
protected function saveFile(ImageContentInterface $imageContent)
{
Expand All @@ -50,6 +53,8 @@ protected function saveFile(ImageContentInterface $imageContent)
}

/**
* Save file content and return file details
*
* @param ImageContentInterface $imageContent
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
Expand All @@ -58,16 +63,19 @@ public function processFileContent(ImageContentInterface $imageContent)
{
$filePath = $this->saveFile($imageContent);

$fileAbsolutePath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath($filePath);
$fileHash = hash('sha256', $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->readFile($filePath));
$imageSize = getimagesize($fileAbsolutePath);
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);
$fileAbsolutePath = $mediaDirectory->getAbsolutePath($filePath);
$fileContent = $mediaDirectory->readFile($filePath);
$fileHash = hash('sha256', $fileContent);
$imageSize = getimagesizefromstring($fileContent);
$stat = $mediaDirectory->stat($fileAbsolutePath);
$result = [
'type' => $imageContent->getType(),
'title' => $imageContent->getName(),
'fullpath' => $fileAbsolutePath,
'quote_path' => $filePath,
'order_path' => $filePath,
'size' => filesize($fileAbsolutePath),
'size' => $stat['size'],
'width' => $imageSize ? $imageSize[0] : 0,
'height' => $imageSize ? $imageSize[1] : 0,
'secret_key' => substr($fileHash, 0, 20),
Expand Down
7 changes: 5 additions & 2 deletions app/code/Magento/ImportExport/Model/Import/Source/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\ImportExport\Model\Import\Source;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\ValidatorException;

/**
Expand All @@ -16,15 +17,17 @@ class Zip extends Csv
* @param string $file
* @param \Magento\Framework\Filesystem\Directory\Write $directory
* @param string $options
* @param \Magento\Framework\Archive\Zip|null $zipArchive
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\ValidatorException
*/
public function __construct(
$file,
\Magento\Framework\Filesystem\Directory\Write $directory,
$options
$options,
\Magento\Framework\Archive\Zip $zipArchive = null
) {
$zip = new \Magento\Framework\Archive\Zip();
$zip = $zipArchive ?? ObjectManager::getInstance()->get(\Magento\Framework\Archive\Zip::class);
$csvFile = $zip->unpack(
$file,
preg_replace('/\.zip$/i', '.csv', $file)
Expand Down
129 changes: 129 additions & 0 deletions app/code/Magento/RemoteStorage/Plugin/ExistingValidate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\RemoteStorage\Plugin;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\RuntimeException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\TargetDirectory;
use Magento\Framework\Image\Adapter\AbstractAdapter;
use Magento\RemoteStorage\Model\Config;
use Psr\Log\LoggerInterface;
use Magento\Catalog\Model\Product\Option\Type\File\ExistingValidate as Subject;

/**
* @see AbstractAdapter
*/
class ExistingValidate
{
/**
* @var Filesystem\Directory\WriteInterface
*/
private $tmpDirectoryWrite;

/**
* @var Filesystem\Directory\WriteInterface
*/
private $remoteDirectoryWrite;

/**
* @var array
*/
private $tmpFiles = [];

/**
* @var bool
*/
private $isEnabled;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param Filesystem $filesystem
* @param TargetDirectory $targetDirectory
* @param Config $config
* @param LoggerInterface $logger
* @throws FileSystemException
* @throws RuntimeException
*/
public function __construct(
Filesystem $filesystem,
TargetDirectory $targetDirectory,
Config $config,
LoggerInterface $logger
) {
$this->tmpDirectoryWrite = $filesystem->getDirectoryWrite(DirectoryList::TMP);
$this->remoteDirectoryWrite = $targetDirectory->getDirectoryWrite(DirectoryList::ROOT);
$this->isEnabled = $config->isEnabled();
$this->logger = $logger;
}

/**
* Copies file from the remote server to the tmp directory
*
* @param Subject $subject
* @param string $value
* @param string|null $originalName
* @return array
* @throws FileSystemException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeIsValid(Subject $subject, $value, string $originalName = null)
{
if ($this->isEnabled) {
$value = $this->copyFileToTmp($value);
}
return [$value, $originalName];
}

/**
* Remove 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());
}
}

/**
* Move files from storage to tmp folder
*
* @param string $filePath
* @return string
* @throws FileSystemException
*/
private function copyFileToTmp(string $filePath): string
{
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;
}
}
Loading

0 comments on commit fa1824e

Please sign in to comment.