chore(server): linting

This commit is contained in:
Roberto Tonino 2021-05-19 20:41:40 +02:00
parent 57f7f4d6db
commit 8574259115
7 changed files with 50 additions and 53 deletions

View File

@ -33,7 +33,7 @@ export function saveSettings(newSettings: any) {
} }
export let queueOrder: string[] = [] export let queueOrder: string[] = []
export let queue: any = {} export const queue: any = {}
export let currentJob: any = null export let currentJob: any = null
restoreQueueFromDisk() restoreQueueFromDisk()
@ -52,7 +52,8 @@ export async function addToQueue(dz: any, url: string, bitrate: number) {
downloadObjs.forEach((downloadObj: any) => { downloadObjs.forEach((downloadObj: any) => {
// Check if element is already in queue // Check if element is already in queue
if (Object.keys(queue).includes(downloadObj.uuid)) throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject) if (Object.keys(queue).includes(downloadObj.uuid))
throw new AlreadyInQueue(downloadObj.getEssentialDict(), !isSingleObject)
// Save queue status when adding something to the queue // Save queue status when adding something to the queue
if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue') if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue')
@ -60,10 +61,10 @@ export async function addToQueue(dz: any, url: string, bitrate: number) {
queueOrder.push(downloadObj.uuid) queueOrder.push(downloadObj.uuid)
fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder))
queue[downloadObj.uuid] = downloadObj.getEssentialDict() queue[downloadObj.uuid] = downloadObj.getEssentialDict()
queue[downloadObj.uuid].status = "inQueue" queue[downloadObj.uuid].status = 'inQueue'
let savedObject = downloadObj.toDict() const savedObject = downloadObj.toDict()
savedObject.status = "inQueue" savedObject.status = 'inQueue'
fs.writeFileSync(configFolder + `queue${sep}${downloadObj.uuid}.json`, JSON.stringify(savedObject)) fs.writeFileSync(configFolder + `queue${sep}${downloadObj.uuid}.json`, JSON.stringify(savedObject))
slimmedObjects.push(downloadObj.getSlimmedDict()) slimmedObjects.push(downloadObj.getSlimmedDict())
@ -83,9 +84,9 @@ async function startQueue(dz: any): Promise<any> {
} }
currentJob = true // lock currentJob currentJob = true // lock currentJob
const currentUUID: string = queueOrder.shift() || "" const currentUUID: string = queueOrder.shift() || ''
console.log(currentUUID) console.log(currentUUID)
queue[currentUUID].status = "downloading" queue[currentUUID].status = 'downloading'
const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${currentUUID}.json`).toString()) const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${currentUUID}.json`).toString())
let downloadObject: any let downloadObject: any
switch (currentItem.__type__) { switch (currentItem.__type__) {
@ -104,17 +105,17 @@ async function startQueue(dz: any): Promise<any> {
listener.send('startDownload', currentUUID) listener.send('startDownload', currentUUID)
await currentJob.start() await currentJob.start()
if (! downloadObject.isCanceled){ if (!downloadObject.isCanceled) {
// Set status // Set status
if (downloadObject.failed == downloadObject.size){ if (downloadObject.failed == downloadObject.size) {
queue[currentUUID].status = "failed" queue[currentUUID].status = 'failed'
} else if(downloadObject.failed > 0){ } else if (downloadObject.failed > 0) {
queue[currentUUID].status = "withErrors" queue[currentUUID].status = 'withErrors'
} else { } else {
queue[currentUUID].status = "completed" queue[currentUUID].status = 'completed'
} }
let savedObject = downloadObject.getSlimmedDict() const savedObject = downloadObject.getSlimmedDict()
savedObject.status = queue[currentUUID].status savedObject.status = queue[currentUUID].status
// Save queue status // Save queue status
@ -127,18 +128,20 @@ async function startQueue(dz: any): Promise<any> {
} while (queueOrder.length) } while (queueOrder.length)
} }
export function cancelDownload(uuid: string){ export function cancelDownload(uuid: string) {
if (Object.keys(queue).includes(uuid)){ if (Object.keys(queue).includes(uuid)) {
switch (queue[uuid].status) { switch (queue[uuid].status) {
case "downloading": case 'downloading':
currentJob.downloadObject.isCanceled = true currentJob.downloadObject.isCanceled = true
listener.send("cancellingCurrentItem", uuid) listener.send('cancellingCurrentItem', uuid)
break break
case "inQueue": case 'inQueue':
queueOrder.splice(queueOrder.indexOf(uuid), 1) queueOrder.splice(queueOrder.indexOf(uuid), 1)
fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder))
// break
default: default:
listener.send("removedFromQueue", uuid) // This gets called even in the 'inQueue' case. Is this the expected behaviour? If no, de-comment the break
listener.send('removedFromQueue', uuid)
break break
} }
fs.unlinkSync(configFolder + `queue${sep}${uuid}.json`) fs.unlinkSync(configFolder + `queue${sep}${uuid}.json`)
@ -146,41 +149,41 @@ export function cancelDownload(uuid: string){
} }
} }
export function cancelAllDownloads(){ export function cancelAllDownloads() {
queueOrder = [] queueOrder = []
let currentItem: string | null = null let currentItem: string | null = null
Object.values(queue).forEach((downloadObject: any) => { Object.values(queue).forEach((downloadObject: any) => {
if (downloadObject.status == "downloading"){ if (downloadObject.status == 'downloading') {
currentJob.downloadObject.isCanceled = true currentJob.downloadObject.isCanceled = true
listener.send("cancellingCurrentItem", downloadObject.uuid) listener.send('cancellingCurrentItem', downloadObject.uuid)
currentItem = downloadObject.uuid currentItem = downloadObject.uuid
} }
fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`) fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`)
delete queue[downloadObject.uuid] delete queue[downloadObject.uuid]
}) })
fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder)) fs.writeFileSync(configFolder + `queue${sep}order.json`, JSON.stringify(queueOrder))
listener.send("removedAllDownloads", currentItem) listener.send('removedAllDownloads', currentItem)
} }
export function clearCompletedDownloads(){ export function clearCompletedDownloads() {
Object.values(queue).forEach((downloadObject: any) => { Object.values(queue).forEach((downloadObject: any) => {
if (downloadObject.status === "completed"){ if (downloadObject.status === 'completed') {
fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`) fs.unlinkSync(configFolder + `queue${sep}${downloadObject.uuid}.json`)
delete queue[downloadObject.uuid] delete queue[downloadObject.uuid]
} }
}) })
listener.send("removedFinishedDownloads") listener.send('removedFinishedDownloads')
} }
export function restoreQueueFromDisk(){ export function restoreQueueFromDisk() {
if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue') if (!fs.existsSync(configFolder + 'queue')) fs.mkdirSync(configFolder + 'queue')
const allItems: string[] = fs.readdirSync(configFolder + 'queue') const allItems: string[] = fs.readdirSync(configFolder + 'queue')
allItems.forEach((filename: string) => { allItems.forEach((filename: string) => {
if (filename == 'order.json'){ if (filename == 'order.json') {
queueOrder = JSON.parse(fs.readFileSync(configFolder + `queue${sep}order.json`).toString()) queueOrder = JSON.parse(fs.readFileSync(configFolder + `queue${sep}order.json`).toString())
} else { } else {
const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${filename}`).toString()) const currentItem: any = JSON.parse(fs.readFileSync(configFolder + `queue${sep}${filename}`).toString())
if (currentItem.status === 'inQueue'){ if (currentItem.status === 'inQueue') {
let downloadObject: any let downloadObject: any
switch (currentItem.__type__) { switch (currentItem.__type__) {
case 'Single': case 'Single':
@ -194,7 +197,7 @@ export function restoreQueueFromDisk(){
break break
} }
queue[downloadObject.uuid] = downloadObject.getEssentialDict() queue[downloadObject.uuid] = downloadObject.getEssentialDict()
queue[downloadObject.uuid].status = "inQueue" queue[downloadObject.uuid].status = 'inQueue'
} else { } else {
queue[currentItem.uuid] = currentItem queue[currentItem.uuid] = currentItem
} }

View File

@ -1,19 +1,18 @@
// @ts-expect-error // import { Deezer } from 'deezer-js'
import { Deezer } from 'deezer-js'
import { ApiHandler } from '../../../types' import { ApiHandler } from '../../../types'
import { queueOrder, queue, currentJob } from '../../../main' import { queueOrder, queue, currentJob } from '../../../main'
const path: ApiHandler['path'] = '/getQueue' const path: ApiHandler['path'] = '/getQueue'
let homeCache: any // let homeCache: any
const handler: ApiHandler['handler'] = async (req, res) => { const handler: ApiHandler['handler'] = (req, res) => {
let result = { const result = {
queue, queue,
order: queueOrder, order: queueOrder,
currentItem: "", currentItem: ''
} }
if (currentJob) result.currentItem = currentJob.downloadObject.uuid if (currentJob) result.currentItem = currentJob.downloadObject.uuid
res.send(result) res.send(result)
} }

View File

@ -18,7 +18,7 @@ router.get('/connect', (req, res) => {
if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer() if (!sessionDZ[req.session.id]) sessionDZ[req.session.id] = new Deezer()
const dz = sessionDZ[req.session.id] const dz = sessionDZ[req.session.id]
let result: any = { const result: any = {
update: { update: {
currentCommit: 'testing', currentCommit: 'testing',
latestCommit: 'testing', latestCommit: 'testing',
@ -30,12 +30,12 @@ router.get('/connect', (req, res) => {
deezerNotAvailable: false deezerNotAvailable: false
} }
if (Object.keys(queue).length > 0){ if (Object.keys(queue).length > 0) {
result.queue = { result.queue = {
queue, queue,
queueOrder queueOrder
} }
if (currentJob && currentJob !== true){ if (currentJob && currentJob !== true) {
result.queue.current = currentJob.downloadObject.uuid result.queue.current = currentJob.downloadObject.uuid
} }
} }

View File

@ -5,7 +5,7 @@ import { cancelAllDownloads } from '../../main'
const eventName = 'cancelAllDownloads' const eventName = 'cancelAllDownloads'
const cb = (_: any, __: any, ___: WsServer) => { const cb = (_: any, __: any, ___: WsServer) => {
cancelAllDownloads() cancelAllDownloads()
consoleInfo(`Queue cleared`) consoleInfo(`Queue cleared`)
} }

View File

@ -3,9 +3,4 @@ import removeFinishedDownloads from './removeFinishedDownloads'
import removeFromQueue from './removeFromQueue' import removeFromQueue from './removeFromQueue'
import cancelAllDownloads from './cancelAllDownloads' import cancelAllDownloads from './cancelAllDownloads'
export default [ export default [saveSettings, removeFinishedDownloads, removeFromQueue, cancelAllDownloads]
saveSettings,
removeFinishedDownloads,
removeFromQueue,
cancelAllDownloads
]

View File

@ -5,7 +5,7 @@ import { clearCompletedDownloads } from '../../main'
const eventName = 'removeFinishedDownloads' const eventName = 'removeFinishedDownloads'
const cb = (_: any, __: any, ___: WsServer) => { const cb = (_: any, __: any, ___: WsServer) => {
clearCompletedDownloads() clearCompletedDownloads()
consoleInfo('Completed downloads cleared') consoleInfo('Completed downloads cleared')
} }

View File

@ -5,7 +5,7 @@ import { cancelDownload } from '../../main'
const eventName = 'removeFromQueue' const eventName = 'removeFromQueue'
const cb = (data: any, __: any, ___: WsServer) => { const cb = (data: any, __: any, ___: WsServer) => {
cancelDownload(data) cancelDownload(data)
consoleInfo(`Cancelled ${data}`) consoleInfo(`Cancelled ${data}`)
} }