/** * Check var/generation read and write access * * @return bool */ public function check() { $initParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM); $filesystemDirPaths = isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]) ? $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] : []; $directoryList = new DirectoryList(BP, $filesystemDirPaths); $generationDirectoryPath = $directoryList->getPath(DirectoryList::GENERATION); $driverPool = new DriverPool(); $fileWriteFactory = new WriteFactory($driverPool); /** @var \Magento\Framework\Filesystem\DriverInterface $driver */ $driver = $driverPool->getDriver(DriverPool::FILE); $directoryWrite = new Write($fileWriteFactory, $driver, $generationDirectoryPath); if ($directoryWrite->isExist()) { if ($directoryWrite->isDirectory() || $directoryWrite->isReadable()) { try { $probeFilePath = $generationDirectoryPath . DIRECTORY_SEPARATOR . uniqid(mt_rand()) . 'tmp'; $fileWriteFactory->create($probeFilePath, DriverPool::FILE, 'w'); $driver->deleteFile($probeFilePath); } catch (\Exception $e) { return false; } } else { return false; } } else { try { $directoryWrite->create(); } catch (\Exception $e) { return false; } } return true; }
/** * Create thumbnail for image and save it to thumbnails directory * * @param string $source Image path to be resized * @param bool $keepRation Keep aspect ratio or not * @return bool|string Resized filepath or false if errors were occurred */ public function resizeFile($source, $keepRation = true) { $realPath = $this->_directory->getRelativePath($source); if (!$this->_directory->isFile($realPath) || !$this->_directory->isExist($realPath)) { return false; } $targetDir = $this->getThumbsPath($source); $pathTargetDir = $this->_directory->getRelativePath($targetDir); if (!$this->_directory->isExist($pathTargetDir)) { $this->_directory->create($pathTargetDir); } if (!$this->_directory->isExist($pathTargetDir)) { return false; } $image = $this->_imageFactory->create(); $image->open($source); $image->keepAspectRatio($keepRation); $image->resize($this->_resizeParameters['width'], $this->_resizeParameters['height']); $dest = $targetDir . '/' . pathinfo($source, PATHINFO_BASENAME); $image->save($dest); if ($this->_directory->isFile($this->_directory->getRelativePath($dest))) { return $dest; } return false; }
/** * Get thumbnail path in current directory by image name * * @param string $imageName * @return string * @throws \InvalidArgumentException */ public function getThumbnailPath($imageName) { $imagePath = $this->getCurrentPath() . '/' . $imageName; if (!$this->mediaDirectoryWrite->isExist($imagePath) || 0 !== strpos($imagePath, $this->getStorageRoot())) { throw new \InvalidArgumentException('The image not found.'); } return $this->getThumbnailDirectory($imagePath) . '/' . pathinfo($imageName, PATHINFO_BASENAME); }
/** * Delete all files in a directory recursively * * @param string $targetDir * @return void */ protected function _deleteFilesRecursively($targetDir) { if ($this->_directory->isExist($targetDir)) { foreach ($this->_directory->read($targetDir) as $path) { $this->_directory->delete($path); } } }
/** * Load composer packages available for update from cache * * @return bool|string */ private function loadPackagesForUpdateFromCache() { if ($this->directory->isExist($this->pathToCacheFile) && $this->directory->isReadable($this->pathToCacheFile)) { try { $data = $this->directory->readFile($this->pathToCacheFile); return json_decode($data, true); } catch (\Magento\Framework\Exception\FileSystemException $e) { } } return false; }
/** * Get directory collection * * @param string $currentPath * @return array * @throws \Magento\Framework\Exception\LocalizedException */ public function getDirsCollection($currentPath) { if (!$this->mediaWriteDirectory->isExist($currentPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot find a directory with this name.')); } $paths = $this->mediaWriteDirectory->search('.*', $currentPath); $directories = []; foreach ($paths as $path) { if ($this->mediaWriteDirectory->isDirectory($path)) { $directories[] = $path; } } return $directories; }
/** * Add sitemap file to robots.txt * * @param string $sitemapFileName * @return void */ protected function _addSitemapToRobotsTxt($sitemapFileName) { $robotsSitemapLine = 'Sitemap: ' . $this->getSitemapUrl($this->getSitemapPath(), $sitemapFileName); $filename = 'robots.txt'; $content = ''; if ($this->_directory->isExist($filename)) { $content = $this->_directory->readFile($filename); } if (strpos($content, $robotsSitemapLine) === false) { if (!empty($content)) { $content .= $this->_findNewLinesDelimiter($content); } $content .= $robotsSitemapLine; } $this->_directory->writeFile($filename, $content); }
/** * Export attribute value to entity model * * @param array|string $value * @return $this */ public function compactValue($value) { if ($this->getIsAjaxRequest()) { return $this; } $attribute = $this->getAttribute(); $original = $this->getEntity()->getData($attribute->getAttributeCode()); $toDelete = false; if ($original) { if (!$attribute->getIsRequired() && !empty($value['delete'])) { $toDelete = true; } if (!empty($value['tmp_name'])) { $toDelete = true; } } $destinationFolder = $attribute->getEntity()->getEntityTypeCode(); // unlink entity file if ($toDelete) { $this->getEntity()->setData($attribute->getAttributeCode(), ''); $file = $destinationFolder . $original; if ($this->_directory->isExist($file)) { $this->_directory->delete($file); } } if (!empty($value['tmp_name'])) { try { $uploader = new \Magento\Framework\File\Uploader($value); $uploader->setFilesDispersion(true); $uploader->setFilenamesCaseSensitivity(false); $uploader->setAllowRenameFiles(true); $uploader->save($this->_directory->getAbsolutePath($destinationFolder), $value['name']); $fileName = $uploader->getUploadedFileName(); $this->getEntity()->setData($attribute->getAttributeCode(), $fileName); } catch (\Exception $e) { $this->_logger->logException($e); } } return $this; }
/** * Return path of the current selected directory or root directory for startup * Try to create target directory if it doesn't exist * * @return string * @throws \Magento\Framework\Model\Exception */ public function getCurrentPath() { if (!$this->_currentPath) { $currentPath = $this->_directory->getAbsolutePath() . \Magento\Cms\Model\Wysiwyg\Config::IMAGE_DIRECTORY; $path = $this->_getRequest()->getParam($this->getTreeNodeName()); if ($path) { $path = $this->convertIdToPath($path); if ($this->_directory->isDirectory($this->_directory->getRelativePath($path))) { $currentPath = $path; } } try { $currentDir = $this->_directory->getRelativePath($currentPath); if (!$this->_directory->isExist($currentDir)) { $this->_directory->create($currentDir); } } catch (\Magento\Framework\Filesystem\FilesystemException $e) { $message = __('The directory %1 is not writable by server.', $currentPath); throw new \Magento\Framework\Model\Exception($message); } $this->_currentPath = $currentPath; } return $this->_currentPath; }
/** * Check whether minified file should be created * * @param string $minifiedFile path relative to pub/view_cache * @return bool */ protected function _isUpdateNeeded($minifiedFile) { return !$this->pubViewCacheDir->isExist($minifiedFile); }