From 7c86834d28f3bee52b3bcd77aec6c17cac5e72d5 Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Fri, 14 Jul 2023 22:07:20 -0600 Subject: [PATCH] feat: allow getting from either S3 or local --- backend/routers/index.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/routers/index.ts b/backend/routers/index.ts index a047a4a..7b88e28 100644 --- a/backend/routers/index.ts +++ b/backend/routers/index.ts @@ -104,20 +104,26 @@ router.get('/direct/:fakeId', async (req, res) => { else { // Get file metadata - const meta = files.get(fakeId); + const meta = files.get(fakeId)!; + + // File data can come from either S3 or local filesystem + let output: Readable | NodeJS.ReadableStream; // Try to retrieve the file - const file = await getFileS3(meta!.fileKey); - if (!file.Body) return res.status(500).send('Unknown error'); + if (!!meta.save.s3) { + const file = await getFileS3(meta.fileKey); + if (!file.Body) return res.status(500).send('Unknown error'); + output = file.Body as Readable; + } else output = fs.createReadStream(meta.save.local!); // Configure response headers - res.type(meta!.mimetype) - .header('Content-Disposition', `inline; filename="${meta!.filename}"`) + res.type(meta.mimetype) + .header('Content-Disposition', `inline; filename="${meta.filename}"`) .header('Cache-Control', 'public, max-age=31536000, immutable') .header('Accept-Ranges', 'bytes'); // Send the file (thanks to https://stackoverflow.com/a/67373050) - (file.Body as Readable).pipe(res); + output.pipe(res); } });