added format parameter, removed 'hello worker' response

This commit is contained in:
uh wot 2021-01-25 09:29:09 +01:00
parent 49dc7869ac
commit 8b50a3682b
Signed by: uhwot
GPG Key ID: CB2454984587B781
2 changed files with 38 additions and 8 deletions

4
dist/worker.js vendored

File diff suppressed because one or more lines are too long

View File

@ -41,8 +41,17 @@ async function url_gen(md5_origin, format, id, media_version) {
return `https://e-cdns-proxy-${md5_origin[0]}.dzcdn.net/api/1/${result}`
}
const format_string_to_num = {
'64': '10',
'128': '1',
'320': '3',
'flac': '9',
'misc': '0',
}
async function track(request) {
const id = request.url.split('/')[4]
const url = new URL(request.url)
const id = url.pathname.split('/')[2]
if (id === "") {
return new Response("Track ID needs to be specified", {status: 400, headers: {'content-type': "text/plain"}})
@ -67,12 +76,35 @@ async function track(request) {
}
const md5_origin = json.md5_origin
const format = "3"
const media_version = json.media_version
const url = await url_gen(md5_origin, format, id, media_version)
format = url.searchParams.get('f')
if (format === null) {
if (!(id[0] === '-')) {
format = '320'
} else {
format = 'misc' // user-uploaded tracks
}
} else {
format = format.toLowerCase()
if (format_string_to_num[format] === undefined) {
index = Object.values(format_string_to_num).indexOf(format)
if (index === -1) {
return new Response("Invalid format", {status: 400, headers: {'content-type': "text/plain"}})
}
format = Object.keys(format_string_to_num)[index]
}
}
return new Response(null, {status: 302, headers: {'location': url}})
if (json['filesize_' + format] === '0') {
return new Response("Format unavailable", {status: 403, headers: {'content-type': "text/plain"}})
}
format = format_string_to_num[format]
const cdn_url = await url_gen(md5_origin, format, id, media_version)
return new Response(null, {status: 302, headers: {'location': cdn_url}})
}
async function handleRequest(request) {
@ -80,8 +112,6 @@ async function handleRequest(request) {
// Replace with the appropriate paths and handlers
r.get('/track/.*', () => track(request))
r.get('/', () => new Response('Hello worker!')) // return a default message for the root route
const resp = await r.route(request)
return resp
}