From 64b5567cf2a9cdaa70215c037f0702e1ae6a9111 Mon Sep 17 00:00:00 2001 From: Tilman Date: Wed, 3 Dec 2025 00:17:32 +0100 Subject: [PATCH] Fix linting, formatting --- routes/api/files/download-directory.tsx | 77 +++++++++++++------------ 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/routes/api/files/download-directory.tsx b/routes/api/files/download-directory.tsx index 8411c24..0185dea 100644 --- a/routes/api/files/download-directory.tsx +++ b/routes/api/files/download-directory.tsx @@ -1,5 +1,6 @@ import { Handlers } from 'fresh/server.ts'; import { join } from '@std/path'; +import { Buffer } from 'node:buffer'; import JSZip from 'jszip'; import { FreshContextState } from '/lib/types.ts'; @@ -8,6 +9,42 @@ import { ensureUserPathIsValidAndSecurelyAccessible } from '/lib/models/files.ts interface Data {} +// Recursively add files to the zip with optimizations +async function addFilesToZip(currentPath: string, zipFolder: JSZip, basePath = '') { + const entries = []; + for await (const entry of Deno.readDir(currentPath)) { + entries.push(entry); + } + + // Process files in parallel batches + const batchSize = 10; + for (let i = 0; i < entries.length; i += batchSize) { + const batch = entries.slice(i, i + batchSize); + + await Promise.all( + batch.map(async (entry) => { + const entryPath = join(currentPath, entry.name); + const relativePath = basePath ? `${basePath}/${entry.name}` : entry.name; + + if (entry.isFile) { + // Use async file reading with streaming hint + const fileContent = await Deno.readFile(entryPath); + zipFolder.file(entry.name, fileContent, { + binary: true, + createFolders: false, + }); + } else if (entry.isDirectory) { + // Create a folder in the zip and recursively add its contents + const subFolder = zipFolder.folder(entry.name); + if (subFolder) { + await addFilesToZip(entryPath, subFolder, relativePath); + } + } + }), + ); + } +} + export const handler: Handlers = { async GET(request, context) { if (!context.state.user) { @@ -43,42 +80,6 @@ export const handler: Handlers = { // Create a JSZip instance const zip = new JSZip(); - // Recursively add files to the zip with optimizations - async function addFilesToZip(currentPath: string, zipFolder: JSZip, basePath = '') { - const entries = []; - for await (const entry of Deno.readDir(currentPath)) { - entries.push(entry); - } - - // Process files in parallel batches - const batchSize = 10; - for (let i = 0; i < entries.length; i += batchSize) { - const batch = entries.slice(i, i + batchSize); - - await Promise.all( - batch.map(async (entry) => { - const entryPath = join(currentPath, entry.name); - const relativePath = basePath ? `${basePath}/${entry.name}` : entry.name; - - if (entry.isFile) { - // Use async file reading with streaming hint - const fileContent = await Deno.readFile(entryPath); - zipFolder.file(entry.name, fileContent, { - binary: true, - createFolders: false, - }); - } else if (entry.isDirectory) { - // Create a folder in the zip and recursively add its contents - const subFolder = zipFolder.folder(entry.name); - if (subFolder) { - await addFilesToZip(entryPath, subFolder, relativePath); - } - } - }) - ); - } - } - // Add all files from the directory await addFilesToZip(fullDirectoryPath, zip); @@ -87,14 +88,14 @@ export const handler: Handlers = { type: 'nodebuffer', streamFiles: true, compression: 'DEFLATE', - compressionOptions: { + compressionOptions: { level: 1, }, }); // Convert Node.js stream to Web ReadableStream const readableStream = new ReadableStream({ - async start(controller) { + start(controller) { let isCancelled = false; zipStream.on('data', (chunk: Buffer) => {