Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1494: Save image content hash to the media asset when saving image from the Adobe Stock #1508

Merged
3 changes: 2 additions & 1 deletion AdobeStockImage/Model/Extract/MediaGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public function convert(Document $document, array $additionalData = []): AssetIn
'height' => $assetData['height'],
'size' => $assetData['size'],
'contentType' => $assetData['content_type'],
'source' => $assetData['source']
'source' => $assetData['source'],
'hash' => $assetData['hash']
]);
}
}
18 changes: 18 additions & 0 deletions AdobeStockImage/Model/SaveMediaGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Filesystem;
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\Framework\Exception\FileSystemException;

/**
* Process save action of the media gallery asset.
Expand Down Expand Up @@ -61,11 +62,13 @@ public function execute(Document $document, string $destinationPath): void
{
try {
$fileSize = $this->calculateFileSize($destinationPath);
$hashedImageContent = $this->hashImageContent($destinationPath);
joweecaquicla marked this conversation as resolved.
Show resolved Hide resolved
$additionalData = [
'id' => null,
'path' => $destinationPath,
'source' => 'Adobe Stock',
'size' => $fileSize,
'hash' => $hashedImageContent
joweecaquicla marked this conversation as resolved.
Show resolved Hide resolved
];

$mediaGalleryAsset = $this->documentToMediaGalleryAsset->convert($document, $additionalData);
Expand All @@ -86,4 +89,19 @@ private function calculateFileSize(string $destinationPath): int
$mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
return $mediaDirectory->stat($mediaDirectory->getAbsolutePath($destinationPath))['size'];
}

/**
* Hash image content.
*
* @param string $destinationPath
* @return string
* @throws FileSystemException
*/
private function hashImageContent(string $destinationPath): string
{
$mediaDirectory = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA);
$imageContent = $mediaDirectory->readFile($mediaDirectory->getAbsolutePath($destinationPath));
$hashedImageContent = sha1($imageContent);
return $hashedImageContent;
}
coderimus marked this conversation as resolved.
Show resolved Hide resolved
}
11 changes: 9 additions & 2 deletions AdobeStockImage/Test/Unit/Model/SaveMediaGalleryAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public function testExecute(): void
$document = $this->createMock(Document::class);
$destinationPath = 'path';

$this->filesystem->expects($this->once())
$this->filesystem->expects($this->atLeastOnce())
->method('getDirectoryRead')
->with(DirectoryList::MEDIA)
->willReturn($this->mediaDirectory);

$this->mediaDirectory->expects($this->once())
$this->mediaDirectory->expects($this->atLeastOnce())
->method('getAbsolutePath')
->with($destinationPath)
->willReturn('root/pub/media/catalog/test-image.jpeg');
Expand All @@ -99,11 +99,18 @@ public function testExecute(): void
->method('stat')
->willReturn(['size' => $fileSize]);

$hash = 'hash';

$this->mediaDirectory->expects($this->once())
->method('readFile')
->willReturn($hash);

$additionalData = [
'id' => null,
'path' => $destinationPath,
'source' => 'Adobe Stock',
'size' => $fileSize,
'hash' => sha1($hash)
];
$mediaGalleryAssetMock = $this->createMock(Asset::class);
$this->converter->expects($this->once())
Expand Down