|
|
|
@ -7,43 +7,68 @@ use Drupal\file\FileRepositoryInterface; |
|
|
|
use Drupal\Core\File\FileExists; |
|
|
|
use Drupal\Core\File\FileExists; |
|
|
|
use Drupal\file\Entity\File; |
|
|
|
use Drupal\file\Entity\File; |
|
|
|
use Drupal\media\Entity\Media; |
|
|
|
use Drupal\media\Entity\Media; |
|
|
|
|
|
|
|
use Drupal\Core\Entity\EntityTypeManagerInterface; |
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
use Psr\Log\LoggerInterface; |
|
|
|
|
|
|
|
|
|
|
|
class InplaceMediaProcessor { |
|
|
|
class InplaceMediaProcessor { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected $mediaStorage; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct( |
|
|
|
public function __construct( |
|
|
|
protected FileSystemInterface $fileSystem, |
|
|
|
FileSystemInterface $fileSystem, |
|
|
|
protected FileRepositoryInterface $fileRepository, |
|
|
|
FileRepositoryInterface $fileRepository, |
|
|
|
protected LoggerInterface $logger, |
|
|
|
LoggerInterface $logger, |
|
|
|
) {} |
|
|
|
EntityTypeManagerInterface $entityTypeManager, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
$this->fileSystem = $fileSystem; |
|
|
|
|
|
|
|
$this->fileRepository = $fileRepository; |
|
|
|
|
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
|
$this->mediaStorage = $entityTypeManager->getStorage('media'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected function mediaExists(string $uri, array $opts): bool { |
|
|
|
|
|
|
|
return (bool) $this->mediaStorage->getQuery() |
|
|
|
|
|
|
|
->condition('bundle', $opts['media_type']) |
|
|
|
|
|
|
|
->condition("{$opts['file_type']}.entity.uri", $uri) |
|
|
|
|
|
|
|
->condition('field_media_use.target_id', $opts['media_use']) |
|
|
|
|
|
|
|
->accessCheck(FALSE) |
|
|
|
|
|
|
|
->range(0, 1) |
|
|
|
|
|
|
|
->execute(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public function processFile(string $file, array $opts): void { |
|
|
|
public function processFile(string $file, array $opts): bool { |
|
|
|
$source = "{$opts['source_dir']}/{$file}"; |
|
|
|
$source = "{$opts['source_dir']}/{$file}"; |
|
|
|
$dest = "{$opts['destination_path']}/{$file}"; |
|
|
|
$dest = "{$opts['destination_path']}/{$file}"; |
|
|
|
|
|
|
|
|
|
|
|
if (!file_exists($source)) { |
|
|
|
if (!file_exists($source)) { |
|
|
|
$this->logger->warning('Missing file @file', ['@file' => $source]); |
|
|
|
$this->logger->warning('Missing file @file', ['@file' => $source]); |
|
|
|
return; |
|
|
|
return FALSE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$uri = ($source === $dest) |
|
|
|
$uri = ($source === $dest) |
|
|
|
? $dest |
|
|
|
? $dest |
|
|
|
: $this->fileSystem->copy($source, $dest, FileExists::Rename); |
|
|
|
: $this->fileSystem->copy($source, $dest, FileExists::Rename); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->mediaExists($uri, $opts)) { |
|
|
|
|
|
|
|
return TRUE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$fileEntity = $this->fileRepository->loadByUri($uri) |
|
|
|
$fileEntity = $this->fileRepository->loadByUri($uri) |
|
|
|
?? File::create(['uri' => $uri, 'status' => 1, 'uid' => 1]); |
|
|
|
?? File::create(['uri' => $uri, 'status' => 1]); |
|
|
|
$fileEntity->save(); |
|
|
|
$fileEntity->save(); |
|
|
|
|
|
|
|
|
|
|
|
preg_match('/^(\d+)_/', $file, $m); |
|
|
|
preg_match('/^(\d+)_/', $file, $m); |
|
|
|
$nid = $m[1] ?? NULL; |
|
|
|
$nid = $m[1] ?? NULL; |
|
|
|
|
|
|
|
|
|
|
|
Media::create([ |
|
|
|
Media::create([ |
|
|
|
'bundle' => $opts['media_type'], |
|
|
|
'bundle' => $opts['media_type'], |
|
|
|
'name' => $file, |
|
|
|
'name' => $file, |
|
|
|
'uid' => 1, |
|
|
|
|
|
|
|
$opts['file_type'] => ['target_id' => $fileEntity->id()], |
|
|
|
$opts['file_type'] => ['target_id' => $fileEntity->id()], |
|
|
|
'field_media_use' => ['target_id' => $opts['media_use']], |
|
|
|
'field_media_use' => ['target_id' => $opts['media_use']], |
|
|
|
'field_media_of' => $nid, |
|
|
|
'field_media_of' => $nid, |
|
|
|
])->save(); |
|
|
|
])->save(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return TRUE; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|