From 12d6cafdd0a383453181b61f6dce4e426e149539 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Fri, 19 Aug 2022 23:49:53 +0200 Subject: [PATCH] Added can't stream error before adding to queue --- server/src/app.ts | 7 ++++++- server/src/helpers/errors.ts | 22 ++++++++++++++++++++++ server/src/routes/api/post/addToQueue.ts | 9 ++++++--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/server/src/app.ts b/server/src/app.ts index f04ba6b..0a5d3fc 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -5,7 +5,7 @@ import { v4 as uuidv4 } from 'uuid' import deemix from 'deemix' import got from 'got' import { Settings, Listener } from './types' -import { NotLoggedIn } from './helpers/errors' +import { NotLoggedIn, CantStream } from './helpers/errors' import { GUI_PACKAGE } from './helpers/paths' import { logger } from './helpers/logger' @@ -160,6 +160,11 @@ export class DeemixApp { async addToQueue(dz: any, url: string[], bitrate: number) { if (!dz.logged_in) throw new NotLoggedIn() + if ( + !this.settings.feelingLucky && + ((!dz.current_user.can_stream_lossless && bitrate === 9) || (!dz.current_user.can_stream_hq && bitrate === 3)) + ) + throw new CantStream(bitrate) let downloadObjs: any[] = [] const downloadErrors: any[] = [] diff --git a/server/src/helpers/errors.ts b/server/src/helpers/errors.ts index e737811..67bfa44 100644 --- a/server/src/helpers/errors.ts +++ b/server/src/helpers/errors.ts @@ -1,3 +1,16 @@ +const { TrackFormats } = require('deezer-js') + +const bitrateLabels = { + [TrackFormats.MP4_RA3]: '360 HQ', + [TrackFormats.MP4_RA2]: '360 MQ', + [TrackFormats.MP4_RA1]: '360 LQ', + [TrackFormats.FLAC]: 'FLAC', + [TrackFormats.MP3_320]: '320kbps', + [TrackFormats.MP3_128]: '128kbps', + [TrackFormats.DEFAULT]: '128kbps', + [TrackFormats.LOCAL]: 'MP3' +} + export class BadRequestError extends Error { constructor() { super() @@ -31,3 +44,12 @@ export class NotLoggedIn extends QueueError { this.name = 'NotLoggedIn' } } + +export class CantStream extends QueueError { + bitrate: number + constructor(bitrate: number) { + super(`Your account can't stream at ${bitrateLabels[bitrate]}.`) + this.name = 'CantStream' + this.bitrate = bitrate + } +} diff --git a/server/src/routes/api/post/addToQueue.ts b/server/src/routes/api/post/addToQueue.ts index 6060874..d69cca0 100644 --- a/server/src/routes/api/post/addToQueue.ts +++ b/server/src/routes/api/post/addToQueue.ts @@ -14,19 +14,22 @@ const handler: ApiHandler['handler'] = async (req, res) => { const url = req.body.url.split(/[\s;]+/) let bitrate = req.body.bitrate if (bitrate === 'null' || bitrate === null) bitrate = deemix.getSettings().settings.maxBitrate + bitrate = Number(bitrate) let obj: any try { obj = await deemix.addToQueue(dz, url, bitrate) } catch (e) { + res.send({ result: false, errid: e.name, data: { url, bitrate } }) switch (e.name) { case 'NotLoggedIn': - res.send({ result: false, errid: e.name, data: { url, bitrate } }) - deemix.listener.send('loginNeededToDownload') + deemix.listener.send('queueError' + e.name) + break + case 'CantStream': + deemix.listener.send('queueError' + e.name, e.bitrate) break default: logger.error(e) - res.send({ result: false, errid: e.name, data: { url, bitrate } }) break } return