diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-16 12:51:51 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-28 13:18:29 +0200 |
commit | 2f66bd5b754f072a3cfeda759d71a479e4538350 (patch) | |
tree | 624960bf23548f5bf2362090c2142db657ffb82b | |
parent | d66e16b07efb5e2b315e36cfb01228b428660df1 (diff) | |
download | nextcloud-server-2f66bd5b754f072a3cfeda759d71a479e4538350.tar.gz nextcloud-server-2f66bd5b754f072a3cfeda759d71a479e4538350.zip |
fix: Allow `Streamer` to specify type in constructor instead of magin UA handling
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
-rw-r--r-- | lib/private/Streamer.php | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/private/Streamer.php b/lib/private/Streamer.php index 1f510f730ee..642e221ecc4 100644 --- a/lib/private/Streamer.php +++ b/lib/private/Streamer.php @@ -21,20 +21,30 @@ use ZipStreamer\ZipStreamer; class Streamer { // array of regexp. Matching user agents will get tar instead of zip - private array $preferTarFor = [ '/macintosh|mac os x/i' ]; + private const UA_PREFERS_TAR = [ '/macintosh|mac os x/i' ]; // streamer instance private $streamerInstance; + public static function isUserAgentPreferTar(IRequest $request): bool { + return $request->isUserAgent(self::UA_PREFERS_TAR); + } + /** * Streamer constructor. * - * @param IRequest $request + * @param bool|IRequest $preferTar If true a tar stream is used. + * For legacy reasons also a IRequest can be passed to detect this preference by user agent, + * please migrate to `Streamer::isUserAgentPreferTar()` instead. * @param int|float $size The size of the files in bytes * @param int $numberOfFiles The number of files (and directories) that will * be included in the streamed file */ - public function __construct(IRequest $request, int|float $size, int $numberOfFiles) { + public function __construct(IRequest|bool $preferTar, int|float $size, int $numberOfFiles) { + if ($preferTar instanceof IRequest) { + $preferTar = self::isUserAgentPreferTar($preferTar); + } + /** * zip32 constraints for a basic (without compression, volumes nor * encryption) zip file according to the Zip specification: @@ -61,10 +71,11 @@ class Streamer { * from not fully scanned external storage. And then things fall apart * if somebody tries to package to much. */ - if ($size > 0 && $size < 4 * 1000 * 1000 * 1000 && $numberOfFiles < 65536) { - $this->streamerInstance = new ZipStreamer(['zip64' => false]); - } elseif ($request->isUserAgent($this->preferTarFor)) { + if ($preferTar) { + // If TAR ball is preferred use it $this->streamerInstance = new TarStreamer(); + } elseif ($size > 0 && $size < 4 * 1000 * 1000 * 1000 && $numberOfFiles < 65536) { + $this->streamerInstance = new ZipStreamer(['zip64' => false]); } else { $this->streamerInstance = new ZipStreamer(['zip64' => PHP_INT_SIZE !== 4]); } @@ -84,6 +95,7 @@ class Streamer { /** * Stream directory recursively * + * @param string $dir Directory path relative to root of current user * @throws NotFoundException * @throws NotPermittedException * @throws InvalidPathException |