diff --git a/composer.json b/composer.json index 4351f44..f8cf9dc 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,6 @@ ], "require": { "php": "^7.1", - "ext-zip": "*", "drupal/core": "~8.4", "drush/drush": "~9.0", "cweagans/composer-patches": "~1.0", diff --git a/modules/gdpr_tasks/src/Plugin/Archiver/Zip.php b/modules/gdpr_tasks/src/Plugin/Archiver/Zip.php new file mode 100644 index 0000000..01c7641 --- /dev/null +++ b/modules/gdpr_tasks/src/Plugin/Archiver/Zip.php @@ -0,0 +1,46 @@ +zip = new \ZipArchive(); + if ((file_exists($file_path) && $this->zip->open($file_path, \ZipArchive::OVERWRITE) !== TRUE) || $this->zip->open($file_path, \ZipArchive::CREATE) !== TRUE) { + throw new ArchiverException(t('Cannot open %file_path', array('%file_path' => $file_path))); + } + } + + /** + * {@inheritdoc} + */ + public function add($file_path) { + $local_name = basename($file_path); + $this->zip->addFile($file_path, $local_name); + + return $this; + } + + /** + * Method to close the opened archive file. + */ + public function close() { + $this->zip->close(); + } +} diff --git a/modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php b/modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php index 5e1a7ed..0139550 100644 --- a/modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php +++ b/modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php @@ -12,6 +12,7 @@ use Drupal\Core\Queue\QueueInterface; use Drupal\Core\Queue\QueueWorkerBase; use Drupal\gdpr_fields\EntityTraversalFactory; use Drupal\gdpr_tasks\Entity\TaskInterface; +use Drupal\gdpr_tasks\Plugin\Archiver\Zip; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -348,12 +349,7 @@ class GdprTasksSarWorker extends QueueWorkerBase implements ContainerFactoryPlug $file_path = $this->fileSystem->realpath($file->uri->value); - $zip = new \ZipArchive(); - if (!$zip->open($file_path, \ZipArchive::CREATE | \ZipArchive::OVERWRITE)) { - // @todo: Improve error handling. - $this->messenger->addError(t('Error opening file.')); - return; - } + $zip = new Zip($file_path); // Gather all the files we need to include in this package. $part_files = []; @@ -364,7 +360,7 @@ class GdprTasksSarWorker extends QueueWorkerBase implements ContainerFactoryPlug // Add the file to the zip. // @todo: Add error handling. - $zip->addFile($this->fileSystem->realpath($part_file->uri->value), $part_file->filename->value); + $zip->add($this->fileSystem->realpath($part_file->uri->value), $part_file->filename->value); } // Add in any attached files that need including. @@ -374,7 +370,7 @@ class GdprTasksSarWorker extends QueueWorkerBase implements ContainerFactoryPlug // Add the file to the zip. $filename = "assets/{$asset_file->fid->value}." . pathinfo($asset_file->uri->value, PATHINFO_EXTENSION); // @todo: Add error handling. - $zip->addFile($this->fileSystem->realpath($asset_file->uri->value), $filename); + $zip->add($this->fileSystem->realpath($asset_file->uri->value), $filename); } // Clear our parts and assets file lists.