_date = $date; $this->filesystem = $filesystem; } /** * Collect file info * * Return array( * filename => string * content => string|bool * update_time => string * directory => string * * @param string $mediaDirectory * @param string $path * @return array * @throws \Magento\Framework\Exception\LocalizedException */ public function collectFileInfo($mediaDirectory, $path) { $path = ltrim($path, '\\/'); $fullPath = $mediaDirectory . '/' . $path; $dir = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); $relativePath = $dir->getRelativePath($fullPath); if (!$dir->isFile($relativePath)) { throw new \Magento\Framework\Exception\LocalizedException( __('The "%1" file doesn\'t exist. Verify the file and try again.', $fullPath) ); } if (!$dir->isReadable($relativePath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File %1 is not readable', $fullPath)); } $path = str_replace(['/', '\\'], '/', $path); $directory = dirname($path); if ($directory == '.') { $directory = null; } return [ 'filename' => basename($path), 'content' => $dir->readFile($relativePath), 'update_time' => $this->_date->date(), 'directory' => $directory ]; } }