added format parameter, removed 'hello worker' response
This commit is contained in:
parent
49dc7869ac
commit
8b50a3682b
File diff suppressed because one or more lines are too long
42
index.js
42
index.js
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue