View source
<?php
namespace Drupal\photos;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Component\Transliteration\TransliterationInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
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\Messenger\MessengerInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\photos\Entity\PhotosImage;
class PhotosUpload implements PhotosUploadInterface {
use StringTranslationTrait;
protected $configFactory;
protected $entityTypeManager;
protected $fileSystem;
protected $messenger;
protected $moduleHandler;
protected $streamWrapperManager;
protected $transliteration;
public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_manager, FileSystem $file_system, MessengerInterface $messenger, ModuleHandlerInterface $module_handler, StreamWrapperManagerInterface $stream_wrapper_manager, TransliterationInterface $transliteration) {
$this->configFactory = $config_factory;
$this->entityTypeManager = $entity_manager;
$this->fileSystem = $file_system;
$this->messenger = $messenger;
$this->moduleHandler = $module_handler;
$this->streamWrapperManager = $stream_wrapper_manager;
$this->transliteration = $transliteration;
}
public function cleanTitle($title = '') {
if (\Drupal::config('photos.settings')
->get('photos_clean_title')) {
$title = pathinfo($title, PATHINFO_FILENAME);
$title = preg_replace("/[\\-_]/", " ", $title);
$title = trim($title);
}
return $title;
}
public function path($schemaType = 'default') {
$path[] = 'photos';
switch ($schemaType) {
case 'private':
$scheme = 'private';
break;
case 'public':
$scheme = 'public';
break;
case 'default':
default:
$scheme = \Drupal::config('system.file')
->get('default_scheme');
break;
}
$dirs = [];
$finalPath = FALSE;
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(File $file) {
if ($file
->id() && isset($file->album_id)) {
$fid = $file
->id();
$album_id = $file->album_id;
if (isset($file->title) && !empty($file->title)) {
$title = $file->title;
}
else {
$title = $this
->cleanTitle($file
->getFilename());
}
$image = \Drupal::service('image.factory')
->get($file
->getFileUri());
if ($image
->isValid()) {
$newPhotosImageEntity = [
'album_id' => $file->album_id,
'title' => $title,
'weight' => isset($file->weight) ? $file->weight : 0,
'description' => isset($file->des) ? $file->des : '',
];
$uploadField = \Drupal::config('photos.settings')
->get('multi_upload_default_field');
$uploadFieldParts = explode(':', $uploadField);
$field = isset($uploadFieldParts[0]) ? $uploadFieldParts[0] : 'field_image';
$allBundleFields = \Drupal::service('entity_field.manager')
->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' => \Drupal::currentUser()
->id(),
];
$values[$mediaField] = [
'target_id' => $file
->id(),
];
$media = Media::create($values);
$media
->setName('Photo ' . $file
->id())
->setPublished(TRUE)
->save();
$newPhotosImageEntity[$field] = [
'target_id' => $media
->id(),
];
}
}
}
}
$photosImage = PhotosImage::create($newPhotosImageEntity);
try {
$photosImage
->save();
if ($photosImage && $photosImage
->id()) {
if (\Drupal::config('photos.settings')
->get('photos_user_count_cron')) {
$user = \Drupal::currentUser();
PhotosAlbum::setCount('user_image', $photosImage
->getOwnerId() ? $photosImage
->getOwnerId() : $user
->id());
PhotosAlbum::setCount('node_album', $file->album_id);
}
$file_usage = \Drupal::service('file.usage');
$file_usage
->add($file, 'photos', 'node', $album_id);
if ($photos_size_max = \Drupal::config('photos.settings')
->get('photos_size_max')) {
file_validate_image_resolution($file, $photos_size_max);
}
return TRUE;
}
} catch (EntityStorageException $e) {
watchdog_exception('photos', $e);
}
}
}
return FALSE;
}
public function saveExistingMedia($mediaId, $albumId) {
$mediaItem = NULL;
try {
$mediaItem = $this->entityTypeManager
->getStorage('media')
->load($mediaId);
} catch (InvalidPluginDefinitionException $e) {
} catch (PluginNotFoundException $e) {
}
if ($mediaItem) {
$newPhotosImageEntity = [
'album_id' => $albumId,
'title' => $mediaItem
->getName(),
];
$uploadField = \Drupal::config('photos.settings')
->get('multi_upload_default_field');
$uploadFieldParts = explode(':', $uploadField);
$field = isset($uploadFieldParts[0]) ? $uploadFieldParts[0] : 'field_image';
$allBundleFields = \Drupal::service('entity_field.manager')
->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 (\Drupal::config('photos.settings')
->get('photos_user_count_cron')) {
$user = \Drupal::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, $params, $scheme = 'default') {
$file_count = 0;
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 = \Drupal::config('system.file')
->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)) {
$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;
}
}