From 4bef255fe01b461076b8e404008fb657d45eb3a1 Mon Sep 17 00:00:00 2001 From: uh wot Date: Sun, 2 Jul 2023 02:17:35 +0200 Subject: [PATCH] hopefully fixed filename preview on telegram --- src/main.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index dfb6892..2a9e8c6 100644 --- a/src/main.js +++ b/src/main.js @@ -59,7 +59,7 @@ async function track_req(request, env) { switch (type) { case 'track': - return await track(env, id, format, tagging, request.headers.get('range')) + return await track(env, id, format, tagging, request.headers.get('range'), request.headers.get('user-agent')) case 'album': case 'playlist': return await m3u8(type, id, format, tagging, request.headers.get('host')) @@ -148,7 +148,7 @@ async function auth_dzapi(env) { } } -async function track(env, id, format, tagging, range_header) { +async function track(env, id, format, tagging, range_header, user_agent) { // other users' user-upped tracks cannot be downloaded with the gw-light API let json; if (id >= 0) { @@ -258,13 +258,19 @@ async function track(env, id, format, tagging, range_header) { if (json.VERSION) title += ` ${json.VERSION}` const filename = `${title} [${id}].${formats[format].ext}` + let content_disposition + if (user_agent === null || !user_agent.startsWith('TelegramBot')) { + content_disposition = `inline; filename*=UTF-8''${encodeRFC5987ValueChars(filename)}` + } else { + content_disposition = `inline; filename="${filename.replace(/['\\]/g, (c) => `\\${c}`)}"` + } let init = { method: 'GET', headers: {} } let resp_init = { status: 200, headers: { 'content-type': formats[format].mime, - 'content-disposition': `inline; filename*=UTF-8''${encodeRFC5987ValueChars(filename)}`, + 'content-disposition': content_disposition, 'accept-ranges': 'bytes' } }