deemixer/server/src/routes/api/post/addToQueue.ts
2022-01-13 01:27:08 +01:00

39 lines
1.0 KiB
TypeScript

// @ts-expect-error
import { Deezer } from 'deezer-js'
import { ApiHandler } from '../../../types'
import { sessionDZ, addToQueue, getSettings, listener } from '../../../main'
const path: ApiHandler['path'] = '/addToQueue'
const handler: ApiHandler['handler'] = async (req, res) => {
if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer()
const dz = sessionDZ[req.session.id]
const url = req.body.url.split(/[\s;]+/)
let bitrate = req.body.bitrate
if (bitrate === 'null') bitrate = getSettings().settings.maxBitrate
let obj: any
try {
obj = await addToQueue(dz, url, bitrate)
} catch (e) {
switch (e.name) {
case 'NotLoggedIn':
res.send({ result: false, errid: e.name, data: { url, bitrate } })
listener.send('loginNeededToDownload')
break
default:
console.error(e)
res.send({ result: false, errid: e.name, data: { url, bitrate } })
break
}
return
}
res.send({ result: true, data: { url, bitrate, obj } })
}
const apiHandler: ApiHandler = { path, handler }
export default apiHandler