public function PhotosUpload::unzip in Album Photos 8.5
Same name and namespace in other branches
- 8.4 src/PhotosUpload.php \Drupal\photos\PhotosUpload::unzip()
- 6.0.x src/PhotosUpload.php \Drupal\photos\PhotosUpload::unzip()
Unzip archive of image files.
Parameters
string $source: The zip file location.
array $params: Array of additional parameters like album id.
string $scheme: The file scheme.
Overrides PhotosUploadInterface::unzip
File
- src/
PhotosUpload.php, line 313
Class
- PhotosUpload
- Functions to help with uploading images to albums.
Namespace
Drupal\photosCode
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();
// Get relative path.
$default_scheme = \Drupal::config('system.file')
->get('default_scheme');
$relative_path = $this->fileSystem
->realpath($default_scheme . "://") . '/';
$source = str_replace($default_scheme . '://', $relative_path, $source);
// Open zip archive.
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)) {
// Update file values.
$temp_file->album_id = $params['album_id'];
$temp_file->nid = $params['nid'];
// Use image file name as title.
$temp_file->title = $image['name'];
$temp_file->des = $params['des'];
// Prepare file entity.
$file = $temp_file;
try {
// Save image.
$file
->save();
if ($this
->saveImage($file)) {
$file_count++;
}
} catch (EntityStorageException $e) {
watchdog_exception('photos', $e);
}
}
}
}
$zip
->close();
// Delete zip file.
$this->fileSystem
->delete($source);
}
else {
$this->messenger
->addWarning($this
->t('Compressed file does not exist, please try again: @src', [
'@src' => $source,
]));
}
}
return $file_count;
}