From 9ea290685ec179fd8bf39c66ff6704fa4e7f7754 Mon Sep 17 00:00:00 2001 From: daniel-j Date: Wed, 6 Dec 2023 16:50:19 +0100 Subject: [PATCH] filename sanitation when downloading to device --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index d2adf2e..7249fab 100644 --- a/index.js +++ b/index.js @@ -155,9 +155,17 @@ router.get('/download/:key', async ctx => { return } expireKey(key) - console.log('Sending file', info.file.path) + // const fallback = basename(info.file.path) + const sanename = info.file.name.replace(/[^\.\w\-"']/g, '_') + console.log('Sending file', [info.file.path, info.file.name, sanename]) await sendfile(ctx, info.file.path) - ctx.attachment(info.file.name) + if (info.agent.includes('Kindle')) { + // Kindle needs a safe name or it thinks it's an invalid file + ctx.attachment(sanename) + } else { + // Kobo always uses fallback + ctx.attachment(info.file.name, {fallback: sanename}) + } }) router.post('/upload', upload.single('file'), async ctx => {