diff --git a/src/app/api/lightbox/bulk-download/route.ts b/src/app/api/lightbox/bulk-download/route.ts index c1f352a..9736b82 100644 --- a/src/app/api/lightbox/bulk-download/route.ts +++ b/src/app/api/lightbox/bulk-download/route.ts @@ -7,7 +7,7 @@ import { albumGrantService } from "@/modules/albums/grants"; // eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any const archiver: any = require("archiver"); import * as crypto from "crypto"; -import { Readable } from "stream"; +import { Readable, PassThrough } from "stream"; export async function POST(request: NextRequest) { try { @@ -90,15 +90,9 @@ export async function POST(request: NextRequest) { return new Response("No valid assets found for download", { status: 404 }); } - // Create a streaming response with archiver - const archive = archiver('zip', { zlib: { level: 0 } }); // No compression, images are already compressed - - // We must return a ReadableStream to Next.js - const transformStream = new TransformStream(); - - // Pipe archiver output to the writable side of the TransformStream - const writableNodeStream = new WritableStreamToNodeStream(transformStream.writable); - archive.pipe(writableNodeStream); + const passThrough = new PassThrough(); + const archive = archiver('zip', { zlib: { level: 0 } }); + archive.pipe(passThrough); // Start appending files asynchronously (async () => { @@ -111,22 +105,24 @@ export async function POST(request: NextRequest) { try { const original = await immichClient.getOriginal(download.assetId); - // Track bytes read for accounting - let transferredBytes = 0; - const trackingTransform = new TransformStream({ - transform(chunk, controller) { + await new Promise((resolve, reject) => { + const nodeStream = Readable.fromWeb(original.body as any); + let transferredBytes = 0; + + nodeStream.on('data', (chunk) => { transferredBytes += chunk.length; - controller.enqueue(chunk); - }, - async flush() { - await downloadDeliveryRepository.markCompleted(download.downloadId, transferredBytes); - } + }); + + nodeStream.on('end', () => { + downloadDeliveryRepository.markCompleted(download.downloadId, transferredBytes) + .then(() => resolve()) + .catch(reject); + }); + + nodeStream.on('error', (err) => reject(err)); + + archive.append(nodeStream, { name: download.filename }); }); - - const trackedStream = original.body.pipeThrough(trackingTransform); - const nodeStream = Readable.fromWeb(trackedStream as any); - - archive.append(nodeStream, { name: download.filename }); } catch (err) { console.error("Failed to append original for asset", download.assetId, err); await downloadDeliveryRepository.failBeforeDelivery(download.downloadId, { @@ -143,7 +139,7 @@ export async function POST(request: NextRequest) { })(); const date = new Date().toISOString().split('T')[0]; - return new Response(transformStream.readable, { + return new Response(Readable.toWeb(passThrough) as any, { headers: { "Content-Type": "application/zip", "Content-Disposition": `attachment; filename="leuchtkasten-${date}.zip"`, @@ -155,22 +151,3 @@ export async function POST(request: NextRequest) { return new Response("Internal Server Error", { status: 500 }); } } - -// Helper to convert Web WritableStream to Node Writable Stream -import { Writable } from "stream"; -class WritableStreamToNodeStream extends Writable { - private writer: WritableStreamDefaultWriter; - - constructor(writableStream: WritableStream) { - super(); - this.writer = writableStream.getWriter(); - } - - _write(chunk: any, encoding: string, callback: (error?: Error | null) => void) { - this.writer.write(chunk).then(() => callback()).catch(callback); - } - - _final(callback: (error?: Error | null) => void) { - this.writer.close().then(() => callback()).catch(callback); - } -}