Skip to content

Commit

Permalink
magento#1515: Add keywords field to the edit image description slide …
Browse files Browse the repository at this point in the history
…panel - fixed merge conflict
  • Loading branch information
joweecaquicla committed Jul 10, 2020
2 parents 283c61d + 7c968be commit 49b4872
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
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']
]);
}
}
26 changes: 26 additions & 0 deletions AdobeStockImage/Model/SaveMediaGalleryAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Filesystem;
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface;
use Magento\Framework\Exception\FileSystemException;

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

/**
* @var GetContentHashInterface
*/
private $getContentHash;

/**
* @var Filesystem
*/
Expand All @@ -37,15 +44,18 @@ class SaveMediaGalleryAsset
/**
* @param SaveAssetsInterface $saveMediaAsset
* @param DocumentToMediaGalleryAsset $documentToMediaGalleryAsset
* @param GetContentHashInterface $getContentHash
* @param Filesystem $fileSystem
*/
public function __construct(
SaveAssetsInterface $saveMediaAsset,
DocumentToMediaGalleryAsset $documentToMediaGalleryAsset,
GetContentHashInterface $getContentHash,
Filesystem $fileSystem
) {
$this->saveMediaAsset = $saveMediaAsset;
$this->documentToMediaGalleryAsset = $documentToMediaGalleryAsset;
$this->getContentHash = $getContentHash;
$this->fileSystem = $fileSystem;
}

Expand All @@ -66,6 +76,7 @@ public function execute(Document $document, string $destinationPath): void
'path' => $destinationPath,
'source' => 'Adobe Stock',
'size' => $fileSize,
'hash' => $this->hashImageContent($destinationPath)
];

$mediaGalleryAsset = $this->documentToMediaGalleryAsset->convert($document, $additionalData);
Expand All @@ -86,4 +97,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 = $this->getContentHash->execute($imageContent);
return $hashedImageContent;
}
}
18 changes: 16 additions & 2 deletions AdobeStockImage/Test/Unit/Model/SaveMediaGalleryAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\MediaGallery\Model\Asset;
use Magento\MediaGalleryApi\Api\SaveAssetsInterface;
use Magento\MediaGallerySynchronizationApi\Model\GetContentHashInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -51,12 +52,18 @@ class SaveMediaGalleryAssetTest extends TestCase
*/
private $saveMediaAsset;

/**
* @var GetContentHashInterface|MockObject
*/
private $getContentHash;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->saveAssets = $this->createMock(SaveAssetsInterface::class);
$this->getContentHash = $this->createMock(GetContentHashInterface::class);
$this->converter = $this->createMock(DocumentToMediaGalleryAsset::class);
$this->filesystem = $this->createMock(Filesystem::class);
$this->mediaDirectory = $this->createMock(Read::class);
Expand Down Expand Up @@ -84,12 +91,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 +106,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' => $this->getContentHash->execute($hash)
];
$mediaGalleryAssetMock = $this->createMock(Asset::class);
$this->converter->expects($this->once())
Expand Down
1 change: 1 addition & 0 deletions AdobeStockImage/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"magento/module-adobe-stock-asset-api": "*",
"magento/module-adobe-stock-image-api": "*",
"magento/module-media-gallery-api": "*",
"magento/module-media-gallery-synchronization-api": "*",
"magento/module-cms": "*"
},
"suggest": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ define([
* @param {Number} imageId
*/
getImageRecord: function (imageId) {
getDetails(this.imageDetailsUrl, imageId).then(function (imageDetails) {
var id = imageDetails['adobe_stock'][0].value;
getDetails(this.imageDetailsUrl, [imageId]).then(function (imageDetails) {
var id = imageDetails[imageId]['adobe_stock'][0].value;

this.image().actions().licenseProcess(
id,
imageDetails.title,
imageDetails.path,
imageDetails['content_type'],
imageDetails[imageId].title,
imageDetails[imageId].path,
imageDetails[imageId]['content_type'],
true
).then(function () {
this.image().actions().login().getUserQuota();
Expand Down
74 changes: 50 additions & 24 deletions MediaGalleryUi/view/adminhtml/web/js/directory/directoryTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ define([
filterChipsProvider: 'componentType = filters, ns = ${ $.ns }',
directoryTreeSelector: '#media-gallery-directory-tree',
getDirectoryTreeUrl: 'media_gallery/directories/gettree',
jsTreeReloaded: null,
modules: {
directories: '${ $.name }_directories',
filterChips: '${ $.filterChipsProvider }'
Expand All @@ -45,8 +46,9 @@ define([
return $(this.directoryTreeSelector).length === 0;
}.bind(this),
function () {
this.renderDirectoryTree();
this.initEvents();
this.renderDirectoryTree().then(function () {
this.initEvents();
}.bind(this));
}.bind(this)
);

Expand All @@ -57,17 +59,32 @@ define([
* Render directory tree component.
*/
renderDirectoryTree: function () {
var deferred = $.Deferred();

this.getJsonTree().then(function (data) {
this.createFolderIfNotExists(data).then(function (isFolderCreated) {
if (isFolderCreated) {
this.getJsonTree().then(function (newData) {
this.createTree(newData);
deferred.resolve();
}.bind(this));
} else {
this.createTree(data);
deferred.resolve();
}
}.bind(this));
}.bind(this));

return deferred.promise();
},

/**
* Set jstree reloaded
*
* @param {Boolean} value
*/
setJsTreeReloaded: function (value) {
this.jsTreeReloaded = value;
},

/**
Expand Down Expand Up @@ -98,19 +115,13 @@ define([
this.createDirectoryUrl,
pathArray
).then(function () {
deferred.resolve({
result: true
});
deferred.resolve(true);
});
} else {
deferred.resolve({
result: false
});
deferred.resolve(false);
}
} else {
deferred.resolve({
result: false
});
deferred.resolve(false);
}

return deferred.promise();
Expand Down Expand Up @@ -203,21 +214,40 @@ define([
* Handle jstree events
*/
initEvents: function () {
this.firejsTreeEvents();
this.overrideMultiselectBehavior();

$(this.directoryTreeSelector).on('loaded.jstree', function () {
this.checkChipFiltersState();
}.bind(this));

$(window).on('reload.MediaGallery', function () {
this.checkChipFiltersState();
this.getJsonTree().then(function (data) {
this.createFolderIfNotExists(data).then(function (isCreated) {
if (isCreated) {
this.renderDirectoryTree().then(function () {
this.setJsTreeReloaded(true);
this.firejsTreeEvents();
}.bind(this));
} else {
this.checkChipFiltersState();
}
}.bind(this));
}.bind(this));
}.bind(this));
},

/**
* Fire event for jstree component
*/
firejsTreeEvents: function () {
$(this.directoryTreeSelector).on('select_node.jstree', function (element, data) {
var path = $(data.rslt.obj).data('path');

this.setActiveNodeFilter(path);
this.setJsTreeReloaded(false);
}.bind(this));

$(this.directoryTreeSelector).on('loaded.jstree', function () {
this.checkChipFiltersState();
}.bind(this));

},

/**
Expand Down Expand Up @@ -295,7 +325,8 @@ define([
* @param {String} nodePath
*/
setActiveNodeFilter: function (nodePath) {
if (this.activeNode() === nodePath) {

if (this.activeNode() === nodePath && !this.jsTreeReloaded) {
this.selectStorageRoot();
} else {
this.selectFolder(nodePath);
Expand Down Expand Up @@ -376,13 +407,8 @@ define([

this.getJsonTree().then(function (data) {
this.createTree(data);
this.overrideMultiselectBehavior();
$(this.directoryTreeSelector).on('select_node.jstree', function (element, object) {
var path = $(object.rslt.obj).data('path');

this.setActiveNodeFilter(path);
}.bind(this));

this.setJsTreeReloaded(true);
this.initEvents();
deferred.resolve();
}.bind(this));

Expand Down
20 changes: 12 additions & 8 deletions MediaGalleryUi/view/adminhtml/web/js/image/image-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ define([
'uiElement',
'Magento_MediaGalleryUi/js/action/deleteImageWithDetailConfirmation',
'Magento_MediaGalleryUi/js/grid/columns/image/insertImageAction',
'Magento_MediaGalleryUi/js/action/saveDetails'
'Magento_MediaGalleryUi/js/action/saveDetails',
'mage/validation'
], function ($, _, Element, deleteImageWithDetailConfirmation, addSelected, saveDetails) {
'use strict';

Expand Down Expand Up @@ -74,14 +75,17 @@ define([
var saveDetailsUrl = this.mediaGalleryEditDetails().saveDetailsUrl,
modalElement = $(this.modalSelector);

saveDetails(
saveDetailsUrl,
modalElement.find('#image-edit-details-form')
).then(function () {
this.closeModal();
this.imageModel().reloadGrid();
}.bind(this));
modalElement.find('#image-edit-details-form').validation();

if (modalElement.find('#image-edit-details-form').validation('isValid')) {
saveDetails(
saveDetailsUrl,
modalElement.find('#image-edit-details-form')
).then(function () {
this.closeModal();
this.imageModel().reloadGrid();
}.bind(this));
}
},

/**
Expand Down

0 comments on commit 49b4872

Please sign in to comment.