From fa6826c5543111e1015d1573a2512442d4cbb7b2 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Sun, 16 May 2021 20:11:48 +0200 Subject: [PATCH] Implemented removeFromQueue and cancelAllDownloads --- server/package.json | 2 +- server/src/main.ts | 67 +++++++++++++++---- server/src/routes/index.ts | 18 ++++- .../websocket/modules/cancelAllDownloads.ts | 12 ++++ server/src/websocket/modules/index.ts | 6 +- .../src/websocket/modules/removeFromQueue.ts | 12 ++++ server/yarn.lock | 8 +-- 7 files changed, 102 insertions(+), 23 deletions(-) diff --git a/server/package.json b/server/package.json index 9a12347..753df68 100644 --- a/server/package.json +++ b/server/package.json @@ -15,7 +15,7 @@ "dependencies": { "cookie-parser": "1.4.5", "debug": "2.6.9", - "deemix": "0.0.3", + "deemix": "0.0.4", "deezer-js": "0.0.10", "dotenv": "8.2.0", "express": "4.17.1", diff --git a/server/src/main.ts b/server/src/main.ts index 99901fa..e42aed0 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -84,6 +84,8 @@ async function startQueue(dz: any): Promise { currentJob = true // lock currentJob const currentUUID: string = queueOrder.shift() || "" + console.log(currentUUID) + queue[currentUUID].status = "downloading" const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${currentUUID}.json`).toString()) let downloadObject: any switch (currentItem.__type__) { @@ -100,29 +102,66 @@ async function startQueue(dz: any): Promise { } currentJob = new Downloader(dz, downloadObject, settings, listener) listener.send('startDownload', currentUUID) - queue[currentUUID].status = "downloading" await currentJob.start() - // Set status - if (downloadObject.failed == downloadObject.size){ - queue[currentUUID].status = "failed" - } else if(downloadObject.failed > 0){ - queue[currentUUID].status = "withErrors" - } else { - queue[currentUUID].status = "completed" + if (! downloadObject.isCanceled){ + // Set status + if (downloadObject.failed == downloadObject.size){ + queue[currentUUID].status = "failed" + } else if(downloadObject.failed > 0){ + queue[currentUUID].status = "withErrors" + } else { + queue[currentUUID].status = "completed" + } + + let savedObject = downloadObject.getSlimmedDict() + savedObject.status = queue[currentUUID].status + + // Save queue status + fs.writeFileSync(configFolder + `queue${sep}${currentUUID}.json`, JSON.stringify(savedObject)) } - - let savedObject = downloadObject.getSlimmedDict() - savedObject.status = queue[currentUUID].status - - // Save queue status - fs.writeFileSync(configFolder + `queue${sep}${currentUUID}.json`, JSON.stringify(savedObject)) + console.log(queueOrder) fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) currentJob = null } while (queueOrder.length) } +export function cancelDownload(uuid: string){ + if (Object.keys(queue).includes(uuid)){ + switch (queue[uuid].status) { + case "downloading": + currentJob.downloadObject.isCanceled = true + listener.send("cancellingCurrentItem", uuid) + break + case "inQueue": + queueOrder.splice(queueOrder.indexOf(uuid), 1) + fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) + default: + listener.send("removedFromQueue", uuid) + break + } + fs.unlinkSync(configFolder + `queue${sep}${uuid}.json`) + delete queue[uuid] + } +} + +export function cancelAllDownloads(){ + queueOrder = [] + let currentItem: string | null = null + Object.values(queue).forEach((downloadObject: any) => { + if (downloadObject.status == "downloading"){ + currentJob.downloadObject.isCanceled = true + listener.send("cancellingCurrentItem", downloadObject.uuid) + currentItem = downloadObject.uuid + } + fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`) + delete queue[downloadObject.uuid] + }) + fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) + listener.send("removedAllDownloads", currentItem) +} + export function clearCompletedDownloads(){ Object.values(queue).forEach((downloadObject: any) => { if (downloadObject.status === "completed"){ diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index d474a22..f8a022f 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -1,7 +1,7 @@ import express from 'express' // @ts-expect-error import { Deezer } from 'deezer-js' -import { sessionDZ } from '../main' +import { sessionDZ, queue, queueOrder, currentJob } from '../main' const router = express.Router() @@ -18,7 +18,7 @@ router.get('/connect', (req, res) => { if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() const dz = sessionDZ[req.session.id] - res.send({ + let result: any = { update: { currentCommit: 'testing', latestCommit: 'testing', @@ -28,7 +28,19 @@ router.get('/connect', (req, res) => { autologin: !dz.logged_in, currentUser: dz.current_user, deezerNotAvailable: false - }) + } + + if (Object.keys(queue).length > 0){ + result.queue = { + queue, + queueOrder + } + if (currentJob && currentJob !== true){ + result.queue.current = currentJob.downloadObject.uuid + } + } + + res.send(result) }) export default router diff --git a/server/src/websocket/modules/cancelAllDownloads.ts b/server/src/websocket/modules/cancelAllDownloads.ts index e69de29..f8753cb 100644 --- a/server/src/websocket/modules/cancelAllDownloads.ts +++ b/server/src/websocket/modules/cancelAllDownloads.ts @@ -0,0 +1,12 @@ +import { Server as WsServer } from 'ws' +import { consoleInfo } from '../../helpers/errors' +import { cancelAllDownloads } from '../../main' + +const eventName = 'cancelAllDownloads' + +const cb = (_: any, __: any, ___: WsServer) => { + cancelAllDownloads() + consoleInfo(`Queue cleared`) +} + +export default { eventName, cb } diff --git a/server/src/websocket/modules/index.ts b/server/src/websocket/modules/index.ts index e68634b..51f1393 100644 --- a/server/src/websocket/modules/index.ts +++ b/server/src/websocket/modules/index.ts @@ -1,7 +1,11 @@ import saveSettings from './saveSettings' import removeFinishedDownloads from './removeFinishedDownloads' +import removeFromQueue from './removeFromQueue' +import cancelAllDownloads from './cancelAllDownloads' export default [ saveSettings, - removeFinishedDownloads + removeFinishedDownloads, + removeFromQueue, + cancelAllDownloads ] diff --git a/server/src/websocket/modules/removeFromQueue.ts b/server/src/websocket/modules/removeFromQueue.ts index e69de29..67b6026 100644 --- a/server/src/websocket/modules/removeFromQueue.ts +++ b/server/src/websocket/modules/removeFromQueue.ts @@ -0,0 +1,12 @@ +import { Server as WsServer } from 'ws' +import { consoleInfo } from '../../helpers/errors' +import { cancelDownload } from '../../main' + +const eventName = 'removeFromQueue' + +const cb = (data: any, __: any, ___: WsServer) => { + cancelDownload(data) + consoleInfo(`Cancelled ${data}`) +} + +export default { eventName, cb } diff --git a/server/yarn.lock b/server/yarn.lock index 73f3306..e18cffd 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -1878,10 +1878,10 @@ decompress-response@^6.0.0: dependencies: mimic-response "^3.1.0" -deemix@0.0.3: - version "0.0.3" - resolved "https://registry.yarnpkg.com/deemix/-/deemix-0.0.3.tgz#a32b1b3c8e99fe26ea7a326658e49b4892c96dd5" - integrity sha512-cUsfliHRXWLOYB7K+UCN0Lmx3d4ueWfaQ/Xinhg8/cHetifJC4/CxD8YDdBArI7Fl3StkJVGt45BajU/z00k6Q== +deemix@0.0.4: + version "0.0.4" + resolved "https://registry.yarnpkg.com/deemix/-/deemix-0.0.4.tgz#20077d643d6b87675b95d647315164a5852176a2" + integrity sha512-d4WfVhfgiH5Zjxs6tNWyrMRd/ea7wLc3zlyP5TAVmK6HuAjvicLPNCjFxCMOrufuZVPRxjv4EtKiMpB7U+xDdQ== dependencies: async "^3.2.0" browser-id3-writer "^4.4.0"