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

Updated MediaGallery modules and marked as API #27499

Merged
merged 44 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c12201e
Updated MediaGallery modules and marked as API
sivaschenko Mar 30, 2020
abb4503
Fixed static tests
sivaschenko Mar 30, 2020
be2e695
magento/magento2#27499: Code review changes
sivaschenko Apr 1, 2020
e8eb3a9
magento/magento2#27499: Corrected DI configuration
sivaschenko Apr 1, 2020
e671db3
- Rename interface
VladimirZaets Apr 3, 2020
83e7e37
Test coverage
VladimirZaets Apr 3, 2020
df912e4
Merge branch '2.4-develop' of github.com:magento/magento2 into media-…
sivaschenko Apr 3, 2020
3784f92
magento/magento2#27499: Removed DataExtractor in favour to DataObject…
sivaschenko Apr 3, 2020
b77e66d
magento/magento2#27499: Code review changes
sivaschenko Apr 3, 2020
909ea3f
agento/magento2#27499: Updated blacklist patterns
sivaschenko Apr 4, 2020
0a57486
Add configuration for MediaGallery/Directory
VladimirZaets Apr 7, 2020
7a82782
Merge - Add configuration for MediaGallery/Directory
VladimirZaets Apr 7, 2020
90bf91d
Corrected blacklist patterns
sivaschenko Apr 7, 2020
7fd26a9
Added comments and strict types enabling
sivaschenko Apr 7, 2020
e1bea53
- Fix unit test
VladimirZaets Apr 8, 2020
2bd8609
- Fix unit test
VladimirZaets Apr 8, 2020
e69ee88
- Remove optional example attribute from xsd
VladimirZaets Apr 8, 2020
5aceeab
- Add rollback for media_asset fixture.
VladimirZaets Apr 8, 2020
a8ba203
magento/magento2#27499: Moved configuration xsd to MediaGalleryApi mo…
sivaschenko Apr 8, 2020
9f4bafc
magento/magento2#27499: MediaGallery bulk interfaces introduced
sivaschenko Apr 8, 2020
9106468
magento/magento2#27499: Extracted MediaGalleryCatalog module
sivaschenko Apr 9, 2020
96d3862
magento/magento2#27499: Corrected services implementation
sivaschenko Apr 9, 2020
e7a78fa
magento/magento2#27499: Fixed directory integration tests
sivaschenko Apr 9, 2020
f4d980a
magento/magento2#27499: Integration tests
sivaschenko Apr 9, 2020
ce08d34
magento/magento2#27499: Corrected comments and unit tests
sivaschenko Apr 9, 2020
6dfb2cc
magento/magento2#27499: Filtering data for insert
sivaschenko Apr 9, 2020
8e2337f
Merge remote-tracking branch 'mainline/2.4-develop' into sivaschenko/…
Apr 10, 2020
c13a5fb
magento/magento#227499: Intorduced ConfigInterface
sivaschenko Apr 14, 2020
30ce748
magento/magento2#27499: Code review fixes
Apr 14, 2020
9eecf12
Merge branch 'media-gallery-api' of github.com:sivaschenko/magento2 i…
Apr 14, 2020
6a94b59
magento/magento2#27499: Code review fixes
Apr 14, 2020
a77e990
Merge remote-tracking branch 'mainline/2.4-develop' into sivaschenko/…
Apr 14, 2020
e3ea27d
magento/magento2#27499: Code review fixes.
Apr 15, 2020
2e52006
Merge branch '2.4-develop' into media-gallery-api
lenaorobei Apr 16, 2020
024300a
Updated MediaGallery modules and marked as API
Apr 16, 2020
6e077c3
Merge branch 'media-gallery-api' of github.com:sivaschenko/magento2 i…
Apr 16, 2020
01ac38b
Updated MediaGallery modules and marked as API
Apr 16, 2020
1c7fd0a
Eliminate AbstractExtensibleModel usage
Apr 16, 2020
8919c70
Merge branch '2.4-develop' into media-gallery-api
sivaschenko Apr 17, 2020
c6c49b9
Merge branch '2.4-develop' of github.com:magento/magento2 into media-…
sivaschenko Apr 18, 2020
9a7a1c5
Fixed unit tests
sivaschenko Apr 18, 2020
21955c5
Fixed integration tests
sivaschenko Apr 18, 2020
c006590
Added blacklist to directory operations
sivaschenko Apr 18, 2020
8b3c9b1
Added MediaGalleryCatalog to composer.json
sivaschenko Apr 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/code/Magento/MediaGallery/Model/Asset/Command/GetById.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Psr\Log\LoggerInterface;

/**
* Class GetById
* Get media asset by id
*/
class GetById implements GetByIdInterface
{
Expand All @@ -28,7 +28,7 @@ class GetById implements GetByIdInterface
private $resourceConnection;

/**
* @var AssetInterface
* @var AssetInterfaceFactory
*/
private $assetFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class GetByPath implements GetByPathInterface
private $resourceConnection;

/**
* @var AssetInterface
* @var AssetInterfaceFactory
*/
private $mediaAssetFactory;

Expand Down Expand Up @@ -78,9 +78,7 @@ public function execute(string $mediaFilePath): AssetInterface
throw new NoSuchEntityException($message);
}

$mediaAssets = $this->mediaAssetFactory->create(['data' => $data]);

return $mediaAssets;
return $this->mediaAssetFactory->create(['data' => $data]);
} catch (\Exception $exception) {
$this->logger->critical($exception);
$message = __('An error occurred during get media asset list: %1', $exception->getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallery\Model\Directory\Command;

use Magento\Cms\Model\Wysiwyg\Images\Storage;
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\MediaGalleryApi\Model\Directory\Command\CreateByPathInterface;
use Psr\Log\LoggerInterface;

/**
* Create folder by provided path
*/
class CreateByPath implements CreateByPathInterface
{
/**
* @var LoggerInterface
*/
private $logger;

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

/**
* @param LoggerInterface $logger
* @param Storage $storage
*/
public function __construct(
LoggerInterface $logger,
Storage $storage
) {
$this->logger = $logger;
$this->storage = $storage;
}

/**
* Create new directory by the provided path in the media storage
*
* @param string $path
* @param string $name
* @throws CouldNotSaveException
*/
public function execute(string $path, string $name): void
{
try {
$this->storage->createDirectory($name, $this->storage->getCmsWysiwygImages()->getStorageRoot() . $path);
} catch (\Exception $exception) {
$this->logger->critical($exception);
throw new CouldNotSaveException(__('Failed to create the folder'), $exception);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallery\Model\Directory\Command;

use Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\Framework\Exception\CouldNotDeleteException;
use Magento\MediaGalleryApi\Model\Directory\Command\DeleteByPathInterface;
use Psr\Log\LoggerInterface;

/**
* Delete directory from media storage by path
*/
class DeleteByPath implements DeleteByPathInterface
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @var LoggerInterface
*/
private $logger;

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

/**
* @param LoggerInterface $logger
* @param Storage $storage
*/
public function __construct(
LoggerInterface $logger,
Storage $storage
) {
$this->logger = $logger;
$this->storage = $storage;
}

/**
* Removes directory and corresponding thumbnails directory from media storage if not in blacklist
*
* @param string $path
* @throws CouldNotDeleteException
*/
public function execute(string $path): void
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
{
try {
$this->storage->deleteDirectory($this->storage->getCmsWysiwygImages()->getStorageRoot() . $path);
} catch (\Exception $exception) {
$this->logger->critical($exception);
throw new CouldNotDeleteException(__('Failed to delete the folder'), $exception);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallery\Model\File\Command;

use Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\MediaGalleryApi\Model\File\Command\DeleteByAssetIdInterface;
use Magento\MediaGalleryApi\Model\Asset\Command\GetByIdInterface;

/**
* Load Media Asset path from database by id and delete the file
*/
class DeleteByAssetId implements DeleteByAssetIdInterface
{
/**
* @var GetByIdInterface
*/
private $getAssetById;

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

/**
* @var Filesystem
*/
private $filesystem;

/**
* DeleteAssetById constructor.
*
* @param GetByIdInterface $getAssetById
* @param Storage $imagesStorage
* @param Filesystem $filesystem
*/
public function __construct(
GetByIdInterface $getAssetById,
Storage $imagesStorage,
Filesystem $filesystem
) {
$this->getAssetById = $getAssetById;
$this->imagesStorage = $imagesStorage;
$this->filesystem = $filesystem;
}

/**
* Delete image by asset ID
*
* @param int $assetId
* @return void
* @throws LocalizedException
*/
public function execute(int $assetId): void
{
$mediaFilePath = $this->getAssetById->execute($assetId)->getPath();
$mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA);

if (!$mediaDirectory->isFile($mediaFilePath)) {
throw new LocalizedException(__('File "%1" does not exist in media directory.', $mediaFilePath));
}

$this->imagesStorage->deleteFile($mediaDirectory->getAbsolutePath() . $mediaFilePath);
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Psr\Log\LoggerInterface;

/**
* ClassGetAssetKeywords
* Retrieve keywords for the media asset
*/
class GetAssetKeywords implements GetAssetKeywordsInterface
{
Expand Down Expand Up @@ -59,7 +59,7 @@ public function __construct(
*
* @param int $assetId
*
* @return KeywordInterface[]|[]
* @return KeywordInterface[]
* @throws IntegrationException
*/
public function execute(int $assetId): array
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaGallery\Test\Unit\Model\File\Command;

use Magento\Cms\Model\Wysiwyg\Images\Storage;
use Magento\Framework\App\Filesystem\DirectoryList;
sidolov marked this conversation as resolved.
Show resolved Hide resolved
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\Read;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\MediaGalleryApi\Api\Data\AssetInterface;
use Magento\MediaGalleryApi\Model\Asset\Command\GetByIdInterface;
use Magento\MediaGallery\Model\File\Command\DeleteByAssetId;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Test the DeleteByAssetIdTest command model
*/
class DeleteByAssetIdTest extends TestCase
{
/**
* @var MockObject|Filesystem
*/
private $filesystem;

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

/**
* @var MockObject|GetByIdInterface
*/
private $getById;

/**
* @var DeleteByAssetId
*/
private $object;

/**
* Initialize basic test class mocks
*/
protected function setUp(): void
{
$this->filesystem = $this->createMock(Filesystem::class);
$this->storage = $this->createMock(Storage::class);
$this->getById = $this->createMock(GetByIdInterface::class);

$this->object = (new ObjectManager($this))->getObject(
DeleteByAssetId::class,
[
'filesystem' => $this->filesystem,
'imagesStorage' => $this->storage,
'getAssetById' => $this->getById
]
);
}

/**
* Test delete file by asset id
*/
public function testExecute(): void
{
$assetId = 42;
$path = '/file1.jpg';
$absoluteMediaPath = '/var/www/html/pub/media';

$asset = $this->createMock(AssetInterface::class);
$asset->expects($this->once())
->method('getPath')
->willReturn($path);

$this->getById->expects($this->once())
->method('execute')
->with($assetId)
->willReturn($asset);

$directory = $this->createMock(Read::class);
$directory->expects($this->once())
->method('isFile')
->willReturn(true);
$directory->expects($this->once())
->method('getAbsolutePath')
->willReturn($absoluteMediaPath);

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

$this->storage->expects($this->once())
->method('deleteFile')
->with($absoluteMediaPath . $path);

$this->object->execute($assetId);
}
}
4 changes: 4 additions & 0 deletions app/code/Magento/MediaGallery/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

<preference for="Magento\MediaGalleryApi\Model\DataExtractorInterface" type="Magento\MediaGallery\Model\DataExtractor"/>

<preference for="Magento\MediaGalleryApi\Model\Directory\Command\CreateByPathInterface" type="Magento\MediaGallery\Model\Directory\Command\CreateByPath"/>
<preference for="Magento\MediaGalleryApi\Model\Directory\Command\DeleteByPathInterface" type="Magento\MediaGallery\Model\Directory\Command\DeleteByPath"/>
<preference for="Magento\MediaGalleryApi\Model\File\Command\DeleteByAssetIdInterface" type="Magento\MediaGallery\Model\File\Command\DeleteByAssetId"/>

<type name="Magento\Catalog\Model\Product\Gallery\Processor">
<plugin name="media_gallery_image_remove_metadata" type="Magento\MediaGallery\Plugin\Product\Gallery\Processor"
sortOrder="10" disabled="false"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
/**
* Represents a media gallery asset which contains information about a media asset entity such
* as path to the media storage, media asset title and its content type, etc.
* @api
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
*/
interface AssetInterface extends ExtensibleDataInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* Represents a media gallery keyword. This object contains information about a media asset keyword entity.
* @api
*/
interface KeywordInterface extends ExtensibleDataInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
/**
* A command represents the media gallery assets delete action. A media gallery asset is filtered by directory
* path value.
* @api
sivaschenko marked this conversation as resolved.
Show resolved Hide resolved
*/
interface DeleteByDirectoryPathInterface
{
/**
* Delete media assets by directory path
*
* @param string $directoryPath
*
* @return void
*/
public function execute(string $directoryPath): void;
Expand Down
Loading