Implemented removeFromQueue and cancelAllDownloads

This commit is contained in:
RemixDev 2021-05-16 20:11:48 +02:00
parent de85a39718
commit fa6826c554
7 changed files with 102 additions and 23 deletions

View File

@ -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",

View File

@ -84,6 +84,8 @@ async function startQueue(dz: any): Promise<any> {
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,9 +102,9 @@ async function startQueue(dz: any): Promise<any> {
}
currentJob = new Downloader(dz, downloadObject, settings, listener)
listener.send('startDownload', currentUUID)
queue[currentUUID].status = "downloading"
await currentJob.start()
if (! downloadObject.isCanceled){
// Set status
if (downloadObject.failed == downloadObject.size){
queue[currentUUID].status = "failed"
@ -117,12 +119,49 @@ async function startQueue(dz: any): Promise<any> {
// 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"){

View File

@ -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

View File

@ -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 }

View File

@ -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
]

View File

@ -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 }

View File

@ -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"