123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\MediaStorage\Model\File\Storage;
- /**
- * Class Database
- *
- * @api
- * @since 100.0.2
- */
- class Database extends \Magento\MediaStorage\Model\File\Storage\Database\AbstractDatabase
- {
- /**
- * Prefix of model events names
- *
- * @var string
- */
- protected $_eventPrefix = 'media_storage_file_storage_database';
- /**
- * Directory singleton
- *
- * @var \Magento\MediaStorage\Model\File\Storage\Directory\Database
- */
- protected $_directoryModel = null;
- /**
- * Collect errors during sync process
- *
- * @var string[]
- */
- protected $_errors = [];
- /**
- * @var \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory
- */
- protected $_directoryFactory;
- /**
- * @var \Magento\MediaStorage\Helper\File\Media
- */
- protected $_mediaHelper;
- /**
- * Store media base directory path
- *
- * @var string
- * @since 100.1.0
- */
- protected $mediaBaseDirectory = null;
- /**
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb
- * @param \Magento\Framework\Stdlib\DateTime\DateTime $dateModel
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $configuration
- * @param \Magento\MediaStorage\Helper\File\Media $mediaHelper
- * @param \Magento\MediaStorage\Model\ResourceModel\File\Storage\Database $resource
- * @param Directory\DatabaseFactory $directoryFactory
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param string $connectionName
- * @param array $data
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- \Magento\MediaStorage\Helper\File\Storage\Database $coreFileStorageDb,
- \Magento\Framework\Stdlib\DateTime\DateTime $dateModel,
- \Magento\Framework\App\Config\ScopeConfigInterface $configuration,
- \Magento\MediaStorage\Helper\File\Media $mediaHelper,
- \Magento\MediaStorage\Model\ResourceModel\File\Storage\Database $resource,
- \Magento\MediaStorage\Model\File\Storage\Directory\DatabaseFactory $directoryFactory,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- $connectionName = null,
- array $data = []
- ) {
- $this->_directoryFactory = $directoryFactory;
- $this->_mediaHelper = $mediaHelper;
- parent::__construct(
- $context,
- $registry,
- $coreFileStorageDb,
- $dateModel,
- $configuration,
- $resource,
- $resourceCollection,
- $connectionName,
- $data
- );
- $this->_init(get_class($this->_resource));
- }
- /**
- * Retrieve directory model
- *
- * @return \Magento\MediaStorage\Model\File\Storage\Directory\Database
- */
- public function getDirectoryModel()
- {
- if ($this->_directoryModel === null) {
- $this->_directoryModel = $this->_directoryFactory->create(
- ['connectionName' => $this->getConnectionName()]
- );
- }
- return $this->_directoryModel;
- }
- /**
- * Create tables for file and directory storages
- *
- * @return $this
- */
- public function init()
- {
- $this->getDirectoryModel()->prepareStorage();
- $this->prepareStorage();
- return $this;
- }
- /**
- * Return storage name
- *
- * @return \Magento\Framework\Phrase
- */
- public function getStorageName()
- {
- return __('database "%1"', $this->getConnectionName());
- }
- /**
- * Load object data by filename
- *
- * @param string $filePath
- * @return $this
- */
- public function loadByFilename($filePath)
- {
- $filename = basename($filePath);
- $path = dirname($filePath);
- $this->_getResource()->loadByFilename($this, $filename, $path);
- return $this;
- }
- /**
- * Check if there was errors during sync process
- *
- * @return bool
- */
- public function hasErrors()
- {
- return !empty($this->_errors) || $this->getDirectoryModel()->hasErrors();
- }
- /**
- * Clear files and directories in storage
- *
- * @return $this
- */
- public function clear()
- {
- $this->getDirectoryModel()->clearDirectories();
- $this->_getResource()->clearFiles();
- return $this;
- }
- /**
- * Export directories from storage
- *
- * @param int $offset
- * @param int $count
- * @return bool|array
- */
- public function exportDirectories($offset = 0, $count = 100)
- {
- return $this->getDirectoryModel()->exportDirectories($offset, $count);
- }
- /**
- * Import directories to storage
- *
- * @param array $dirs
- * @return \Magento\MediaStorage\Model\File\Storage\Directory\Database
- */
- public function importDirectories($dirs)
- {
- return $this->getDirectoryModel()->importDirectories($dirs);
- }
- /**
- * Export files list in defined range
- *
- * @param int $offset
- * @param int $count
- * @return array|bool
- */
- public function exportFiles($offset = 0, $count = 100)
- {
- $offset = (int)$offset >= 0 ? (int)$offset : 0;
- $count = (int)$count >= 1 ? (int)$count : 1;
- $result = $this->_getResource()->getFiles($offset, $count);
- if (empty($result)) {
- return false;
- }
- return $result;
- }
- /**
- * Import files list
- *
- * @param array $files
- * @return $this
- */
- public function importFiles($files)
- {
- if (!is_array($files)) {
- return $this;
- }
- $dateSingleton = $this->_date;
- foreach ($files as $file) {
- if (!isset($file['filename']) || !strlen($file['filename']) || !isset($file['content'])) {
- continue;
- }
- try {
- $file['update_time'] = $dateSingleton->date();
- $file['directory_id'] = isset(
- $file['directory']
- ) && strlen(
- $file['directory']
- ) ? $this->_directoryFactory->create(
- ['connectionName' => $this->getConnectionName()]
- )->loadByPath(
- $file['directory']
- )->getId() : null;
- $this->_getResource()->saveFile($file);
- } catch (\Exception $e) {
- $this->_errors[] = $e->getMessage();
- $this->_logger->critical($e);
- }
- }
- return $this;
- }
- /**
- * Store file into database
- *
- * @param string $filename
- * @return $this
- */
- public function saveFile($filename)
- {
- $fileInfo = $this->_mediaHelper->collectFileInfo($this->getMediaBaseDirectory(), $filename);
- $filePath = $fileInfo['directory'];
- $directory = $this->_directoryFactory->create()->loadByPath($filePath);
- if (!$directory->getId()) {
- $directory = $this->getDirectoryModel()->createRecursive($filePath);
- }
- $fileInfo['directory_id'] = $directory->getId();
- $this->_getResource()->saveFile($fileInfo);
- return $this;
- }
- /**
- * Check whether file exists in DB
- *
- * @param string $filePath
- * @return bool
- */
- public function fileExists($filePath)
- {
- return $this->_getResource()->fileExists(basename($filePath), dirname($filePath));
- }
- /**
- * Copy files
- *
- * @param string $oldFilePath
- * @param string $newFilePath
- * @return $this
- */
- public function copyFile($oldFilePath, $newFilePath)
- {
- $this->_getResource()->copyFile(
- basename($oldFilePath),
- dirname($oldFilePath),
- basename($newFilePath),
- dirname($newFilePath)
- );
- return $this;
- }
- /**
- * Rename files in database
- *
- * @param string $oldFilePath
- * @param string $newFilePath
- * @return $this
- */
- public function renameFile($oldFilePath, $newFilePath)
- {
- $this->_getResource()->renameFile(
- basename($oldFilePath),
- dirname($oldFilePath),
- basename($newFilePath),
- dirname($newFilePath)
- );
- $newPath = dirname($newFilePath);
- $directory = $this->_directoryFactory->create()->loadByPath($newPath);
- if (!$directory->getId()) {
- $directory = $this->getDirectoryModel()->createRecursive($newPath);
- }
- $this->loadByFilename($newFilePath);
- if ($this->getId()) {
- $this->setDirectoryId($directory->getId())->save();
- }
- return $this;
- }
- /**
- * Return directory listing
- *
- * @param string $directory
- * @return array
- */
- public function getDirectoryFiles($directory)
- {
- $directory = $this->_coreFileStorageDb->getMediaRelativePath($directory);
- return $this->_getResource()->getDirectoryFiles($directory);
- }
- /**
- * Delete file from database
- *
- * @param string $path
- * @return $this
- */
- public function deleteFile($path)
- {
- $filename = basename($path);
- $directory = dirname($path);
- $this->_getResource()->deleteFile($filename, $directory);
- return $this;
- }
- /**
- * Retrieve media base directory path
- *
- * @return string
- * @since 100.1.0
- */
- public function getMediaBaseDirectory()
- {
- if ($this->mediaBaseDirectory === null) {
- $this->mediaBaseDirectory = $this->_coreFileStorageDb->getMediaBaseDir();
- }
- return $this->mediaBaseDirectory;
- }
- }
|