/** * Check file in database storage if needed and place it on file system * * @param string $filePath * @return bool */ protected function _processDatabaseFile($filePath) { if (!$this->_fileStorageDatabase->checkDbUsage()) { return false; } $relativePath = $this->_fileStorageDatabase->getMediaRelativePath($filePath); $file = $this->_storageDatabaseFactory->create()->loadByFilename($relativePath); if (!$file->getId()) { return false; } $stream = $this->_rootDir->openFile($filePath, 'w+'); $stream->lock(); $stream->write($filePath, $file->getContent()); $stream->unlock(); $stream->close(); return true; }
/** * Synchronize file * * @param string $relativeFileName * @return void * @throws \LogicException */ public function synchronize($relativeFileName) { /** @var $storage \Magento\MediaStorage\Model\File\Storage\Database */ $storage = $this->storageFactory->create(); try { $storage->loadByFilename($relativeFileName); } catch (\Exception $e) { } if ($storage->getId()) { /** @var Write $file */ $file = $this->mediaDirectory->openFile($relativeFileName, 'w'); try { $file->lock(); $file->write($storage->getContent()); $file->unlock(); $file->close(); } catch (FileSystemException $e) { $file->close(); } } }
/** * Save database file to file system * * @param string $filename * @return bool|int */ public function saveFileToFilesystem($filename) { if ($this->checkDbUsage()) { /** @var $file \Magento\MediaStorage\Model\File\Storage\Database */ $file = $this->_dbStorageFactory->create()->loadByFilename($this->_removeAbsPathFromFileName($filename)); if (!$file->getId()) { return false; } return $this->getStorageFileModel()->saveFile($file->getData(), true); } return false; }
/** * Retrieve storage model * If storage not defined - retrieve current storage * * params = array( * connection => string, - define connection for model if needed * init => bool - force initialization process for storage model * ) * * @param int|null $storage * @param array $params * @return AbstractModel|bool */ public function getStorageModel($storage = null, $params = []) { if (is_null($storage)) { $storage = $this->_coreFileStorage->getCurrentStorageCode(); } switch ($storage) { case self::STORAGE_MEDIA_FILE_SYSTEM: $model = $this->_fileFactory->create(); break; case self::STORAGE_MEDIA_DATABASE: $connection = isset($params['connection']) ? $params['connection'] : null; $model = $this->_databaseFactory->create(['connectionName' => $connection]); break; default: return false; } if (isset($params['init']) && $params['init']) { $model->init(); } return $model; }
/** * Return files * * @param string $path Parent directory path * @param string $type Type of storage, e.g. image, media etc. * @return \Magento\Framework\Data\Collection\Filesystem */ public function getFilesCollection($path, $type = null) { if ($this->_coreFileStorageDb->checkDbUsage()) { $files = $this->_storageDatabaseFactory->create()->getDirectoryFiles($path); /** @var \Magento\MediaStorage\Model\File\Storage\File $fileStorageModel */ $fileStorageModel = $this->_storageFileFactory->create(); foreach ($files as $file) { $fileStorageModel->saveFile($file); } } $collection = $this->getCollection($path)->setCollectDirs(false)->setCollectFiles(true)->setCollectRecursively(false)->setOrder('mtime', \Magento\Framework\Data\Collection::SORT_ORDER_ASC); // Add files extension filter if ($allowed = $this->getAllowedExtensions($type)) { $collection->setFilesFilter('/\\.(' . implode('|', $allowed) . ')$/i'); } // prepare items foreach ($collection as $item) { $item->setId($this->_cmsWysiwygImages->idEncode($item->getBasename())); $item->setName($item->getBasename()); $item->setShortName($this->_cmsWysiwygImages->getShortFilename($item->getBasename())); $item->setUrl($this->_cmsWysiwygImages->getCurrentUrl() . $item->getBasename()); if ($this->isImage($item->getBasename())) { $thumbUrl = $this->getThumbnailUrl($item->getFilename(), true); // generate thumbnail "on the fly" if it does not exists if (!$thumbUrl) { $thumbUrl = $this->_backendUrl->getUrl('cms/*/thumbnail', ['file' => $item->getId()]); } $size = @getimagesize($item->getFilename()); if (is_array($size)) { $item->setWidth($size[0]); $item->setHeight($size[1]); } } else { $thumbUrl = $this->_assetRepo->getUrl(self::THUMB_PLACEHOLDER_PATH_SUFFIX); } $item->setThumbUrl($thumbUrl); } return $collection; }