FileProcessor.php in Content Synchronizer 8.2
File
src/Plugin/content_synchronizer/entity_processor/FileProcessor.php
View source
<?php
namespace Drupal\content_synchronizer\Plugin\content_synchronizer\entity_processor;
use Drupal\content_synchronizer\Base\JsonWriterTrait;
use Drupal\content_synchronizer\Processors\Entity\EntityProcessorBase;
use Drupal\content_synchronizer\Processors\Entity\EntityProcessorInterface;
use Drupal\content_synchronizer\Processors\ExportProcessor;
use Drupal\content_synchronizer\Processors\ImportProcessor;
use Drupal\Core\Entity\EntityInterface;
use Drupal\file\Entity\File;
class FileProcessor extends EntityProcessorBase implements EntityProcessorInterface {
use JsonWriterTrait;
const DIR_ASSETS = "assets";
protected $exportAssetsDirPath;
protected $importAssetsDirPath;
public function getDataToExport(EntityInterface $entityToExport) {
$this
->addFileToAssets($entityToExport);
return parent::getDataToExport($entityToExport);
}
protected function addFileToAssets(File $file) {
$assetsDir = $this
->getExportAssetsDir();
$destination = $this
->createDirTreeForFileDest(str_replace('://', '/', $file
->getFileUri()), $assetsDir);
file_copy($file, $destination);
}
protected function getExportAssetsDir() {
if (!$this->exportAssetsDirPath) {
$writer = ExportProcessor::getCurrentExportProcessor()
->getWriter();
$this->exportAssetsDirPath = $writer
->getDirPath() . '/' . self::DIR_ASSETS;
$this
->createDirectory($this->exportAssetsDirPath);
}
return $this->exportAssetsDirPath;
}
protected function getImportAssetsDir() {
if (!$this->importAssetsDirPath) {
$this->importAssetsDirPath = ImportProcessor::getCurrentImportProcessor()
->getImport()
->getArchiveFilesPath() . '/' . self::DIR_ASSETS;
}
return $this->importAssetsDirPath;
}
public function getEntityToImport(array $data, EntityInterface $entityToImport = NULL) {
if ($file = parent::getEntityToImport($data, $entityToImport)) {
$assetsFile = $this
->getImportAssetsDir() . '/' . str_replace('://', '/', $file
->getFileUri());
if (file_exists($assetsFile)) {
if (strpos($file
->getFileUri(), '://')) {
list($root, $destination) = explode('://', $file
->getFileUri());
$root .= '://';
}
else {
list($root, $destination) = [
$file
->getFileUri(),
'/',
];
}
$destination = $this
->createDirTreeForFileDest($destination, $root);
if ($result = copy($assetsFile, $file
->getFileUri())) {
return $file;
}
}
}
return NULL;
}
}