View source
<?php
namespace Drupal\photos;
use Drupal\Component\Utility\Html;
use Drupal\Core\Cache\Cache;
use Drupal\Core\File\FileSystemInterface;
use Drupal\file\Entity\File;
class PhotosUpload {
public static function rename($name = 0, $ext = 0) {
if (\Drupal::config('photos.settings')
->get('photos_rname')) {
if ($name) {
$name_parts = explode('.', $name);
return round(rand(15770, 967049700)) . \Drupal::time()
->getRequestTime() . '.' . ($ext ? $ext : end($name_parts));
}
if (!empty($_FILES['files'])) {
foreach ($_FILES['files']['name'] as $field => $filename) {
$filename_parts = explode('.', $filename);
$_FILES['files']['name'][$field] = round(rand(15770, 967049700)) . \Drupal::time()
->getRequestTime() . '.' . ($ext ? $ext : end($filename_parts));
}
}
}
elseif ($name) {
return $name;
}
}
public static function saveFile($file, $val = []) {
$errors = [];
foreach ($val as $function => $args) {
array_unshift($args, $file);
$errors = array_merge($errors, call_user_func_array($function, $args));
}
if (!empty($errors)) {
$message = t('The selected file %name could not be uploaded.', [
'%name' => $file->filename,
]);
if (count($errors) > 1) {
$message .= '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
}
else {
$message .= ' ' . array_pop($errors);
}
\Drupal::messenger()
->addMessage($message);
return 0;
}
$file
->save();
return $file
->id();
}
public static function saveImage($file) {
$exif = $file
->getMimeType() == 'image/jpeg' ? 1 : 0;
if ($file
->id() && isset($file->pid)) {
$fid = $file
->id();
$pid = $file->pid;
$db = \Drupal::database();
$db
->merge('photos_image')
->key([
'fid' => $file
->id(),
])
->fields([
'pid' => $file->pid,
'title' => isset($file->title) ? $file->title : $file
->getFilename(),
'des' => isset($file->des) ? $file->des : '',
'wid' => isset($file->wid) ? $file->wid : 0,
'comcount' => 0,
'count' => 0,
'exif' => $exif,
])
->execute();
if (isset($fid) && !empty($fid)) {
if (isset($file->nid)) {
$db
->insert('photos_node')
->fields([
'nid' => $file->nid,
'fid' => $file
->id(),
])
->execute();
}
if (\Drupal::config('photos.settings')
->get('photos_user_count_cron')) {
$user = \Drupal::currentUser();
PhotosAlbum::setCount('user_image', $file->getOwnerId ? $file->getOwnerId : $user
->id());
PhotosAlbum::setCount('node_album', $file->pid);
if (isset($file->nid)) {
PhotosAlbum::setCount('node_node', $file->nid);
}
}
$file_usage = \Drupal::service('file.usage');
$file_usage
->add($file, 'photos', 'node', $pid);
if ($photos_size_max = \Drupal::config('photos.settings')
->get('photos_size_max')) {
file_validate_image_resolution($file, $photos_size_max);
}
return TRUE;
}
}
return FALSE;
}
public static function path($type = 'default', $file = FALSE, $account = FALSE) {
if (!$account) {
$user = \Drupal::currentUser();
$account = $user;
}
$path = [];
if (\Drupal::config('photos.settings')
->get('photos_path')) {
$mm = \Drupal::service('date.formatter')
->format(\Drupal::time()
->getRequestTime(), 'custom', "Y|m|d");
$m = explode('|', $mm);
$a = [
'%uid' => $account
->id(),
'%username' => $account
->getAccountName(),
'%Y' => $m[0],
'%m' => $m[1],
'%d' => $m[2],
];
$b = strtr(\Drupal::config('photos.settings')
->get('photos_path'), $a);
$path = explode('/', $b);
}
else {
$path[] = 'photos';
}
switch ($type) {
case 'default':
$scheme = \Drupal::config('system.file')
->get('default_scheme');
break;
case 'private':
$scheme = 'private';
break;
case 'public':
$scheme = 'public';
break;
}
$dirs = [];
$t = FALSE;
foreach ($path as $folder) {
$dirs[] = $folder;
$t = $scheme . '://' . implode('/', $dirs);
if (!\Drupal::service('file_system')
->prepareDirectory($t, FileSystemInterface::CREATE_DIRECTORY)) {
return FALSE;
}
}
return $t;
}
public static function unzip($source, $value, $scheme = 'default', $account = FALSE) {
$file_count = 0;
if (version_compare(PHP_VERSION, '5') >= 0) {
if (!is_file($source)) {
\Drupal::messenger()
->addMessage(t('Compressed file does not exist, please check the path: @src', [
'@src' => $source,
]));
return 0;
}
$type = [
'jpg',
'gif',
'png',
'jpeg',
'JPG',
'GIF',
'PNG',
'JPEG',
];
$zip = new \ZipArchive();
$default_scheme = \Drupal::config('system.file')
->get('default_scheme');
$relative_path = \Drupal::service('file_system')
->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, $type)) {
$path = \Drupal::service('file_system')
->createFilename(PhotosUpload::rename($image['name'], $ext), PhotosUpload::path($scheme, '', $account));
if ($temp_file = file_save_data($zip
->getFromIndex($x), $path)) {
$temp_file->pid = $value->pid;
$temp_file->nid = $value->nid;
$temp_file->title = $image['name'];
$temp_file->des = $value->des;
$file = $temp_file;
if (PhotosUpload::saveFile($file)) {
if (PhotosUpload::saveImage($file)) {
$file_count++;
}
}
}
}
}
$zip
->close();
\Drupal::service('file_system')
->delete($source);
}
else {
\Drupal::messenger()
->addWarning(t('Compressed file does not exist, please try again: @src', [
'@src' => $source,
]));
}
}
return $file_count;
}
public static function moveImageFiles($files, $account, $nid, $scheme, $allow_zip, $file_extensions, $copy, &$context) {
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_id'] = 0;
$context['sandbox']['max'] = count($files);
$context['results']['images_processed'] = 0;
$context['results']['nid'] = $nid;
$context['results']['uid'] = $account
->id();
$context['results']['copy'] = $copy;
}
$limit = 20;
$process_files = array_slice($files, $context['sandbox']['current_id'], $limit);
$count = 0;
foreach ($process_files as $dir_file) {
$ext = mb_substr($dir_file->uri, -3);
if ($ext != 'zip' && $ext != 'ZIP') {
$photos_path = PhotosUpload::path($scheme, '', $account);
$photos_name = PhotosUpload::rename($dir_file->filename);
$file_uri = \Drupal::service('file_system')
->getDestinationFilename($photos_path . '/' . $photos_name, FileSystemInterface::EXISTS_RENAME);
$context['message'] = t('Processing:') . ' ' . Html::escape($photos_name);
if ($copy) {
$file_processed = \Drupal::service('file_system')
->copy($dir_file->uri, $file_uri);
}
else {
$file_processed = \Drupal::service('file_system')
->move($dir_file->uri, $file_uri);
}
if ($file_processed) {
$image = \Drupal::service('image.factory')
->get($file_uri);
if ($image
->getWidth()) {
$file = File::create([
'uri' => $file_uri,
'uid' => $account
->id(),
'status' => FILE_STATUS_PERMANENT,
'pid' => $nid,
'nid' => $nid,
'filename' => $photos_name,
'filesize' => $image
->getFileSize(),
'filemime' => $image
->getMimeType(),
]);
if (PhotosUpload::saveFile($file)) {
PhotosUpload::saveImage($file);
$count++;
}
}
}
}
else {
if (!\Drupal::config('photos.settings')
->get('photos_upzip')) {
\Drupal::messenger()
->addError(t('Please update settings to allow zip uploads.'));
}
else {
$directory = PhotosUpload::path();
\Drupal::service('file_system')
->prepareDirectory($directory);
$context['message'] = t('Processing:') . ' ' . Html::escape($dir_file->uri);
$zip = \Drupal::service('file_system')
->getDestinationFilename($directory . '/' . trim(basename($dir_file->uri)), FileSystemInterface::EXISTS_RENAME);
if ($copy) {
$file_processed = \Drupal::service('file_system')
->copy($dir_file->uri, $zip);
}
else {
$file_processed = \Drupal::service('file_system')
->move($dir_file->uri, $zip);
}
if ($file_processed) {
$value = new \stdClass();
$value->pid = $nid;
$value->nid = $nid;
$value->des = '';
$value->title = $dir_file->filename;
if (!($file_count = PhotosUpload::unzip($zip, $value, $scheme, $account))) {
}
else {
$count = $count + $file_count;
}
}
}
}
$context['sandbox']['progress']++;
$context['sandbox']['current_id']++;
}
$context['results']['images_processed'] += $count;
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
public static function finishedMovingImageFiles($success, $results, $operations) {
Cache::invalidateTags([
'node:' . $results['nid'],
'photos:album:' . $results['nid'],
]);
PhotosAlbum::setCount('user_image', $results['uid']);
PhotosAlbum::setCount('node_album', $results['nid']);
if ($success) {
if ($results['copy']) {
$message = \Drupal::translation()
->formatPlural($results['images_processed'], 'One image copied to selected album.', '@count images copied to selected album.');
}
else {
$message = \Drupal::translation()
->formatPlural($results['images_processed'], 'One image moved to selected album.', '@count images moved to selected album.');
}
}
else {
$message = t('Finished with an error.');
}
\Drupal::messenger()
->addMessage($message);
}
}