Skip to content

Commit

Permalink
/download change response mime to application/zip and allow URI ids
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Nov 15, 2024
1 parent 737e25b commit c9b2100
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/acdhOeaw/arche/core/Download.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -178,10 +179,12 @@ private function fetchParentsMeta(int $id, object $meta): void {
* @return array<string>
*/
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] = '';
Expand Down
4 changes: 2 additions & 2 deletions tests/DownloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit c9b2100

Please sign in to comment.