From c9b2100d85e91cb4d51b0e2f12c26b7354b4cc83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20=C5=BB=C3=B3=C5=82tak?= Date: Fri, 15 Nov 2024 11:00:01 +0100 Subject: [PATCH] /download change response mime to application/zip and allow URI ids --- src/acdhOeaw/arche/core/Download.php | 11 +++++++---- tests/DownloadTest.php | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/acdhOeaw/arche/core/Download.php b/src/acdhOeaw/arche/core/Download.php index cfa1bda..4f11091 100644 --- a/src/acdhOeaw/arche/core/Download.php +++ b/src/acdhOeaw/arche/core/Download.php @@ -40,6 +40,7 @@ */ class Download { + const CONTENT_TYPE = 'application/zip'; const DEFAULT_COMPRESSION_METHOD = CompressionMethod::STORE; const DEFAULT_COMPRESSION_LEVEL = 1; const DEFAULT_STRICT = false; @@ -113,7 +114,7 @@ public function get(): void { $this->parents = []; unset($this->parentQuery); unset($this->parentQueryParam); - $zip = new ZipStream(defaultCompressionMethod: $method, defaultDeflateLevel: $level, enableZip64: !$strict, defaultEnableZeroHeader: !$strict, outputName: $fileName); + $zip = new ZipStream(contentType: self::CONTENT_TYPE, defaultCompressionMethod: $method, defaultDeflateLevel: $level, enableZip64: !$strict, defaultEnableZeroHeader: !$strict, outputName: $fileName); foreach ($validIds as $id) { $binary = new BinaryPayload($id); $path = $binary->getPath(); @@ -178,10 +179,12 @@ private function fetchParentsMeta(int $id, object $meta): void { * @return array */ private function collectChildren(array $ids): array { - $query = RC::$pdo->prepare("SELECT id FROM get_relatives(?, ?, 999999, 0, false, false)"); - $param = [null, RC::$schema->parent]; + $baseUrl = RC::$config->rest->urlBase . RC::$config->rest->pathBase; + $query = RC::$pdo->prepare("SELECT (get_relatives(id, ?, 999999, 0, false, false)).id FROM identifiers WHERE ids = ?"); + $param = [RC::$schema->parent, null]; + $allIds = []; foreach ($ids as $id) { - $param[0] = $id; + $param[1] = is_numeric($id) ? $baseUrl . $id : $id; $query->execute($param); while ($i = $query->fetchColumn()) { $allIds[(string) $i] = ''; diff --git a/tests/DownloadTest.php b/tests/DownloadTest.php index efeb1ac..161100c 100644 --- a/tests/DownloadTest.php +++ b/tests/DownloadTest.php @@ -93,7 +93,7 @@ public function testCollection(): void { $this->commitTransaction($txId); // two arbitrary binaries using GET - $ids = [$binaries[2][1], $binaries[0][0]]; + $ids = [self::$baseUrl . $binaries[2][1], $binaries[0][0]]; $ids = array_map(fn($x) => preg_replace('`^.*/`', '', $x), $ids); $uri = self::$baseUrl . 'download?' . http_build_query(['ids' => $ids]); $req = new Request('get', $uri, ['eppn' => 'admin']); @@ -197,7 +197,7 @@ public function testAuth(): void { private function testZipBasics(Response $resp, int $expectedCount): array { $this->assertEquals(200, $resp->getStatusCode()); $this->assertEquals(["attachment; filename*=UTF-8''data.zip"], $resp->getHeader('Content-Disposition')); - $this->assertEquals(['application/x-zip'], $resp->getHeader('Content-Type')); + $this->assertEquals(['application/zip'], $resp->getHeader('Content-Type')); file_put_contents(self::TMP_ZIP, (string) $resp->getBody()); $zip = new ZipArchive(); $this->assertTrue($zip->open(self::TMP_ZIP));