/** * 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; }