Skip to content

Deleted lib/internal/Magento/Framework/Filesystem/ExtendedDriverInterface.php. getMetadata() method moved to DataInterface #32531

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

Open
wants to merge 5 commits into
base: 2.5-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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/Eav/Model/Attribute/Data/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Magento\Eav\Model\Attribute\Data;

use Magento\Framework\Filesystem\ExtendedDriverInterface;
use Magento\RemoteStorage\Driver\RemoteDriverInterface;

/**
* EAV Entity Attribute Image File Data Model
Expand All @@ -29,7 +29,7 @@ protected function _validateByRules($value)
{
$label = __($this->getAttribute()->getStoreLabel());
$rules = $this->getAttribute()->getValidateRules();
$localStorage = !$this->_directory->getDriver() instanceof ExtendedDriverInterface;
$localStorage = !$this->_directory->getDriver() instanceof RemoteDriverInterface;
$imageProp = $localStorage
? @getimagesize($value['tmp_name'])
: $this->_directory->getDriver()->getMetadata($value['tmp_name']);
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/Eav/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"magento/module-catalog": "*",
"magento/module-config": "*",
"magento/module-media-storage": "*",
"magento/module-store": "*"
"magento/module-store": "*",
"magento/module-remote-storage": "*"
},
"type": "magento2-module",
"license": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\MediaGalleryApi\Api\Data\AssetInterfaceFactory;
use Magento\MediaGallerySynchronization\Model\Filesystem\GetFileInfo;
use Magento\MediaGallerySynchronizationApi\Model\CreateAssetFromFileInterface;
use Magento\RemoteStorage\Driver\RemoteDriverInterface;

/**
* Create media asset object based on the file information
Expand Down Expand Up @@ -75,7 +76,7 @@ public function execute(string $path): AssetInterface
$absolutePath = $this->getMediaDirectory()->getAbsolutePath($path);
$driver = $this->getMediaDirectory()->getDriver();

if ($driver instanceof Filesystem\ExtendedDriverInterface) {
if ($driver instanceof RemoteDriverInterface) {
$meta = $driver->getMetadata($absolutePath);
} else {
/**
Expand Down
3 changes: 2 additions & 1 deletion app/code/Magento/MediaGallerySynchronization/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"magento/framework": "*",
"magento/module-media-gallery-api": "*",
"magento/module-media-gallery-synchronization-api": "*",
"magento/framework-message-queue": "*"
"magento/framework-message-queue": "*",
"magento/module-remote-storage": "*"
},
"type": "magento2-module",
"license": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace Magento\RemoteStorage\Driver;

use Magento\Framework\Filesystem\ExtendedDriverInterface;
use Magento\Framework\Filesystem\DriverInterface;

/**
* Remote storage driver.
*/
interface RemoteDriverInterface extends ExtendedDriverInterface
interface RemoteDriverInterface extends DriverInterface
{
/**
* Test storage connection.
Expand Down
14 changes: 9 additions & 5 deletions lib/internal/Magento/Framework/File/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Utility for mime type retrieval
*
* @deprecated
* @see Filesystem\ExtendedDriverInterface::getMetadata()
* @see Filesystem\DriverInterface::getMetadata()
*/
class Mime
{
Expand Down Expand Up @@ -113,12 +113,16 @@ public function getMimeType($file)
throw new FileSystemException(__("File '$file' doesn't exist"));
}

if ($driver instanceof Filesystem\ExtendedDriverInterface) {
return $driver->getMetadata($file)['mimetype'];
$mimeType = '';
if ($driver instanceof Filesystem\DriverInterface) {
$mimeType = $driver->getMetadata($file)['mimetype'] ?? '';
}

$mime = new Filesystem\Driver\File\Mime();
if (!$mimeType) {
$mime = new Filesystem\Driver\File\Mime();
$mimeType = $mime->getMimeType($file);
}

return $mime->getMimeType($file);
return $mimeType;
}
}
8 changes: 5 additions & 3 deletions lib/internal/Magento/Framework/File/Test/Unit/MimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MimeTest extends TestCase
protected function setUp(): void
{
$this->localDriverMock = $this->getMockForAbstractClass(Filesystem\DriverInterface::class);
$this->remoteDriverMock = $this->getMockForAbstractClass(Filesystem\ExtendedDriverInterface::class);
$this->remoteDriverMock = $this->getMockForAbstractClass(Filesystem\DriverInterface::class);

$this->localDirectoryMock = $this->getMockForAbstractClass(Filesystem\Directory\WriteInterface::class);
$this->localDirectoryMock->method('getDriver')
Expand All @@ -83,7 +83,8 @@ public function testGetMimeTypeNonexistentFileException(): void
->method('isExists')
->with('nonexistent.file')
->willReturn(true);

$this->localDriverMock->method('getMetadata')
->willThrowException(new FileSystemException(__('File \'nonexistent.file\' doesn\'t exist')));
$file = 'nonexistent.file';
$this->object->getMimeType($file);
}
Expand All @@ -103,7 +104,8 @@ public function testGetMimeType($file, $expectedType): void
->method('isExists')
->with($file)
->willReturn(true);

$this->localDriverMock->method('getMetadata')
->willReturn(['mimetype' => $expectedType]);
$actualType = $this->object->getMimeType($file);
self::assertSame($expectedType, $actualType);
}
Expand Down
65 changes: 65 additions & 0 deletions lib/internal/Magento/Framework/Filesystem/Driver/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Framework\Filesystem\Driver;

use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Filesystem\Driver\File\Mime;
use Magento\Framework\Filesystem\DriverInterface;
use Magento\Framework\Filesystem\Glob;
use Magento\Framework\Phrase;
Expand Down Expand Up @@ -1096,4 +1097,68 @@ public function getRealPathSafety($path)

return rtrim(implode(DIRECTORY_SEPARATOR, $realPath), DIRECTORY_SEPARATOR);
}

/**
* Retrieve file metadata.
*
* @param string $path
*
* @return array
* @throws FileSystemException
*/
public function getMetadata(string $path): array
{
if (!$this->isExists($path) || !$this->isFile($path)) {
throw new FileSystemException(__("File '$path' doesn't exist"));
}

$file = new \SplFileInfo($path);

$mimeType = $this->getFileMimeType($path);

if ($this->isFileAnImage($mimeType)) {
$imageInfo = getimagesize($path);
$mimeType = $imageInfo['mime'] ?? $mimeType;
}

return [
'path' => $file->getPath(),
'dirname' => dirname($file->getPath()),
'basename' => $file->getBasename(),
'extension' => $file->getExtension(),
'filename' => $file->getFilename(),
'timestamp' => $file->getMTime(),
'size' => $file->getSize(),
'mimetype' => $mimeType,
'extra' => [
'image-width' => $imageInfo[0] ?? 0,
'image-height' => $imageInfo[1] ?? 0
]
];
}

/**
* Checks whether file is an image
*
* @param string $mimeType
*
* @return bool
*/
protected function isFileAnImage(string $mimeType): bool
{
return strstr($mimeType, 'image/');
}

/**
* Returns file mime type
*
* @param string $path
*
* @return string
* @throws FileSystemException
*/
private function getFileMimeType(string $path): string
{
return Mime::getMimeInstance()->getMimeType($path);
}
}
19 changes: 19 additions & 0 deletions lib/internal/Magento/Framework/Filesystem/Driver/File/Mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class Mime
'inode/x-empty',
];

/**
* @var Mime
*/
private static $instance = null;

/**
* Get mime type of a file
*
Expand Down Expand Up @@ -125,6 +130,20 @@ public function getMimeType(string $path): string
return $result;
}

/**
* Get Mime instance
*
* @return static
*/
public static function getMimeInstance(): Mime
{
if (null === static::$instance) {
static::$instance = new static();
}

return static::$instance;
}

/**
* Get mime type by the native mime_content_type function.
*
Expand Down
46 changes: 46 additions & 0 deletions lib/internal/Magento/Framework/Filesystem/Driver/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Magento\Framework\Filesystem\Driver;

use League\Flysystem\AdapterInterface;
use Magento\Framework\Exception\FileSystemException;

/**
Expand All @@ -22,6 +23,11 @@ class Http extends File
*/
protected $scheme = 'http';

/**
* @var AdapterInterface
*/
private $adapter;

/**
* Checks if path exists
*
Expand Down Expand Up @@ -228,6 +234,46 @@ public function getAbsolutePath($basePath, $path, $scheme = null)
return $this->getScheme() . $basePath . $path;
}

/**
* Retrieve file metadata.
*
* @param string $path
*
* @return array
* @throws FileSystemException
*/
public function getMetadata(string $path): array
{
if (!$this->isExists($path)) {
throw new FileSystemException(__("File '$path' doesn't exist"));
}

$fileStat = $this->stat($path);
$mimeType = $fileStat['type'] ?? '';

$file = new \SplFileInfo($path);

if ($this->isFileAnImage($mimeType)) {
$imageInfo = getimagesize($this->getScheme() . $path);
$mimeType = $imageInfo['mime'] ?? $mimeType;
}

return [
'path' => $file->getPath(),
'dirname' => dirname($file->getPath()),
'basename' => $file->getBasename(),
'extension' => $file->getExtension(),
'filename' => $file->getFilename(),
'timestamp' => isset($fileStat['mtime']) ? strtotime($fileStat['mtime']) : 0,
'size' => $fileStat['size'] ?? 0,
'mimetype' => $mimeType,
'extra' => [
'image-width' => $imageInfo[0] ?? 0,
'image-height' => $imageInfo[1] ?? 0
]
];
}

/**
* Return path with scheme
*
Expand Down
13 changes: 13 additions & 0 deletions lib/internal/Magento/Framework/Filesystem/Driver/StatefulFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,17 @@ public function getRealPathSafety($path)
{
return $this->driverFile->getRealPathSafety($path);
}

/**
* Retrieve file metadata
*
* @param string $path
*
* @return array
* @throws FileSystemException
*/
public function getMetadata(string $path): array
{
return $this->driverFile->getMetadata($path);
}
}
27 changes: 27 additions & 0 deletions lib/internal/Magento/Framework/Filesystem/DriverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,31 @@ public function getRealPathSafety($path);
* @return mixed
*/
public function getRelativePath($basePath, $path = null);

/**
* Retrieve file metadata.
*
* Implementation must return associative array with next keys:
*
* ```
* [
* 'path',
* 'dirname',
* 'basename',
* 'extension',
* 'filename',
* 'timestamp',
* 'size',
* 'mimetype',
* 'extra' => [
* 'image-width',
* 'image-height'
* ]
* ];
*
* @param string $path Absolute path to file
* @return array
* @throws FileSystemException
*/
public function getMetadata(string $path): array;
}
Loading