Skip to content

Commit d7d86c4

Browse files
committed
Deleted lib/internal/Magento/Framework/Filesystem/ExtendedDriverInterface.php. getMetadata() method moved to DataInterface
1 parent 6ac4cda commit d7d86c4

File tree

9 files changed

+310
-52
lines changed

9 files changed

+310
-52
lines changed

app/code/Magento/RemoteStorage/Driver/RemoteDriverInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
namespace Magento\RemoteStorage\Driver;
99

10-
use Magento\Framework\Filesystem\ExtendedDriverInterface;
10+
use Magento\Framework\Filesystem\DriverInterface;
1111

1212
/**
1313
* Remote storage driver.
1414
*/
15-
interface RemoteDriverInterface extends ExtendedDriverInterface
15+
interface RemoteDriverInterface extends DriverInterface
1616
{
1717
/**
1818
* Test storage connection.

lib/internal/Magento/Framework/Filesystem/Driver/File.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\Framework\Filesystem\Driver;
99

1010
use Magento\Framework\Exception\FileSystemException;
11+
use Magento\Framework\Filesystem\Driver\File\Mime;
1112
use Magento\Framework\Filesystem\DriverInterface;
1213
use Magento\Framework\Filesystem\Glob;
1314
use Magento\Framework\Phrase;
@@ -1096,4 +1097,68 @@ public function getRealPathSafety($path)
10961097

10971098
return rtrim(implode(DIRECTORY_SEPARATOR, $realPath), DIRECTORY_SEPARATOR);
10981099
}
1100+
1101+
/**
1102+
* Retrieve file metadata.
1103+
*
1104+
* @param string $path
1105+
*
1106+
* @return array
1107+
* @throws FileSystemException
1108+
*/
1109+
public function getMetadata(string $path): array
1110+
{
1111+
if (!$this->isExists($path) || !$this->isFile($path)) {
1112+
throw new FileSystemException(__("File '$path' doesn't exist"));
1113+
}
1114+
1115+
$file = new \SplFileInfo($path);
1116+
1117+
$mimeType = $this->getFileMimeType($path);
1118+
1119+
if ($this->isFileAnImage($mimeType)) {
1120+
$imageInfo = getimagesize($path);
1121+
$mimeType = $imageInfo['mime'] ?? $mimeType;
1122+
}
1123+
1124+
return [
1125+
'path' => $file->getPath(),
1126+
'dirname' => dirname($file->getPath()),
1127+
'basename' => $file->getBasename(),
1128+
'extension' => $file->getExtension(),
1129+
'filename' => $file->getFilename(),
1130+
'timestamp' => $file->getMTime(),
1131+
'size' => $file->getSize(),
1132+
'mimetype' => $mimeType,
1133+
'extra' => [
1134+
'image-width' => $imageInfo[0] ?? 0,
1135+
'image-height' => $imageInfo[1] ?? 0
1136+
]
1137+
];
1138+
}
1139+
1140+
/**
1141+
* Checks whether file is an image
1142+
*
1143+
* @param string $mimeType
1144+
*
1145+
* @return bool
1146+
*/
1147+
private function isFileAnImage(string $mimeType): bool
1148+
{
1149+
return strstr($mimeType, 'image/');
1150+
}
1151+
1152+
/**
1153+
* Returns file mime type
1154+
*
1155+
* @param string $path
1156+
*
1157+
* @return string
1158+
* @throws FileSystemException
1159+
*/
1160+
private function getFileMimeType(string $path): string
1161+
{
1162+
return Mime::getMimeInstance()->getMimeType($path);
1163+
}
10991164
}

lib/internal/Magento/Framework/Filesystem/Driver/File/Mime.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class Mime
9393
'inode/x-empty',
9494
];
9595

96+
/**
97+
* @var Mime
98+
*/
99+
private static $instance = null;
100+
96101
/**
97102
* Get mime type of a file
98103
*
@@ -125,6 +130,20 @@ public function getMimeType(string $path): string
125130
return $result;
126131
}
127132

133+
/**
134+
* Get Mime instance
135+
*
136+
* @return static
137+
*/
138+
public static function getMimeInstance(): Mime
139+
{
140+
if (null === static::$instance) {
141+
static::$instance = new static();
142+
}
143+
144+
return static::$instance;
145+
}
146+
128147
/**
129148
* Get mime type by the native mime_content_type function.
130149
*

lib/internal/Magento/Framework/Filesystem/Driver/StatefulFile.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,17 @@ public function getRealPathSafety($path)
565565
{
566566
return $this->driverFile->getRealPathSafety($path);
567567
}
568+
569+
/**
570+
* Retrieve file metadata
571+
*
572+
* @param string $path
573+
*
574+
* @return array
575+
* @throws FileSystemException
576+
*/
577+
public function getMetadata(string $path): array
578+
{
579+
return $this->driverFile->getMetadata($path);
580+
}
568581
}

lib/internal/Magento/Framework/Filesystem/DriverInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,31 @@ public function getRealPathSafety($path);
393393
* @return mixed
394394
*/
395395
public function getRelativePath($basePath, $path = null);
396+
397+
/**
398+
* Retrieve file metadata.
399+
*
400+
* Implementation must return associative array with next keys:
401+
*
402+
* ```
403+
* [
404+
* 'path',
405+
* 'dirname',
406+
* 'basename',
407+
* 'extension',
408+
* 'filename',
409+
* 'timestamp',
410+
* 'size',
411+
* 'mimetype',
412+
* 'extra' => [
413+
* 'image-width',
414+
* 'image-height'
415+
* ]
416+
* ];
417+
*
418+
* @param string $path Absolute path to file
419+
* @return array
420+
* @throws FileSystemException
421+
*/
422+
public function getMetadata(string $path): array;
396423
}

lib/internal/Magento/Framework/Filesystem/ExtendedDriverInterface.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

lib/internal/Magento/Framework/Filesystem/Test/Unit/Driver/File/MimeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public function testGetMimeType(string $file, string $expectedType): void
5151
self::assertSame($expectedType, $actualType);
5252
}
5353

54+
/**
55+
* Test for getMimeInstance
56+
*/
57+
public function testGetMimeInstance(): void
58+
{
59+
$mime = Mime::getMimeInstance();
60+
$this->assertInstanceOf(Mime::class, $mime);
61+
}
62+
5463
/**
5564
* @return array
5665
*/
Loading

0 commit comments

Comments
 (0)