/** * Get backup-specific data from model for each row * * @param string $filename * @return array */ protected function _generateRow($filename) { $row = parent::_generateRow($filename); foreach ($this->_backup->load($row['basename'], $this->_varDirectory->getAbsolutePath($this->_path))->getData() as $key => $value) { $row[$key] = $value; } $row['size'] = $this->_varDirectory->stat($this->_varDirectory->getRelativePath($filename))['size']; $row['id'] = $row['time'] . '_' . $row['type']; return $row; }
/** * Delete Expired Captcha Images for specific website * * @param \Magento\Captcha\Helper\Data $helper * @param \Magento\Store\Model\Website|null $website * @param \Magento\Store\Model\Store|null $store * @return void */ protected function _deleteExpiredImagesForWebsite(\Magento\Captcha\Helper\Data $helper, \Magento\Store\Model\Website $website = null, \Magento\Store\Model\Store $store = null) { $expire = time() - $helper->getConfig('timeout', $store) * 60; $imageDirectory = $this->_mediaDirectory->getRelativePath($helper->getImgDir($website)); foreach ($this->_mediaDirectory->read($imageDirectory) as $filePath) { if ($this->_mediaDirectory->isFile($filePath) && pathinfo($filePath, PATHINFO_EXTENSION) == 'png' && $this->_mediaDirectory->stat($filePath)['mtime'] < $expire) { $this->_mediaDirectory->delete($filePath); } } }
/** * Custom options downloader * * @param array $info * @return void * @throws \Exception */ public function downloadFile($info) { $relativePath = $info['order_path']; if (!$this->_isCanProcessed($relativePath)) { //try get file from quote $relativePath = $info['quote_path']; if (!$this->_isCanProcessed($relativePath)) { throw new LocalizedException(__('Path "%1" is not part of allowed directory "%2"', $relativePath, $this->rootDirBasePath)); } } $this->_fileFactory->create($info['title'], ['value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'], $this->rootDirBasePath); }
/** * Custom options downloader * * @param array $info * @return void * @throws \Exception */ public function downloadFile($info) { $relativePath = $info['order_path']; if ($this->_isCanProcessed($relativePath)) { //try get file from quote $relativePath = $info['quote_path']; if ($this->_isCanProcessed($relativePath)) { throw new \Exception(); } } $this->_fileFactory->create($info['title'], array('value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'), \Magento\Framework\App\Filesystem::ROOT_DIR); }
/** * Custom options downloader * * @param array $info * @return void * @throws \Exception */ public function downloadFile($info) { $relativePath = $info['order_path']; if ($this->_isCanProcessed($relativePath)) { //try get file from quote $relativePath = $info['quote_path']; if ($this->_isCanProcessed($relativePath)) { throw new \Exception(); } } $this->_fileFactory->create($info['title'], ['value' => $this->_rootDir->getRelativePath($relativePath), 'type' => 'filename'], DirectoryList::ROOT); }
/** * Save config in cache file * * @return void */ public function save() { /** @var Write $file */ $file = $this->rootDirectory->openFile($this->rootDirectory->getRelativePath($this->cacheFilePath), 'w'); try { $file->lock(); $file->write(json_encode($this->config)); $file->unlock(); $file->close(); } catch (FileSystemException $e) { $file->close(); } }
/** * Get backup-specific data from model for each row * * @param string $filename * @return array */ protected function _generateRow($filename) { $row = parent::_generateRow($filename); foreach ($this->_backup->load($row['basename'], $this->_varDirectory->getAbsolutePath($this->_path))->getData() as $key => $value) { $row[$key] = $value; } $row['size'] = $this->_varDirectory->stat($this->_varDirectory->getRelativePath($filename))['size']; if (isset($row['display_name']) && $row['display_name'] == '') { $row['display_name'] = 'WebSetupWizard'; } $row['id'] = $row['time'] . '_' . $row['type'] . (isset($row['display_name']) ? $row['display_name'] : ''); return $row; }
/** * Delete preview image * * @return bool */ public function removePreviewImage() { $previewImage = $this->theme->getPreviewImage(); $this->theme->setPreviewImage(null); if ($previewImage) { $previewImagePath = $this->themeImagePath->getImagePreviewDirectory() . '/' . $previewImage; return $this->mediaDirectory->delete($this->mediaDirectory->getRelativePath($previewImagePath)); } return false; }
/** * Returns an object for upload a media files * * @param string $type * @param array $parameters * @return \Magento\CatalogImportExport\Model\Import\Uploader * @throws \Magento\Framework\Exception\LocalizedException */ public function getUploader($type, $parameters) { $dirConfig = DirectoryList::getDefaultConfig(); $dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH]; $DS = DIRECTORY_SEPARATOR; if (!empty($parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR])) { $tmpPath = $parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR]; } else { $tmpPath = $dirAddon . $DS . $this->mediaDirectory->getRelativePath('import'); } if (!$this->fileUploader->setTmpDir($tmpPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not readable.', $tmpPath)); } $destinationDir = "downloadable/files/" . $type; $destinationPath = $dirAddon . $DS . $this->mediaDirectory->getRelativePath($destinationDir); $this->mediaDirectory->create($destinationDir); if (!$this->fileUploader->setDestDir($destinationPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not writable.', $destinationPath)); } return $this->fileUploader; }
/** * Synchronize file * * @param string $relativeFileName * @param string $filePath * @return void * @throws \LogicException */ public function synchronize($relativeFileName, $filePath) { /** @var $storage \Magento\Core\Model\File\Storage\Database */ $storage = $this->storageFactory->create(); try { $storage->loadByFilename($relativeFileName); } catch (\Exception $e) { } if ($storage->getId()) { /** @var Write $file */ $file = $this->pubDirectory->openFile($this->pubDirectory->getRelativePath($filePath), 'w'); try { $file->lock(); $file->write($storage->getContent()); $file->unlock(); $file->close(); } catch (FilesystemException $e) { $file->close(); } } }
/** * Add image to media gallery and return new filename * * @param \Magento\Catalog\Model\Product $product * @param string $file file path of image in file system * @param string|string[] $mediaAttribute code of attribute with type 'media_image', * leave blank if image should be only in gallery * @param boolean $move if true, it will move source file * @param boolean $exclude mark image as disabled in product page view * @return string * @throws \Magento\Framework\Exception\LocalizedException * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function addImage(\Magento\Catalog\Model\Product $product, $file, $mediaAttribute = null, $move = false, $exclude = true) { $file = $this->_mediaDirectory->getRelativePath($file); if (!$this->_mediaDirectory->isFile($file)) { throw new LocalizedException(__('The image does not exist.')); } $pathinfo = pathinfo($file); $imgExtensions = ['jpg', 'jpeg', 'gif', 'png']; if (!isset($pathinfo['extension']) || !in_array(strtolower($pathinfo['extension']), $imgExtensions)) { throw new LocalizedException(__('Please correct the image file type.')); } $fileName = \Magento\MediaStorage\Model\File\Uploader::getCorrectFileName($pathinfo['basename']); $dispretionPath = \Magento\MediaStorage\Model\File\Uploader::getDispretionPath($fileName); $fileName = $dispretionPath . '/' . $fileName; $fileName = $this->_getNotDuplicatedFilename($fileName, $dispretionPath); $destinationFile = $this->_mediaConfig->getTmpMediaPath($fileName); try { /** @var $storageHelper \Magento\MediaStorage\Helper\File\Storage\Database */ $storageHelper = $this->_fileStorageDb; if ($move) { $this->_mediaDirectory->renameFile($file, $destinationFile); //If this is used, filesystem should be configured properly $storageHelper->saveFile($this->_mediaConfig->getTmpMediaShortUrl($fileName)); } else { $this->_mediaDirectory->copyFile($file, $destinationFile); $storageHelper->saveFile($this->_mediaConfig->getTmpMediaShortUrl($fileName)); $this->_mediaDirectory->changePermissions($destinationFile, 0777); } } catch (\Exception $e) { throw new LocalizedException(__('We couldn\'t move this file: %1.', $e->getMessage())); } $fileName = str_replace('\\', '/', $fileName); $attrCode = $this->getAttribute()->getAttributeCode(); $mediaGalleryData = $product->getData($attrCode); $position = 0; if (!is_array($mediaGalleryData)) { $mediaGalleryData = ['images' => []]; } foreach ($mediaGalleryData['images'] as &$image) { if (isset($image['position']) && $image['position'] > $position) { $position = $image['position']; } } $position++; $mediaGalleryData['images'][] = ['file' => $fileName, 'position' => $position, 'label' => '', 'disabled' => (int) $exclude]; $product->setData($attrCode, $mediaGalleryData); if ($mediaAttribute !== null) { $this->setMediaAttribute($product, $mediaAttribute, $fileName); } return $fileName; }
/** * Proceed moving a file from TMP to destination folder * * @param string $fileName * @return array */ public function move($fileName) { if (preg_match('/\\bhttps?:\\/\\//i', $fileName, $matches)) { $url = str_replace($matches[0], '', $fileName); $read = $this->_readFactory->create($url, DriverPool::HTTP); $fileName = preg_replace('/[^a-z0-9\\._-]+/i', '', $fileName); $this->_directory->writeFile($this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName), $read->readAll()); } $filePath = $this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName); $this->_setUploadFile($filePath); $result = $this->save($this->getDestDir()); $result['name'] = self::getCorrectFileName($result['name']); return $result; }
/** * Enables apppropriate cache types in app/etc/env.php based on the passed in $cacheTypes array * TODO: to be removed in scope of MAGETWO-53476 * * @param string[] * * @return void */ private function enableCacheTypes($cacheTypes) { if (empty($cacheTypes)) { return; } $envPath = $this->getEnvPath(); if ($this->write->isReadable($this->write->getRelativePath($envPath))) { $envData = (include $envPath); foreach ($cacheTypes as $cacheType) { if (isset($envData['cache_types'][$cacheType])) { $envData['cache_types'][$cacheType] = 1; } } $formatter = new PhpFormatter(); $contents = $formatter->format($envData); $this->write->writeFile($this->write->getRelativePath($envPath), $contents); if (function_exists('opcache_invalidate')) { opcache_invalidate($this->write->getAbsolutePath($envPath)); } } }
/** * Returns an object for upload a media files * * @return \Magento\CatalogImportExport\Model\Import\Uploader * @throws \Magento\Framework\Exception\LocalizedException */ protected function _getUploader() { if (is_null($this->_fileUploader)) { $this->_fileUploader = $this->_uploaderFactory->create(); $this->_fileUploader->init(); $dirConfig = DirectoryList::getDefaultConfig(); $dirAddon = $dirConfig[DirectoryList::MEDIA][DirectoryList::PATH]; $DS = DIRECTORY_SEPARATOR; if (!empty($this->_parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR])) { $tmpPath = $this->_parameters[\Magento\ImportExport\Model\Import::FIELD_NAME_IMG_FILE_DIR]; } else { $tmpPath = $dirAddon . $DS . $this->_mediaDirectory->getRelativePath('import'); } if (!$this->_fileUploader->setTmpDir($tmpPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not readable.', $tmpPath)); } $destinationDir = "catalog/product"; $destinationPath = $dirAddon . $DS . $this->_mediaDirectory->getRelativePath($destinationDir); $this->_mediaDirectory->create($destinationPath); if (!$this->_fileUploader->setDestDir($destinationPath)) { throw new \Magento\Framework\Exception\LocalizedException(__('File directory \'%1\' is not writable.', $destinationPath)); } } return $this->_fileUploader; }
/** * Get file path. * * @return string */ protected function _getFilePath() { return $this->varDirectory->getRelativePath($this->getPath() . '/' . $this->getFileName()); }
/** * Get file path. * * @param string $filename * @return string */ protected function getFilePath($filename) { return $this->varDirectory->getRelativePath(Import::IMPORT_HISTORY_DIR . $filename); }