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