View source
<?php
namespace Drupal\media_pdf_thumbnail\Manager;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\File\FileSystem;
use Drupal\file\FileInterface;
use Drupal\media\MediaInterface;
class MediaPdfThumbnailImageManager {
const VALID_MIME_TYPE = [
'application/pdf',
];
const GENERIC_FILENAME = 'generic.png';
protected $mediaPdfThumbnailImagickManager;
protected $entityTypeManager;
protected $fileSystem;
protected $configFactory;
protected $connection;
protected $cache;
public function __construct(MediaPdfThumbnailImagickManager $mediaPdfThumbnailImagickManager, EntityTypeManager $entityTypeManager, FileSystem $fileSystem, ConfigFactory $configFactory, Connection $connection, CacheTagsInvalidatorInterface $cache) {
$this->mediaPdfThumbnailImagickManager = $mediaPdfThumbnailImagickManager;
$this->entityTypeManager = $entityTypeManager;
$this->fileSystem = $fileSystem;
$this->configFactory = $configFactory;
$this->connection = $connection;
$this->cache = $cache;
}
public function createThumbnail(MediaInterface $media, $fieldName, $thumbnailType) {
if (empty($media)) {
return FALSE;
}
$entityTypeId = $media
->getEntityTypeId();
if ($entityTypeId == 'media' && $fieldName) {
if ($media
->hasField($fieldName) && !empty($media
->get($fieldName)
->getValue())) {
$fileEntity = $this
->getFileEntity($media
->get($fieldName)
->getValue()[0]['target_id']);
if ($fileEntity && in_array($fileEntity
->getMimeType(), self::VALID_MIME_TYPE)) {
$fileEntityInfo['fid'] = $this
->getGenericThumbnail();
$fileEntityInfo['filename'] = NULL;
if ($thumbnailType == 'pdf') {
$fileEntityInfo = $this
->createThumbnailFileEntity($this
->generatePdfImage($fileEntity));
}
if (!empty($fileEntityInfo)) {
$this
->setImageToMediaThumbnail($media, $fileEntityInfo, $thumbnailType);
}
}
}
}
}
protected function getFileEntity($fid) {
return $this->entityTypeManager
->getStorage('file')
->load($fid);
}
protected function getGenericThumbnail() {
$uri = $this->configFactory
->get('media.settings')
->get('icon_base_uri');
$files = $this->entityTypeManager
->getStorage('file')
->loadByProperties([
'uri' => $uri . '/' . self::GENERIC_FILENAME,
]);
return !empty($files) ? reset($files)
->id() : NULL;
}
protected function createThumbnailFileEntity($fileUri) {
if (empty($fileUri)) {
return;
}
$infos = pathinfo($fileUri);
$fileEntity = $this->entityTypeManager
->getStorage('file')
->create([
'uri' => $fileUri,
'status' => FILE_STATUS_PERMANENT,
]);
$fileEntity
->save();
return $fileEntity ? [
'fid' => $fileEntity
->id(),
'filename' => $infos['filename'],
] : NULL;
}
protected function generatePdfImage(FileInterface $fileEntity) {
$fileInfos = $this
->getFileInfos($fileEntity);
if (!empty($fileInfos['source']) && !empty($fileInfos['destination'])) {
return $this->mediaPdfThumbnailImagickManager
->generateImageFromPDF($fileInfos['source'], $fileInfos['destination']);
}
return NULL;
}
protected function getFileInfos(FileInterface $fileEntity) {
$sourcePath = $fileEntity
->getFileUri();
$destinationPath = $sourcePath . '.jpeg';
return [
'source' => $sourcePath,
'destination' => $destinationPath,
];
}
protected function setImageToMediaThumbnail(MediaInterface $media, $fileEntityInfo, $thumbnailType) {
$revisionFieldValue = $media
->get('vid')
->get(0)
->getValue();
$vid = !empty($revisionFieldValue['value']) ? $revisionFieldValue['value'] : NULL;
$mediaRevision = $this->entityTypeManager
->getStorage('media')
->loadRevision($vid);
$currentThumbnailFileUri = $mediaRevision
->get('thumbnail')->entity
->getFileUri();
$thumbnailFileUri = $this->entityTypeManager
->getStorage('file')
->load($fileEntityInfo['fid'])
->getFileUri();
if ($currentThumbnailFileUri != $thumbnailFileUri) {
$mediaRevision
->set('thumbnail', $fileEntityInfo['fid']);
$mediaRevision
->save();
$this->entityTypeManager
->getStorage($media
->getEntityTypeId())
->resetCache([
$media
->id(),
]);
$this->cache
->invalidateTags($media
->getCacheTagsToInvalidate());
}
return;
if ($vid) {
$values = [
'fid' => $fileEntityInfo['fid'],
'module' => $media
->bundle(),
'type' => $media
->getEntityTypeId(),
'id' => $media
->id(),
];
if (!$this
->entryExists('file_usage', $values)) {
$query = $this->connection
->insert('file_usage');
$values['count'] = 1;
$query
->fields($values);
$query
->execute();
}
$query = $this->connection
->update('media_field_revision');
$values = [
'langcode' => $media
->language()
->getId(),
'thumbnail__target_id' => $fileEntityInfo['fid'],
'thumbnail__alt' => $fileEntityInfo['filename'],
];
$query
->fields($values);
$query
->condition('mid', $media
->id());
$query
->condition('vid', $vid);
$query
->condition('langcode', $media
->language()
->getId());
$query
->execute();
$this->entityTypeManager
->getStorage($media
->getEntityTypeId())
->resetCache([
$media
->id(),
]);
$this->cache
->invalidateTags($media
->getCacheTagsToInvalidate());
}
}
protected function entryExists($table, $values) {
$query = $this->connection
->select($table, 't');
foreach ($values as $key => $value) {
$query
->condition('t.' . $key, $value);
}
$query
->fields('t', [
'fid',
]);
return !empty($query
->execute()
->fetchAssoc());
}
}