enigma-bbs/core/download_queue.js

104 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-10-15 03:56:45 +00:00
/* jslint node: true */
'use strict';
2020-05-12 01:52:01 +00:00
const FileEntry = require('./file_entry');
const UserProps = require('./user_property');
const Events = require('./events');
2018-06-03 23:02:28 +00:00
// deps
2020-05-12 01:52:01 +00:00
const _ = 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)) {
2018-11-24 00:41:16 +00:00
if(this.client.user.properties[UserProps.DownloadQueue]) {
this.loadFromProperty(this.client.user.properties[UserProps.DownloadQueue]);
} else {
this.client.user.downloadQueue = [];
}
}
}
2016-10-15 03:56:45 +00:00
2020-05-12 01:52:01 +00:00
static get(client) {
return new DownloadQueue(client);
}
get items() {
return this.client.user.downloadQueue;
}
clear() {
this.client.user.downloadQueue = [];
}
toggle(fileEntry, systemFile=false) {
if(this.isQueued(fileEntry)) {
this.client.user.downloadQueue = this.client.user.downloadQueue.filter(e => fileEntry.fileId !== e.fileId);
} else {
this.add(fileEntry, systemFile);
}
}
2016-10-15 03:56:45 +00:00
add(fileEntry, systemFile=false) {
this.client.user.downloadQueue.push({
fileId : fileEntry.fileId,
areaTag : fileEntry.areaTag,
fileName : fileEntry.fileName,
path : fileEntry.filePath,
byteSize : fileEntry.meta.byte_size || 0,
systemFile : systemFile,
});
}
2016-10-15 03:56:45 +00:00
removeItems(fileIds) {
if(!Array.isArray(fileIds)) {
fileIds = [ fileIds ];
}
2020-05-12 01:52:01 +00:00
const [ remain, removed ] = _.partition(this.client.user.downloadQueue, e => ( -1 === fileIds.indexOf(e.fileId) ));
this.client.user.downloadQueue = remain;
return removed;
}
isQueued(entryOrId) {
if(entryOrId instanceof FileEntry) {
entryOrId = entryOrId.fileId;
}
2016-10-15 03:56:45 +00:00
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); }
loadFromProperty(prop) {
try {
this.client.user.downloadQueue = JSON.parse(prop);
} catch(e) {
this.client.user.downloadQueue = [];
this.client.log.error( { error : e.message, property : prop }, 'Failed parsing download queue property');
}
}
2020-05-12 01:52:01 +00:00
addTemporaryDownload(entry) {
this.add(entry, true); // true=systemFile
// clean up after ourselves when the session ends
const thisUniqueId = this.client.session.uniqueId;
2020-05-12 01:52:01 +00:00
Events.once(Events.getSystemEvents().ClientDisconnected, evt => {
if(thisUniqueId === _.get(evt, 'client.session.uniqueId')) {
2020-05-12 01:52:01 +00:00
FileEntry.removeEntry(entry, { removePhysFile : true }, err => {
const Log = require('./logger').log;
if(err) {
2020-05-12 02:31:35 +00:00
Log.warn( { fileId : entry.fileId, path : entry.filePath }, 'Failed removing temporary session download' );
2020-05-12 01:52:01 +00:00
} else {
2020-05-12 02:31:35 +00:00
Log.debug( { fileId : entry.fileId, path : entry.filePath }, 'Removed temporary session download item' );
2020-05-12 01:52:01 +00:00
}
});
}
});
}
2016-10-15 03:56:45 +00:00
};