diff options
| author | Greg Roach <greg@subaqua.co.uk> | 2025-05-13 10:43:42 +0100 |
|---|---|---|
| committer | Greg Roach <greg@subaqua.co.uk> | 2025-05-13 10:43:42 +0100 |
| commit | 036af8696129956af9534bd1e19e74a9d2305337 (patch) | |
| tree | ba19f51f272e06c7fed2b7b28df14034199322bd | |
| parent | b58a5cf7f9517ca9e16991fcdfc18179b774ee71 (diff) | |
| download | webtrees-036af8696129956af9534bd1e19e74a9d2305337.tar.gz webtrees-036af8696129956af9534bd1e19e74a9d2305337.tar.bz2 webtrees-036af8696129956af9534bd1e19e74a9d2305337.zip | |
Use ZipArchive directly, instead of ZipArchiveAdapter
| -rw-r--r-- | app/Services/GedcomExportService.php | 33 | ||||
| -rw-r--r-- | phpstan-baseline.neon | 6 |
2 files changed, 28 insertions, 11 deletions
diff --git a/app/Services/GedcomExportService.php b/app/Services/GedcomExportService.php index 0ef65c5dd8..1905d3932e 100644 --- a/app/Services/GedcomExportService.php +++ b/app/Services/GedcomExportService.php @@ -31,6 +31,7 @@ use Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\Header; use Fisharebest\Webtrees\Registry; +use Fisharebest\Webtrees\Site; use Fisharebest\Webtrees\Tree; use Fisharebest\Webtrees\Webtrees; use Illuminate\Database\Query\Builder; @@ -38,12 +39,11 @@ use Illuminate\Database\Query\Expression; use Illuminate\Support\Collection; use League\Flysystem\Filesystem; use League\Flysystem\FilesystemOperator; -use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider; -use League\Flysystem\ZipArchive\ZipArchiveAdapter; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamFactoryInterface; use RuntimeException; +use ZipArchive; use function addcslashes; use function date; @@ -123,9 +123,8 @@ class GedcomExportService // Create a new/empty .ZIP file $temp_zip_file = stream_get_meta_data(tmpfile())['uri']; - $zip_provider = new FilesystemZipArchiveProvider($temp_zip_file, 0755); - $zip_adapter = new ZipArchiveAdapter($zip_provider); - $zip_filesystem = new Filesystem($zip_adapter); + $zip_filesystem = new ZipArchive(); + $zip_filesystem->open($temp_zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE); if ($format === 'zipmedia') { $media_path = $tree->getPreference('MEDIA_DIRECTORY'); @@ -139,15 +138,17 @@ class GedcomExportService $resource = $this->export($tree, $sort_by_xref, $encoding, $access_level, $line_endings, $records, $zip_filesystem, $media_path); if ($format === 'gedzip') { - $zip_filesystem->writeStream('gedcom.ged', $resource); + $zip_filesystem->addFromString('gedcom.ged', stream_get_contents($resource)); $extension = '.gdz'; } else { - $zip_filesystem->writeStream($filename . '.ged', $resource); + $zip_filesystem->addFromString($filename . '.ged', stream_get_contents($resource)); $extension = '.zip'; } fclose($resource); + $zip_filesystem->close(); + $stream = $this->stream_factory->createStreamFromFile($temp_zip_file); return $this->response_factory->createResponse() @@ -165,7 +166,7 @@ class GedcomExportService * @param int $access_level Apply privacy filtering * @param string $line_endings CRLF or LF * @param Collection<int,string|object|GedcomRecord>|null $records Just export these records - * @param FilesystemOperator|null $zip_filesystem Write media files to this filesystem + * @param ZipArchive|FilesystemOperator|null $zip_filesystem Write media files to this filesystem * @param string|null $media_path Location within the zip filesystem * * @return resource @@ -177,7 +178,7 @@ class GedcomExportService int $access_level = Auth::PRIV_HIDE, string $line_endings = 'CRLF', Collection|null $records = null, - FilesystemOperator|null $zip_filesystem = null, + ZipArchive|FilesystemOperator|null $zip_filesystem = null, string|null $media_path = null ) { $stream = fopen('php://memory', 'wb+'); @@ -242,14 +243,24 @@ class GedcomExportService $datum->o_gedcom; } - if ($media_path !== null && $zip_filesystem !== null && preg_match('/0 @' . Gedcom::REGEX_XREF . '@ OBJE/', $gedcom) === 1) { + if ($media_path !== null && preg_match('/^0 @' . Gedcom::REGEX_XREF . '@ OBJE/', $gedcom) === 1) { preg_match_all('/\n1 FILE (.+)/', $gedcom, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $media_file = $match[1]; if ($media_filesystem->fileExists($media_file)) { - $zip_filesystem->writeStream($media_path . $media_file, $media_filesystem->readStream($media_file)); + if ($zip_filesystem instanceof Filesystem) { + $zip_filesystem->writeStream($media_path . $media_file, $media_filesystem->readStream($media_file)); + } + + if ($zip_filesystem instanceof ZipArchive) { + // If the media file is stored locally, we can add it directly to the ZipArchive + // $local_file = Site::getPreference('INDEX_DIRECTORY') . $tree->getPreference('MEDIA_DIRECTORY') . $media_path . $media_file; + // $zip_filesystem->addFile($local_file, $media_path . $media_file); + + $zip_filesystem->addFromString($media_path . $media_file, $media_filesystem->read($media_file)); + } } } } diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 85b2528cb0..449f356322 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -6325,6 +6325,12 @@ parameters: path: app/Services/GedcomExportService.php - + message: '#^Parameter \#2 \$content of method ZipArchive\:\:addFromString\(\) expects string, string\|false given\.$#' + identifier: argument.type + count: 2 + path: app/Services/GedcomExportService.php + + - message: '#^Parameter \#2 \$subject of function preg_match expects string, mixed given\.$#' identifier: argument.type count: 1 |
