View source
<?php
namespace Drupal\photos;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\Token;
use Drupal\file\FileInterface;
use Drupal\file\FileUsage\FileUsageInterface;
use Drupal\media\Entity\Media;
use Drupal\photos\Entity\PhotosImage;
class PhotosUpload implements PhotosUploadInterface {
use StringTranslationTrait;
protected $configFactory;
protected $connection;
protected $currentUser;
protected $entityFieldManager;
protected $entityTypeManager;
protected $fileSystem;
protected $fileUsage;
protected $imageFactory;
protected $messenger;
protected $moduleHandler;
protected $streamWrapperManager;
protected $token;
protected $transliteration;
public function __construct(ConfigFactoryInterface $config_factory, Connection $connection, AccountInterface $current_user, EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entity_manager, FileSystem $file_system, FileUsageInterface $file_usage, ImageFactory $image_factory, MessengerInterface $messenger, ModuleHandlerInterface $module_handler, StreamWrapperManagerInterface $stream_wrapper_manager, Token $token, TransliterationInterface $transliteration) {
$this->configFactory = $config_factory;
$this->connection = $connection;
$this->currentUser = $current_user;
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_manager;
$this->imageFactory = $image_factory;
$this->fileSystem = $file_system;
$this->fileUsage = $file_usage;
$this->messenger = $messenger;
$this->moduleHandler = $module_handler;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->token = $token;
$this->transliteration = $transliteration;
}
public function cleanTitle($title = '') {
$config = $this->configFactory
->get('photos.settings');
if ($config
->get('photos_clean_title')) {
$title = pathinfo($title, PATHINFO_FILENAME);
$title = preg_replace("/[\\-_]/", " ", $title);
$title = trim($title);
}
return $title;
}
public function path($schemaType = 'default') {
$fileConfig = $this->configFactory
->get('system.file');
$path[] = 'photos';
switch ($schemaType) {
case 'private':
$scheme = 'private';
break;
case 'public':
$scheme = 'public';
break;
case 'default':
default:
$scheme = $fileConfig
->get('default_scheme');
break;
}
$dirs = [];
foreach ($path as $folder) {
$dirs[] = $folder;
$finalPath = $scheme . '://' . implode('/', $dirs);
if (!$this->fileSystem
->prepareDirectory($finalPath, FileSystemInterface::CREATE_DIRECTORY)) {
return FALSE;
}
}
if ($finalPath) {
$finalPath = rtrim($finalPath, '/');
}
return $finalPath;
}
public function saveImage(FileInterface $file) {
$config = $this->configFactory
->get('photos.settings');
if ($file
->id() && isset($file->album_id)) {
$fid = $file
->id();
$albumId = $file->album_id;
$album_photo_limit = $config
->get('album_photo_limit');
$photo_count = $this->connection
->query("SELECT count FROM {photos_album} WHERE album_id = :album_id", [
':album_id' => $albumId,
])
->fetchField();
if ($album_photo_limit && $photo_count >= $album_photo_limit) {
return FALSE;
}
if (isset($file->title) && !empty($file->title)) {
$title = $file->title;
}
else {
$title = $this
->cleanTitle($file
->getFilename());
}
$image = $this->imageFactory
->get($file
->getFileUri());
$defaultWeight = $this->connection
->select('photos_image_field_data', 'i')
->fields('i', [
'weight',
])
->condition('i.album_id', $albumId)
->orderBy('i.weight', 'DESC')
->execute()
->fetchField();
if ($image
->isValid()) {
$newPhotosImageEntity = [
'album_id' => $albumId,
'title' => $title,
'weight' => isset($file->weight) ? $file->weight : $defaultWeight + 1,
'description' => isset($file->des) ? $file->des : '',
];
$uploadField = $config
->get('multi_upload_default_field');
$uploadFieldParts = explode(':', $uploadField);
$field = isset($uploadFieldParts[0]) ? $uploadFieldParts[0] : 'field_image';
$allBundleFields = $this->entityFieldManager
->getFieldDefinitions('photos_image', 'photos_image');
if (isset($allBundleFields[$field])) {
$fieldType = $allBundleFields[$field]
->getType();
if ($fieldType == 'image') {
$newPhotosImageEntity[$field] = [
'target_id' => $fid,
'alt' => $title,
'title' => $title,
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
];
}
else {
if ($fieldType == 'entity_reference') {
$mediaField = isset($uploadFieldParts[1]) ? $uploadFieldParts[1] : '';
$mediaBundle = isset($uploadFieldParts[2]) ? $uploadFieldParts[2] : '';
if ($mediaField && $mediaBundle) {
$values = [
'bundle' => $mediaBundle,
'uid' => $this->currentUser
->id(),
];
$values[$mediaField] = [
'target_id' => $file
->id(),
];
$media = Media::create($values);
$media
->setName('Photo ' . $file
->id())
->setPublished()
->save();
$newPhotosImageEntity[$field] = [
'target_id' => $media
->id(),
];
}
}
}
}
$photosImage = PhotosImage::create($newPhotosImageEntity);
try {
$photosImage
->save();
if ($photosImage && $photosImage
->id()) {
if (isset($fieldType) && $fieldType == 'image') {
$fieldThirdPartySettings = $allBundleFields[$field]
->getThirdPartySettings('filefield_paths');
if (!empty($fieldThirdPartySettings) && $fieldThirdPartySettings['enabled']) {
$tokenData = [
'file' => $file,
$photosImage
->getEntityTypeId() => $photosImage,
];
$name = $file
->getFilename();
if (!empty($fieldThirdPartySettings['file_name']['value'])) {
$name = filefield_paths_process_string($fieldThirdPartySettings['file_name']['value'], $tokenData, $fieldThirdPartySettings['file_name']['options']);
}
$path = filefield_paths_process_string($fieldThirdPartySettings['file_path']['value'], $tokenData, $fieldThirdPartySettings['file_path']['options']);
$fileUri = $this->streamWrapperManager
->normalizeUri($this->streamWrapperManager
->getScheme($file
->getFileUri()) . '://' . $path . DIRECTORY_SEPARATOR . $name);
}
else {
$fieldSettings = $allBundleFields[$field]
->getSettings();
$uploadLocation = $fieldSettings['file_directory'];
$uploadLocation = PlainTextOutput::renderFromHtml($this->token
->replace($uploadLocation, []));
$uploadLocation = $this->streamWrapperManager
->getScheme($file
->getFileUri()) . '://' . $uploadLocation;
$this->fileSystem
->prepareDirectory($uploadLocation, FileSystemInterface::CREATE_DIRECTORY);
$fileUri = "{$uploadLocation}/{$file->getFilename()}";
$fileUri = $this->fileSystem
->getDestinationFilename($fileUri, FileSystemInterface::EXISTS_RENAME);
$this->fileSystem
->move($file
->getFileUri(), $fileUri, FileSystemInterface::EXISTS_ERROR);
}
$file
->setFileUri($fileUri);
$file
->save();
}
if ($config
->get('photos_user_count_cron')) {
$user = $this->currentUser;
PhotosAlbum::setCount('user_image', $photosImage
->getOwnerId() ? $photosImage
->getOwnerId() : $user
->id());
PhotosAlbum::setCount('node_album', $albumId);
}
$this->fileUsage
->add($file, 'photos', 'node', $albumId);
if ($photos_size_max = $config
->get('photos_size_max')) {
file_validate_image_resolution($file, $photos_size_max);
if (isset($fieldType)) {
$image = $this->imageFactory
->get($file
->getFileUri());
if ($fieldType == 'image') {
$image_files = $photosImage
->get($field)
->getValue();
$image_files[0]['height'] = $image
->getHeight();
$image_files[0]['width'] = $image
->getWidth();
$photosImage
->set($field, $image_files);
$photosImage
->save();
}
else {
if (isset($media) && isset($mediaField)) {
$image_files = $media
->get($mediaField)
->getValue();
$image_files[0]['height'] = $image
->getHeight();
$image_files[0]['width'] = $image
->getWidth();
$media
->set($mediaField, $image_files);
$media
->save();
}
}
}
}
return TRUE;
}
} catch (EntityStorageException $e) {
watchdog_exception('photos', $e);
}
}
}
return FALSE;
}
public function saveExistingMedia($mediaId, $albumId) {
$config = $this->configFactory
->get('photos.settings');
$mediaItem = NULL;
try {
$mediaItem = $this->entityTypeManager
->getStorage('media')
->load($mediaId);
} catch (InvalidPluginDefinitionException $e) {
} catch (PluginNotFoundException $e) {
}
if ($mediaItem) {
$defaultWeight = $this->connection
->select('photos_image_field_data', 'i')
->fields('i', [
'weight',
])
->condition('i.album_id', $albumId)
->orderBy('i.weight', 'DESC')
->execute()
->fetchField();
$newPhotosImageEntity = [
'album_id' => $albumId,
'title' => $mediaItem
->getName(),
'weight' => $defaultWeight + 1,
];
$uploadField = $config
->get('multi_upload_default_field');
$uploadFieldParts = explode(':', $uploadField);
$field = isset($uploadFieldParts[0]) ? $uploadFieldParts[0] : 'field_image';
$allBundleFields = $this->entityFieldManager
->getFieldDefinitions('photos_image', 'photos_image');
if (isset($allBundleFields[$field])) {
$fieldType = $allBundleFields[$field]
->getType();
if ($fieldType == 'entity_reference') {
$mediaField = isset($uploadFieldParts[1]) ? $uploadFieldParts[1] : '';
$mediaBundle = isset($uploadFieldParts[2]) ? $uploadFieldParts[2] : '';
if ($mediaField && $mediaBundle) {
$newPhotosImageEntity[$field] = [
'target_id' => $mediaId,
];
}
$photosImage = PhotosImage::create($newPhotosImageEntity);
try {
$photosImage
->save();
if ($photosImage && $photosImage
->id()) {
if ($config
->get('photos_user_count_cron')) {
$user = $this->currentUser;
PhotosAlbum::setCount('user_image', $photosImage
->getOwnerId() ? $photosImage
->getOwnerId() : $user
->id());
PhotosAlbum::setCount('node_album', $albumId);
}
return TRUE;
}
} catch (EntityStorageException $e) {
watchdog_exception('photos', $e);
}
}
}
}
return FALSE;
}
public function unzip($source, array $params, $scheme = 'default') {
$fileConfig = $this->configFactory
->get('system.file');
$file_count = 0;
$photo_count = 0;
if (isset($params['photo_count'])) {
$photo_count = $params['photo_count'];
}
$album_photo_limit = NULL;
if (isset($params['album_photo_limit'])) {
$album_photo_limit = $params['album_photo_limit'];
}
if (version_compare(PHP_VERSION, '5') >= 0) {
if (!is_file($source)) {
$this->messenger
->addMessage($this
->t('Compressed file does not exist, please check the path: @src', [
'@src' => $source,
]));
return 0;
}
$fileType = [
'jpg',
'gif',
'png',
'jpeg',
'JPG',
'GIF',
'PNG',
'JPEG',
];
$zip = new \ZipArchive();
$default_scheme = $fileConfig
->get('default_scheme');
$relative_path = $this->fileSystem
->realpath($default_scheme . "://") . '/';
$source = str_replace($default_scheme . '://', $relative_path, $source);
if ($zip
->open($source) === TRUE) {
for ($x = 0; $x < $zip->numFiles; ++$x) {
$image = $zip
->statIndex($x);
$filename_parts = explode('.', $image['name']);
$ext = end($filename_parts);
if (in_array($ext, $fileType)) {
if ($album_photo_limit && $photo_count >= $album_photo_limit) {
$this->messenger
->addWarning($this
->t('Maximum number of photos reached for this album.'));
break;
}
$path = $this->fileSystem
->createFilename($image['name'], $this
->path($scheme));
if ($temp_file = file_save_data($zip
->getFromIndex($x), $path)) {
$temp_file->album_id = $params['album_id'];
$temp_file->nid = $params['nid'];
$temp_file->title = $image['name'];
$temp_file->des = $params['des'];
$file = $temp_file;
try {
$file
->save();
if ($this
->saveImage($file)) {
$file_count++;
}
} catch (EntityStorageException $e) {
watchdog_exception('photos', $e);
}
}
}
}
$zip
->close();
$this->fileSystem
->delete($source);
}
else {
$this->messenger
->addWarning($this
->t('Compressed file does not exist, please try again: @src', [
'@src' => $source,
]));
}
}
return $file_count;
}
}