Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Php 32bit - Zip-stream with version 2.1 instead of 3.1 #2934

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/Actions/Album/Archive32.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Actions\Album;

use App\Services\Archives\Zip21Trait;

final class Archive32 extends BaseArchive
{
use Zip21Trait;
}
16 changes: 16 additions & 0 deletions app/Actions/Album/Archive64.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Actions\Album;

use App\Services\Archives\Zip31Trait;

final class Archive64 extends BaseArchive
{
use Zip31Trait;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@
use App\Exceptions\ConfigurationKeyMissingException;
use App\Exceptions\Handler;
use App\Exceptions\Internal\FrameworkException;
use App\Exceptions\Internal\LycheeLogicException;
use App\Image\Files\BaseMediaFile;
use App\Image\Files\FlysystemFile;
use App\Models\Album;
use App\Models\Configs;
use App\Models\Photo;
use App\Models\TagAlbum;
use App\Policies\AlbumPolicy;
use App\Policies\PhotoPolicy;
use App\SmartAlbums\BaseSmartAlbum;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Gate;
Expand All @@ -27,12 +32,11 @@
use function Safe\set_time_limit;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\StreamedResponse;
use ZipStream\CompressionMethod as ZipMethod;
use ZipStream\Exception\FileNotFoundException;
use ZipStream\Exception\FileNotReadableException;
use ZipStream\ZipStream;

class Archive extends Action
abstract class BaseArchive extends Action
{
public const BAD_CHARS = [
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07",
Expand All @@ -44,6 +48,23 @@ class Archive extends Action

protected int $deflateLevel = -1;

/**
* Resolve which version of the archive to use.
*
* @return BaseArchive
*/
public static function resolve(): self
{
if (InstalledVersions::satisfies(new VersionParser(), 'maennchen/zipstream-php', '^3.1')) {
return new Archive64();
}
if (InstalledVersions::satisfies(new VersionParser(), 'maennchen/zipstream-php', '^2.1')) {
return new Archive32();
}

throw new LycheeLogicException('Unsupported version of maennchen/zipstream-php');
}

/**
* @param Collection<int,AbstractAlbum> $albums
*
Expand All @@ -66,10 +87,7 @@ public function do(Collection $albums): StreamedResponse
$this->deflateLevel = Configs::getValueAsInt('zip_deflate_level');

$responseGenerator = function () use ($albums) {
$zip = new ZipStream(defaultCompressionMethod: $this->deflateLevel === -1 ? ZipMethod::STORE : ZipMethod::DEFLATE,
defaultDeflateLevel: $this->deflateLevel,
enableZip64: Configs::getValueAsBool('zip64'),
defaultEnableZeroHeader: true, sendHttpHeaders: false);
$zip = $this->createZip();

$usedDirNames = [];
foreach ($albums as $album) {
Expand Down Expand Up @@ -105,6 +123,13 @@ public function do(Collection $albums): StreamedResponse
return $response;
}

/**
* @return ZipStream
*
* @throws ConfigurationKeyMissingException
*/
abstract protected function createZip(): ZipStream;

/**
* Create the title of the ZIP archive.
*
Expand Down Expand Up @@ -227,7 +252,7 @@ private function compressAlbum(AbstractAlbum $album, array &$usedDirNames, ?stri
} catch (InfoException) {
// Silently do nothing, if `set_time_limit` is denied.
}
$zip->addFileFromStream(fileName: $fileName, stream: $file->read(), comment: $photo->title, lastModificationDateTime: $photo->taken_at);
$this->addFileToZip($zip, $fileName, $file, $photo);
$file->close();
} catch (\Throwable $e) {
Handler::reportSafely($e);
Expand All @@ -248,4 +273,6 @@ private function compressAlbum(AbstractAlbum $album, array &$usedDirNames, ?stri
}
}
}

abstract protected function addFileToZip(ZipStream $zip, string $fileName, FlysystemFile|BaseMediaFile $file, Photo|null $photo): void;
}
16 changes: 16 additions & 0 deletions app/Actions/Photo/Archive32.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Actions\Photo;

use App\Services\Archives\Zip21Trait;

final class Archive32 extends BaseArchive
{
use Zip21Trait;
}
16 changes: 16 additions & 0 deletions app/Actions/Photo/Archive64.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/

namespace App\Actions\Photo;

use App\Services\Archives\Zip31Trait;

final class Archive64 extends BaseArchive
{
use Zip31Trait;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
use App\Exceptions\ConfigurationKeyMissingException;
use App\Exceptions\Internal\FrameworkException;
use App\Exceptions\Internal\InvalidSizeVariantException;
use App\Exceptions\Internal\LycheeLogicException;
use App\Image\Files\BaseMediaFile;
use App\Image\Files\FlysystemFile;
use App\Models\Configs;
use App\Models\Photo;
use Composer\InstalledVersions;
use Composer\Semver\VersionParser;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Safe\Exceptions\InfoException;
Expand All @@ -28,10 +32,9 @@
use function Safe\stream_copy_to_stream;
use Symfony\Component\HttpFoundation\HeaderUtils;
use Symfony\Component\HttpFoundation\StreamedResponse;
use ZipStream\CompressionMethod as ZipMethod;
use ZipStream\ZipStream;

class Archive
abstract class BaseArchive
{
public const BAD_CHARS = [
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07",
Expand All @@ -43,6 +46,23 @@ class Archive

protected int $deflateLevel = -1;

/**
* Resolve which version of the archive to use.
*
* @return BaseArchive
*/
public static function resolve(): self
{
if (InstalledVersions::satisfies(new VersionParser(), 'maennchen/zipstream-php', '^3.1')) {
return new Archive64();
}
if (InstalledVersions::satisfies(new VersionParser(), 'maennchen/zipstream-php', '^2.1')) {
return new Archive32();
}

throw new LycheeLogicException('Unsupported version of maennchen/zipstream-php');
}

/**
* Returns a response for a downloadable file.
*
Expand Down Expand Up @@ -147,7 +167,7 @@ protected function zip(Collection $photos, DownloadVariantType $downloadVariant)
$this->deflateLevel = Configs::getValueAsInt('zip_deflate_level');

$responseGenerator = function () use ($downloadVariant, $photos) {
$zip = new ZipStream(enableZip64: Configs::getValueAsBool('zip64'), defaultEnableZeroHeader: true, sendHttpHeaders: false);
$zip = $this->createZip();

// We first need to scan the whole array of files to avoid
// problems with duplicate file names.
Expand Down Expand Up @@ -238,9 +258,7 @@ protected function zip(Collection $photos, DownloadVariantType $downloadVariant)
);
} while (array_key_exists($filename, $uniqueFilenames));
}
$zip->addFileFromStream(fileName: $filename, stream: $archiveFileInfo->file->read(),
compressionMethod: $this->deflateLevel === -1 ? ZipMethod::STORE : ZipMethod::DEFLATE,
deflateLevel: $this->deflateLevel);
$this->addFileToZip($zip, $filename, $archiveFileInfo->file, null);
$archiveFileInfo->file->close();
// Reset the execution timeout for every iteration.
try {
Expand Down Expand Up @@ -274,6 +292,15 @@ protected function zip(Collection $photos, DownloadVariantType $downloadVariant)
return $response;
}

abstract protected function addFileToZip(ZipStream $zip, string $fileName, FlysystemFile|BaseMediaFile $file, Photo|null $photo): void;

/**
* @return ZipStream
*
* @throws ConfigurationKeyMissingException
*/
abstract protected function createZip(): ZipStream;

/**
* Creates a {@link ArchiveFileInfo} for the indicated photo and variant.
*
Expand Down
14 changes: 6 additions & 8 deletions app/Http/Controllers/Gallery/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace App\Http\Controllers\Gallery;

use App\Actions\Album\Archive as AlbumArchive;
use App\Actions\Album\BaseArchive as AlbumBaseArchive;
use App\Actions\Album\Create;
use App\Actions\Album\CreateTagAlbum;
use App\Actions\Album\Delete;
Expand All @@ -20,7 +20,7 @@
use App\Actions\Album\SetSmartProtectionPolicy;
use App\Actions\Album\Transfer;
use App\Actions\Album\Unlock;
use App\Actions\Photo\Archive as PhotoArchive;
use App\Actions\Photo\BaseArchive as PhotoBaseArchive;
use App\Events\AlbumRouteCacheUpdated;
use App\Exceptions\Internal\LycheeLogicException;
use App\Exceptions\UnauthenticatedException;
Expand Down Expand Up @@ -350,19 +350,17 @@ public function rename(RenameAlbumRequest $request): void
/**
* Return the archive of the pictures of the album and its sub-albums.
*
* @param ZipRequest $request
* @param AlbumArchive $album_archive
* @param PhotoArchive $photo_archive
* @param ZipRequest $request
*
* @return StreamedResponse
*/
public function getArchive(ZipRequest $request, AlbumArchive $album_archive, PhotoArchive $photo_archive): StreamedResponse
public function getArchive(ZipRequest $request): StreamedResponse
{
if ($request->albums()->count() > 0) {
return $album_archive->do($request->albums());
return AlbumBaseArchive::resolve()->do($request->albums());
}

return $photo_archive->do($request->photos(), $request->sizeVariant());
return PhotoBaseArchive::resolve()->do($request->photos(), $request->sizeVariant());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions app/Legacy/V1/Controllers/AlbumController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace App\Legacy\V1\Controllers;

use App\Actions\Album\Archive;
use App\Actions\Album\BaseArchive;
use App\Actions\Album\Create;
use App\Actions\Album\CreateTagAlbum;
use App\Actions\Album\Delete;
Expand Down Expand Up @@ -387,14 +388,13 @@ public function setSorting(SetAlbumSortingRequest $request): void
* Return the archive of the pictures of the album and its sub-albums.
*
* @param ArchiveAlbumsRequest $request
* @param Archive $archive
*
* @return StreamedResponse
*
* @throws LycheeException
*/
public function getArchive(ArchiveAlbumsRequest $request, Archive $archive): StreamedResponse
public function getArchive(ArchiveAlbumsRequest $request): StreamedResponse
{
return $archive->do($request->albums());
return BaseArchive::resolve()->do($request->albums());
}
}
6 changes: 3 additions & 3 deletions app/Legacy/V1/Controllers/PhotoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace App\Legacy\V1\Controllers;

use App\Actions\Photo\Archive;
use App\Actions\Photo\BaseArchive;
use App\Actions\Photo\Delete;
use App\Actions\Photo\Duplicate;
use App\Actions\Photo\Move;
Expand Down Expand Up @@ -313,15 +314,14 @@ public function duplicate(DuplicatePhotosRequest $request, Duplicate $duplicate)
* Return the archive of pictures or just a picture if only one.
*
* @param ArchivePhotosRequest $request
* @param Archive $archive
*
* @return SymfonyResponse
*
* @throws LycheeException
*/
public function getArchive(ArchivePhotosRequest $request, Archive $archive): SymfonyResponse
public function getArchive(ArchivePhotosRequest $request): SymfonyResponse
{
return $archive->do($request->photos(), $request->sizeVariant());
return BaseArchive::resolve()->do($request->photos(), $request->sizeVariant());
}

/**
Expand Down
Loading