enigma-bbs/core/download_queue.js

79 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-10-15 03:56:45 +00:00
/* jslint node: true */
'use strict';
2018-06-03 23:02:28 +00:00
const FileEntry = require('./file_entry.js');
// deps
const { partition } = require('lodash');
2016-10-15 03:56:45 +00:00
module.exports = class DownloadQueue {
constructor(client) {
this.client = client;
2016-10-15 03:56:45 +00:00
if(!Array.isArray(this.client.user.downloadQueue)) {
if(this.client.user.properties.dl_queue) {
this.loadFromProperty(this.client.user.properties.dl_queue);
} else {
this.client.user.downloadQueue = [];
}
}
2016-10-15 03:56:45 +00:00
}
get items() {
return this.client.user.downloadQueue;
}
clear() {
this.client.user.downloadQueue = [];
}
2018-06-03 23:02:28 +00:00
toggle(fileEntry, systemFile=false) {
2016-10-15 03:56:45 +00:00
if(this.isQueued(fileEntry)) {
this.client.user.downloadQueue = this.client.user.downloadQueue.filter(e => fileEntry.fileId !== e.fileId);
2016-10-15 03:56:45 +00:00
} else {
2018-06-03 23:02:28 +00:00
this.add(fileEntry, systemFile);
2016-10-15 03:56:45 +00:00
}
}
2018-06-03 23:02:28 +00:00
add(fileEntry, systemFile=false) {
this.client.user.downloadQueue.push({
2016-10-15 03:56:45 +00:00
fileId : fileEntry.fileId,
areaTag : fileEntry.areaTag,
fileName : fileEntry.fileName,
path : fileEntry.filePath,
byteSize : fileEntry.meta.byte_size || 0,
2018-06-03 23:02:28 +00:00
systemFile : systemFile,
2016-10-15 03:56:45 +00:00
});
}
removeItems(fileIds) {
if(!Array.isArray(fileIds)) {
fileIds = [ fileIds ];
}
2018-06-03 23:02:28 +00:00
const [ remain, removed ] = partition(this.client.user.downloadQueue, e => ( -1 === fileIds.indexOf(e.fileId) ));
this.client.user.downloadQueue = remain;
return removed;
}
2016-10-15 03:56:45 +00:00
isQueued(entryOrId) {
if(entryOrId instanceof FileEntry) {
entryOrId = entryOrId.fileId;
}
return this.client.user.downloadQueue.find(e => entryOrId === e.fileId) ? true : false;
2016-10-15 03:56:45 +00:00
}
toProperty() { return JSON.stringify(this.client.user.downloadQueue); }
2016-10-15 03:56:45 +00:00
loadFromProperty(prop) {
try {
this.client.user.downloadQueue = JSON.parse(prop);
2016-10-15 03:56:45 +00:00
} catch(e) {
this.client.user.downloadQueue = [];
this.client.log.error( { error : e.message, property : prop }, 'Failed parsing download queue property');
2016-10-15 03:56:45 +00:00
}
}
2016-10-15 03:56:45 +00:00
};