hopefully fixed filename preview on telegram

This commit is contained in:
uh wot 2023-07-02 02:17:35 +02:00
parent 7e1afcb1b1
commit 4bef255fe0
Signed by: uhwot
GPG Key ID: CB2454984587B781
1 changed files with 9 additions and 3 deletions

View File

@ -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'
}
}