Skip to content

Commit

Permalink
Merge pull request #2091 from magento-trigger/MAGETWO-86533-MediaGall…
Browse files Browse the repository at this point in the history
…eryImage-Inside-Form

Magetwo 86533 media gallery image inside form
  • Loading branch information
heyitsroberthe authored Feb 23, 2018
2 parents c166940 + f26b717 commit a083717
Show file tree
Hide file tree
Showing 69 changed files with 1,979 additions and 291 deletions.
17 changes: 15 additions & 2 deletions app/code/Magento/Catalog/Model/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,22 @@ public function getImageUrl($attributeCode = 'image')
$image = $this->getData($attributeCode);
if ($image) {
if (is_string($image)) {
$url = $this->_storeManager->getStore()->getBaseUrl(
$store = $this->_storeManager->getStore();

$isRelativeUrl = substr($image, 0, 1) === '/';

$mediaBaseUrl = $store->getBaseUrl(
\Magento\Framework\UrlInterface::URL_TYPE_MEDIA
) . 'catalog/category/' . $image;
);

if ($isRelativeUrl) {
$url = $image;
} else {
$url = $mediaBaseUrl
. ltrim(\Magento\Catalog\Model\Category\FileInfo::ENTITY_MEDIA_PATH, '/')
. '/'
. $image;
}
} else {
throw new \Magento\Framework\Exception\LocalizedException(
__('Something went wrong while getting the image url.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Catalog\Model\Category\Attribute\Backend;

use Magento\Framework\App\Filesystem\DirectoryList;

/**
* Catalog category image attribute backend model
*
Expand Down Expand Up @@ -95,6 +97,11 @@ public function beforeSave($object)
$attributeName = $this->getAttribute()->getName();
$value = $object->getData($attributeName);

if ($this->fileResidesOutsideCategoryDir($value)) {
// use relative path for image attribute so we know it's outside of category dir when we fetch it
$value[0]['name'] = $value[0]['url'];
}

if ($imageName = $this->getUploadedImageName($value)) {
$object->setData($this->additionalData . $attributeName, $value);
$object->setData($attributeName, $imageName);
Expand Down Expand Up @@ -131,6 +138,26 @@ private function isTmpFileAvailable($value)
return is_array($value) && isset($value[0]['tmp_name']);
}

/**
* Check for file path resides outside of category media dir. The URL will be a path including pub/media if true
*
* @param array|null $value
* @return bool
*/
private function fileResidesOutsideCategoryDir($value)
{
if (!is_array($value) || !isset($value[0]['url'])) {
return false;
}

$fileUrl = ltrim($value[0]['url'], '/');
$baseMediaDir = $this->_filesystem->getUri(DirectoryList::MEDIA);

$usingPathRelativeToBase = strpos($fileUrl, $baseMediaDir) === 0;

return $usingPathRelativeToBase;
}

/**
* Save uploaded file and set its name to category
*
Expand All @@ -148,6 +175,7 @@ public function afterSave($object)
$this->_logger->critical($e);
}
}

return $this;
}
}
18 changes: 13 additions & 5 deletions app/code/Magento/Catalog/Model/Category/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,12 +492,20 @@ private function convertValues($category, $categoryData)
unset($categoryData[$attributeCode]);

$fileName = $category->getData($attributeCode);
if ($this->getFileInfo()->isExist($fileName)) {
$stat = $this->getFileInfo()->getStat($fileName);
$mime = $this->getFileInfo()->getMimeType($fileName);
$fileInfo = $this->getFileInfo();

if ($fileInfo->isExist($fileName)) {
$stat = $fileInfo->getStat($fileName);
$mime = $fileInfo->getMimeType($fileName);

$categoryData[$attributeCode][0]['name'] = basename($fileName);

if ($fileInfo->isBeginsWithMediaDirectoryPath($fileName)) {
$categoryData[$attributeCode][0]['url'] = $fileName;
} else {
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
}

$categoryData[$attributeCode][0]['name'] = $fileName;
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
$categoryData[$attributeCode][0]['size'] = isset($stat) ? $stat['size'] : 0;
$categoryData[$attributeCode][0]['type'] = $mime;
}
Expand Down
82 changes: 79 additions & 3 deletions app/code/Magento/Catalog/Model/Category/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\File\Mime;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\Filesystem\Directory\ReadInterface;

/**
* Class FileInfo
Expand Down Expand Up @@ -37,6 +38,11 @@ class FileInfo
*/
private $mediaDirectory;

/**
* @var ReadInterface
*/
private $baseDirectory;

/**
* @param Filesystem $filesystem
* @param Mime $mime
Expand All @@ -62,6 +68,20 @@ private function getMediaDirectory()
return $this->mediaDirectory;
}

/**
* Get Base Directory read instance
*
* @return ReadInterface
*/
private function getBaseDirectory()
{
if (!isset($this->baseDirectory)) {
$this->baseDirectory = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
}

return $this->baseDirectory;
}

/**
* Retrieve MIME type of requested file
*
Expand All @@ -70,7 +90,7 @@ private function getMediaDirectory()
*/
public function getMimeType($fileName)
{
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
$filePath = $this->getFilePath($fileName);
$absoluteFilePath = $this->getMediaDirectory()->getAbsolutePath($filePath);

$result = $this->mime->getMimeType($absoluteFilePath);
Expand All @@ -85,7 +105,7 @@ public function getMimeType($fileName)
*/
public function getStat($fileName)
{
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
$filePath = $this->getFilePath($fileName);

$result = $this->getMediaDirectory()->stat($filePath);
return $result;
Expand All @@ -99,9 +119,65 @@ public function getStat($fileName)
*/
public function isExist($fileName)
{
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
$filePath = $this->getFilePath($fileName);

$result = $this->getMediaDirectory()->isExist($filePath);
return $result;
}

/**
* Construct and return file subpath based on filename relative to media directory
*
* @param string $fileName
* @return string
*/
private function getFilePath($fileName)
{
$filePath = ltrim($fileName, '/');

$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
$isFileNameBeginsWithMediaDirectoryPath = $this->isBeginsWithMediaDirectoryPath($fileName);

// if the file is not using a relative path, it resides in the catalog/category media directory
$fileIsInCategoryMediaDir = !$isFileNameBeginsWithMediaDirectoryPath;

if ($fileIsInCategoryMediaDir) {
$filePath = self::ENTITY_MEDIA_PATH . '/' . $filePath;
} else {
$filePath = substr($filePath, strlen($mediaDirectoryRelativeSubpath));
}

return $filePath;
}

/**
* Checks for whether $fileName string begins with media directory path
*
* @param string $fileName
* @return bool
*/
public function isBeginsWithMediaDirectoryPath($fileName)
{
$filePath = ltrim($fileName, '/');

$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
$isFileNameBeginsWithMediaDirectoryPath = strpos($filePath, $mediaDirectoryRelativeSubpath) === 0;

return $isFileNameBeginsWithMediaDirectoryPath;
}

/**
* Get media directory subpath relative to base directory path
*
* @return string
*/
private function getMediaDirectoryPathRelativeToBaseDirectoryPath()
{
$baseDirectoryPath = $this->getBaseDirectory()->getAbsolutePath();
$mediaDirectoryPath = $this->getMediaDirectory()->getAbsolutePath();

$mediaDirectoryRelativeSubpath = substr($mediaDirectoryPath, strlen($baseDirectoryPath));

return $mediaDirectoryRelativeSubpath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Catalog\Test\Unit\Model\Category\Attribute\Backend;

use Magento\Framework\App\Filesystem\DirectoryList;

class ImageTest extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -27,6 +29,11 @@ class ImageTest extends \PHPUnit\Framework\TestCase
*/
private $logger;

/**
* @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
*/
private $filesystem;

protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -59,6 +66,9 @@ protected function setUp()
\Magento\Catalog\Model\ImageUploader::class,
['moveFileFromTmp']
);

$this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)->disableOriginalConstructor()
->getMock();
}

/**
Expand Down Expand Up @@ -144,6 +154,38 @@ public function testBeforeSaveAttributeFileName()
$this->assertEquals('test123.jpg', $object->getTestAttribute());
}

public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
{
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class, [
'filesystem' => $this->filesystem
]);

$model->setAttribute($this->attribute);

$this->filesystem
->expects($this->once())
->method('getUri')
->with(DirectoryList::MEDIA)
->willReturn('pub/media');

$object = new \Magento\Framework\DataObject([
'test_attribute' => [
[
'name' => '/test123.jpg',
'url' => '/pub/media/wysiwyg/test123.jpg',
]
]
]);

$model->beforeSave($object);

$this->assertEquals('/pub/media/wysiwyg/test123.jpg', $object->getTestAttribute());
$this->assertEquals(
[['name' => '/pub/media/wysiwyg/test123.jpg', 'url' => '/pub/media/wysiwyg/test123.jpg']],
$object->getData('_additional_data_test_attribute')
);
}

public function testBeforeSaveTemporaryAttribute()
{
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
Expand Down
Loading

0 comments on commit a083717

Please sign in to comment.