_coreFileStorage = $coreFileStorage; $this->_scopeConfig = $scopeConfig; $this->_coreConfig = $coreConfig; $this->_fileFlag = $fileFlag; $this->_fileFactory = $fileFactory; $this->_databaseFactory = $databaseFactory; $this->filesystem = $filesystem; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * Show if there were errors while synchronize process * * @param \Magento\Framework\Model\AbstractModel $sourceModel * @param \Magento\Framework\Model\AbstractModel $destinationModel * @return bool */ protected function _synchronizeHasErrors($sourceModel, $destinationModel) { if (!$sourceModel || !$destinationModel) { return true; } return $sourceModel->hasErrors() || $destinationModel->hasErrors(); } /** * Return synchronize process status flag * * @return \Magento\MediaStorage\Model\File\Storage\Flag */ public function getSyncFlag() { return $this->_fileFlag->loadSelf(); } /** * 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 ($storage === null) { $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; } /** * Synchronize current media storage with defined * $storage = array( * type => int * connection => string * ) * * @param array $storage * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function synchronize($storage) { if (is_array($storage) && isset($storage['type'])) { $storageDest = (int)$storage['type']; $connection = isset($storage['connection']) ? $storage['connection'] : null; $helper = $this->_coreFileStorage; // if unable to sync to internal storage from itself if ($storageDest == $helper->getCurrentStorageCode() && $helper->isInternalStorage()) { return $this; } $sourceModel = $this->getStorageModel(); $destinationModel = $this->getStorageModel( $storageDest, ['connection' => $connection, 'init' => true] ); if (!$sourceModel || !$destinationModel) { return $this; } $hasErrors = false; $flag = $this->getSyncFlag(); $flagData = [ 'source' => $sourceModel->getStorageName(), 'destination' => $destinationModel->getStorageName(), 'destination_storage_type' => $storageDest, 'destination_connection_name' => (string)$destinationModel->getConnectionName(), 'has_errors' => false, 'timeout_reached' => false, ]; $flag->setFlagData($flagData); $destinationModel->clear(); $offset = 0; while (($dirs = $sourceModel->exportDirectories($offset)) !== false) { $flagData['timeout_reached'] = false; if (!$hasErrors) { $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel); if ($hasErrors) { $flagData['has_errors'] = true; } } $flag->setFlagData($flagData)->save(); $destinationModel->importDirectories($dirs); $offset += count($dirs); } unset($dirs); $offset = 0; while (($files = $sourceModel->exportFiles($offset, 1)) !== false) { $flagData['timeout_reached'] = false; if (!$hasErrors) { $hasErrors = $this->_synchronizeHasErrors($sourceModel, $destinationModel); if ($hasErrors) { $flagData['has_errors'] = true; } } $flag->setFlagData($flagData)->save(); $destinationModel->importFiles($files); $offset += count($files); } unset($files); } return $this; } /** * Return current media directory, allowed resources for get.php script, etc. * * @return array */ public function getScriptConfig() { $config = []; $config['media_directory'] = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath(); $allowedResources = $this->_coreConfig->getValue(self::XML_PATH_MEDIA_RESOURCE_WHITELIST, 'default'); foreach ($allowedResources as $allowedResource) { $config['allowed_resources'][] = $allowedResource; } $config['update_time'] = $this->_scopeConfig->getValue( self::XML_PATH_MEDIA_UPDATE_TIME, \Magento\Store\Model\ScopeInterface::SCOPE_STORE ); return $config; } }