Skip to content

Commit

Permalink
Merge pull request #1596 from Nazar65/ASI-1497
Browse files Browse the repository at this point in the history
Polpulate asset metadata on uploading the image to magento && Populate media asset description when saving image from adobe stock
  • Loading branch information
sivaschenko committed Jul 26, 2020
2 parents 89e2fb4 + 9126ed2 commit ca7210f
Show file tree
Hide file tree
Showing 33 changed files with 598 additions and 200 deletions.
1 change: 1 addition & 0 deletions AdobeStockImage/Model/Extract/MediaGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function convert(Document $document, array $additionalData = []): AssetIn
return $this->assetFactory->create([
'path' => $assetData['path'],
'title' => $assetData['title'],
'description' => $assetData['description'],
'width' => $assetData['width'],
'height' => $assetData['height'],
'size' => $assetData['size'],
Expand Down
42 changes: 41 additions & 1 deletion AdobeStockImage/Model/SaveMediaGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface;
use Magento\Framework\Exception\FileSystemException;
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
use Magento\Framework\Api\AttributeValueFactory;

/**
* Process save action of the media gallery asset.
Expand All @@ -31,6 +33,11 @@ class SaveMediaGalleryAsset
*/
private $documentToMediaGalleryAsset;

/**
* @var AttributeValueFactory
*/
private $attributeValueFactory;

/**
* @var GetContentHashInterface
*/
Expand All @@ -41,22 +48,33 @@ class SaveMediaGalleryAsset
*/
private $fileSystem;

/**
* @var ExtractMetadataInterface
*/
private $extractMetadata;

/**
* @param SaveAssetsInterface $saveMediaAsset
* @param DocumentToMediaGalleryAsset $documentToMediaGalleryAsset
* @param GetContentHashInterface $getContentHash
* @param Filesystem $fileSystem
* @param ExtractMetadataInterface $extractMetadata
* @param AttributeValueFactory $attributeValueFactory
*/
public function __construct(
SaveAssetsInterface $saveMediaAsset,
DocumentToMediaGalleryAsset $documentToMediaGalleryAsset,
GetContentHashInterface $getContentHash,
Filesystem $fileSystem
Filesystem $fileSystem,
ExtractMetadataInterface $extractMetadata,
AttributeValueFactory $attributeValueFactory
) {
$this->saveMediaAsset = $saveMediaAsset;
$this->documentToMediaGalleryAsset = $documentToMediaGalleryAsset;
$this->getContentHash = $getContentHash;
$this->fileSystem = $fileSystem;
$this->extractMetadata = $extractMetadata;
$this->attributeValueFactory = $attributeValueFactory;
}

/**
Expand All @@ -79,13 +97,35 @@ public function execute(Document $document, string $destinationPath): void
'hash' => $this->hashImageContent($destinationPath)
];

$document = $this->setDescriptionField($document, $destinationPath);
$mediaGalleryAsset = $this->documentToMediaGalleryAsset->convert($document, $additionalData);
$this->saveMediaAsset->execute([$mediaGalleryAsset]);
} catch (\Exception $exception) {
throw new CouldNotSaveException(__('Could not save media gallery asset.'), $exception);
}
}

/**
* Set description from file metadata
*
* @param Document $document
* @param string $destinationPath
*/
private function setDescriptionField(Document $document, string $destinationPath): Document
{
$customAttributes = $document->getCustomAttributes();
$mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
$metadata = $this->extractMetadata->execute($mediaDirectory->getAbsolutePath($destinationPath));
$attribute = $this->attributeValueFactory->create();

$attribute->setAttributeCode('description');
$attribute->setValue($metadata->getDescription());
$customAttributes['description'] = $attribute;
$document->setCustomAttributes($customAttributes);

return $document;
}

/**
* Calculates saved image file size.
*
Expand Down
40 changes: 39 additions & 1 deletion AdobeStockImage/Test/Unit/Model/SaveMediaGalleryAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Magento\MediaGalleryMetadataApi\Api\ExtractMetadataInterface;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Api\AttributeValue;
use Magento\MediaGalleryMetadataApi\Api\Data\MetadataInterface;

/**
* Test saving a media gallery asset and return its id.
Expand Down Expand Up @@ -57,6 +61,16 @@ class SaveMediaGalleryAssetTest extends TestCase
*/
private $getContentHash;

/**
* @var ExtractMetadataInterface|MockObject
*/
private $extractMetadata;

/**
* @var AttributeValueFactory|MockObject
*/
private $attributeValueFactory;

/**
* @inheritdoc
*/
Expand All @@ -67,13 +81,17 @@ protected function setUp(): void
$this->converter = $this->createMock(DocumentToMediaGalleryAsset::class);
$this->filesystem = $this->createMock(Filesystem::class);
$this->mediaDirectory = $this->createMock(Read::class);
$this->extractMetadata = $this->createMock(ExtractMetadataInterface::class);
$this->attributeValueFactory = $this->createMock(AttributeValueFactory::class);

$this->saveMediaAsset = (new ObjectManager($this))->getObject(
SaveMediaGalleryAsset::class,
[
'saveMediaAsset' => $this->saveAssets,
'documentToMediaGalleryAsset' => $this->converter,
'fileSystem' => $this->filesystem
'fileSystem' => $this->filesystem,
'extractMetadata' => $this->extractMetadata,
'attributeValueFactory' => $this->attributeValueFactory
]
);
$reflection = new \ReflectionClass(get_class($this->saveMediaAsset));
Expand Down Expand Up @@ -119,11 +137,31 @@ public function testExecute(): void
'size' => $fileSize,
'hash' => $this->getContentHash->execute($hash)
];
$attributeMock = $this->createMock(AttributeValue::class);
$metadataMock = $this->createMock(MetadataInterface::class);
$document->expects($this->once())
->method('getCustomAttributes')
->willReturn([]);
$this->extractMetadata->expects($this->once())
->method('execute')
->willReturn($metadataMock);
$this->attributeValueFactory->expects($this->once())
->method('create')
->willReturn($attributeMock);

$mediaGalleryAssetMock = $this->createMock(Asset::class);
$this->converter->expects($this->once())
->method('convert')
->with($document, $additionalData)
->willReturn($mediaGalleryAssetMock);
$attributeMock->expects($this->once())
->method('setAttributeCode');
$attributeMock->expects($this->once())
->method('setValue');
$metadataMock->expects($this->once())
->method('getDescription');
$document->expects($this->once())
->method('setCustomAttributes');

$this->saveAssets->expects($this->once())
->method('execute')
Expand Down
3 changes: 2 additions & 1 deletion AdobeStockImage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"magento/module-adobe-stock-image-api": "*",
"magento/module-media-gallery-api": "*",
"magento/module-media-gallery-synchronization-api": "*",
"magento/module-cms": "*"
"magento/module-cms": "*",
"magento/module-media-gallery-metadata-api": "*"
},
"suggest": {
"magento/module-catalog": "*"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
<actionGroup ref="NavigateToCreatedCMSPageActionGroup" stepKey="navigateToCreatedCMSPage">
<argument name="CMSPage" value="$$page$$"/>
</actionGroup>
<click selector="{{CmsNewPagePageContentSection.header}}" stepKey="clickExpandContent"/>
<click selector="{{CmsWYSIWYGSection.InsertImageBtn}}" stepKey="clickInsertImageIcon" />
<actionGroup ref="AdminOpenMediaGalleryFromPageNoEditorActionGroup" stepKey="openMediaGalleryForPage"/>
<waitForPageLoad stepKey="waitForPageLoad" />
<actionGroup ref="AdminEnhancedMediaGalleryUploadImageActionGroup" stepKey="uploadImage">
<argument name="image" value="ImageUpload"/>
Expand Down
46 changes: 13 additions & 33 deletions MediaGalleryIntegration/Plugin/SaveBaseCategoryImageInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,17 @@

namespace Magento\MediaGalleryIntegration\Plugin;

use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGalleryApi\Api\GetAssetsByPathsInterface;
use Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface;
use Magento\Catalog\Model\ImageUploader;
use Magento\MediaGallerySynchronization\Model\CreateAssetFromFile;
use Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\MediaGallerySynchronization\Model\Filesystem\SplFileInfoFactory;
use Magento\MediaGallerySynchronizationApi\Api\SynchronizeFilesInterface;

/**
* Save base category image by SaveAssetsInterface.
*/
class SaveBaseCategoryImageInformation
{
/**
* @var SplFileInfoFactory
*/
private $splFileInfoFactory;

/**
* @var SaveAssetsInterface
*/
private $saveAsset;

/**
* @var DeleteAssetsByPathsInterface
*/
Expand All @@ -39,39 +27,33 @@ class SaveBaseCategoryImageInformation
* @var GetAssetsByPathsInterface
*/
private $getAssetsByPaths;

/**
* @var CreateAssetFromFile
*/
private $createAssetFromFile;

/**
* @var Storage
*/
private $storage;

/**
* @var SynchronizeFilesInterface
*/
private $synchronizeFiles;

/**
* @param DeleteAssetsByPathsInterface $deleteAssetsByPath
* @param GetAssetsByPathsInterface $getAssetsByPaths
* @param SplFileInfoFactory $splFileInfoFactory
* @param CreateAssetFromFile $createAssetFromFile
* @param SaveAssetsInterface $saveAsset
* @param Storage $storage
* @param SynchronizeFilesInterface $synchronizeFiles
*/
public function __construct(
DeleteAssetsByPathsInterface $deleteAssetsByPath,
GetAssetsByPathsInterface $getAssetsByPaths,
SplFileInfoFactory $splFileInfoFactory,
CreateAssetFromFile $createAssetFromFile,
SaveAssetsInterface $saveAsset,
Storage $storage
Storage $storage,
SynchronizeFilesInterface $synchronizeFiles
) {
$this->deleteAssetsByPaths = $deleteAssetsByPath;
$this->getAssetsByPaths = $getAssetsByPaths;
$this->splFileInfoFactory = $splFileInfoFactory;
$this->createAssetFromFile = $createAssetFromFile;
$this->saveAsset = $saveAsset;
$this->storage = $storage;
$this->synchronizeFiles = $synchronizeFiles;
}

/**
Expand All @@ -83,16 +65,14 @@ public function __construct(
public function afterMoveFileFromTmp(ImageUploader $subject, string $imagePath): string
{
$absolutePath = $this->storage->getCmsWysiwygImages()->getStorageRoot() . $imagePath;
$file = $this->splFileInfoFactory->create($absolutePath);
$tmpPath = $subject->getBaseTmpPath() . '/' . $file->getFileName();
$tmpPath = $subject->getBaseTmpPath() . '/' . substr(strrchr($imagePath, "/"), 1);
$tmpAssets = $this->getAssetsByPaths->execute([$tmpPath]);

if (!empty($tmpAssets)) {
$this->deleteAssetsByPaths->execute([$tmpAssets[0]->getPath()]);
}

$this->saveAsset->execute([$this->createAssetFromFile->execute($file)]);
$this->storage->resizeFile($absolutePath);

$this->synchronizeFiles->execute([$absolutePath]);

return $imagePath;
}
Expand Down
Loading

0 comments on commit ca7210f

Please sign in to comment.