Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FileInfo.php
Go to the documentation of this file.
1 <?php
7 
13 
19 class FileInfo
20 {
24  const ENTITY_MEDIA_PATH = '/catalog/category';
25 
29  private $filesystem;
30 
34  private $mime;
35 
39  private $mediaDirectory;
40 
44  private $baseDirectory;
45 
50  public function __construct(
51  Filesystem $filesystem,
52  Mime $mime
53  ) {
54  $this->filesystem = $filesystem;
55  $this->mime = $mime;
56  }
57 
63  private function getMediaDirectory()
64  {
65  if ($this->mediaDirectory === null) {
66  $this->mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA);
67  }
68  return $this->mediaDirectory;
69  }
70 
76  private function getBaseDirectory()
77  {
78  if (!isset($this->baseDirectory)) {
79  $this->baseDirectory = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
80  }
81 
82  return $this->baseDirectory;
83  }
84 
91  public function getMimeType($fileName)
92  {
93  $filePath = $this->getFilePath($fileName);
94  $absoluteFilePath = $this->getMediaDirectory()->getAbsolutePath($filePath);
95 
96  $result = $this->mime->getMimeType($absoluteFilePath);
97  return $result;
98  }
99 
106  public function getStat($fileName)
107  {
108  $filePath = $this->getFilePath($fileName);
109 
110  $result = $this->getMediaDirectory()->stat($filePath);
111  return $result;
112  }
113 
120  public function isExist($fileName)
121  {
122  $filePath = $this->getFilePath($fileName);
123 
124  $result = $this->getMediaDirectory()->isExist($filePath);
125  return $result;
126  }
127 
134  private function getFilePath($fileName)
135  {
136  $filePath = ltrim($fileName, '/');
137 
138  $mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
139  $isFileNameBeginsWithMediaDirectoryPath = $this->isBeginsWithMediaDirectoryPath($fileName);
140 
141  // if the file is not using a relative path, it resides in the catalog/category media directory
142  $fileIsInCategoryMediaDir = !$isFileNameBeginsWithMediaDirectoryPath;
143 
144  if ($fileIsInCategoryMediaDir) {
145  $filePath = self::ENTITY_MEDIA_PATH . '/' . $filePath;
146  } else {
147  $filePath = substr($filePath, strlen($mediaDirectoryRelativeSubpath));
148  }
149 
150  return $filePath;
151  }
152 
160  {
161  $filePath = ltrim($fileName, '/');
162 
163  $mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
164  $isFileNameBeginsWithMediaDirectoryPath = strpos($filePath, $mediaDirectoryRelativeSubpath) === 0;
165 
166  return $isFileNameBeginsWithMediaDirectoryPath;
167  }
168 
174  private function getMediaDirectoryPathRelativeToBaseDirectoryPath()
175  {
176  $baseDirectoryPath = $this->getBaseDirectory()->getAbsolutePath();
177  $mediaDirectoryPath = $this->getMediaDirectory()->getAbsolutePath();
178 
179  $mediaDirectoryRelativeSubpath = substr($mediaDirectoryPath, strlen($baseDirectoryPath));
180 
181  return $mediaDirectoryRelativeSubpath;
182  }
183 }
__construct(Filesystem $filesystem, Mime $mime)
Definition: FileInfo.php:50
$fileName
Definition: translate.phtml:15
$filesystem