Skip to content

Commit 304bb0c

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-59924' into BUGS
# Conflicts: # app/code/Magento/Catalog/Model/Category/DataProvider.php
2 parents ea115ca + 65c0f57 commit 304bb0c

File tree

28 files changed

+3299
-2392
lines changed

28 files changed

+3299
-2392
lines changed

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\Eav\Model\Entity\Type;
1616
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory as CategoryCollectionFactory;
1717
use Magento\Framework\Stdlib\ArrayManager;
18+
use Magento\Framework\App\ObjectManager;
19+
use Magento\Framework\Filesystem;
1820
use Magento\Store\Model\Store;
1921
use Magento\Store\Model\StoreManagerInterface;
2022
use Magento\Ui\Component\Form\Field;
@@ -126,6 +128,11 @@ class DataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
126128
*/
127129
private $arrayManager;
128130

131+
/**
132+
* @var Filesystem
133+
*/
134+
private $fileInfo;
135+
129136
/**
130137
* DataProvider constructor
131138
*
@@ -483,8 +490,16 @@ private function convertValues($category, $categoryData)
483490
if ($attribute->getBackend() instanceof ImageBackendModel) {
484491
unset($categoryData[$attributeCode]);
485492

486-
$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
487-
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
493+
$fileName = $category->getData($attributeCode);
494+
if ($this->getFileInfo()->isExist($fileName)) {
495+
$stat = $this->getFileInfo()->getStat($fileName);
496+
$mime = $this->getFileInfo()->getMimeType($fileName);
497+
498+
$categoryData[$attributeCode][0]['name'] = $fileName;
499+
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
500+
$categoryData['image'][0]['size'] = isset($stat) ? $stat['size'] : 0;
501+
$categoryData['image'][0]['type'] = $mime;
502+
}
488503
}
489504
}
490505

@@ -605,4 +620,19 @@ private function getArrayManager()
605620

606621
return $this->arrayManager;
607622
}
623+
624+
/**
625+
* Get FileInfo instance
626+
*
627+
* @return FileInfo
628+
*
629+
* @deprecated
630+
*/
631+
private function getFileInfo()
632+
{
633+
if ($this->fileInfo === null) {
634+
$this->fileInfo = ObjectManager::getInstance()->get(FileInfo::class);
635+
}
636+
return $this->fileInfo;
637+
}
608638
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\Category;
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\File\Mime;
10+
use Magento\Framework\Filesystem;
11+
use Magento\Framework\Filesystem\Directory\WriteInterface;
12+
13+
/**
14+
* Class FileInfo
15+
*
16+
* Provides information about requested file
17+
*/
18+
class FileInfo
19+
{
20+
/**
21+
* Path in /pub/media directory
22+
*/
23+
const ENTITY_MEDIA_PATH = '/catalog/category';
24+
25+
/**
26+
* @var Filesystem
27+
*/
28+
private $filesystem;
29+
30+
/**
31+
* @var Mime
32+
*/
33+
private $mime;
34+
35+
/**
36+
* @var WriteInterface
37+
*/
38+
private $mediaDirectory;
39+
40+
/**
41+
* @param Filesystem $filesystem
42+
* @param Mime $mime
43+
*/
44+
public function __construct(
45+
Filesystem $filesystem,
46+
Mime $mime
47+
) {
48+
$this->filesystem = $filesystem;
49+
$this->mime = $mime;
50+
}
51+
52+
/**
53+
* Get WriteInterface instance
54+
*
55+
* @return WriteInterface
56+
*/
57+
private function getMediaDirectory()
58+
{
59+
if ($this->mediaDirectory === null) {
60+
$this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
61+
}
62+
return $this->mediaDirectory;
63+
}
64+
65+
/**
66+
* Retrieve MIME type of requested file
67+
*
68+
* @param string $fileName
69+
* @return string
70+
*/
71+
public function getMimeType($fileName)
72+
{
73+
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
74+
$absoluteFilePath = $this->getMediaDirectory()->getAbsolutePath($filePath);
75+
76+
$result = $this->mime->getMimeType($absoluteFilePath);
77+
return $result;
78+
}
79+
80+
/**
81+
* Get file statistics data
82+
*
83+
* @param string $fileName
84+
* @return array
85+
*/
86+
public function getStat($fileName)
87+
{
88+
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
89+
90+
$result = $this->getMediaDirectory()->stat($filePath);
91+
return $result;
92+
}
93+
94+
/**
95+
* Check if the file exists
96+
*
97+
* @param string $fileName
98+
* @return bool
99+
*/
100+
public function isExist($fileName)
101+
{
102+
$filePath = self::ENTITY_MEDIA_PATH . '/' . ltrim($fileName, '/');
103+
104+
$result = $this->getMediaDirectory()->isExist($filePath);
105+
return $result;
106+
}
107+
}

0 commit comments

Comments
 (0)