2017-09-26 16:44:15 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const DownloadQueue = require('./download_queue.js');
|
|
|
|
const theme = require('./theme.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const FileAreaWeb = require('./file_area_web.js');
|
|
|
|
const ErrNotEnabled = require('./enig_error.js').ErrorReasons.NotEnabled;
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2017-09-26 16:44:15 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'File Base Download Web Queue Manager',
|
|
|
|
desc : 'Module for interacting with web backed download queue/batch',
|
|
|
|
author : 'NuSkooler',
|
2017-09-26 16:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const FormIds = {
|
2018-06-23 03:26:46 +00:00
|
|
|
queueManager : 0
|
2017-09-26 16:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
2018-06-22 05:15:04 +00:00
|
|
|
queueManager : {
|
2018-06-23 03:26:46 +00:00
|
|
|
queue : 1,
|
|
|
|
navMenu : 2,
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
customRangeStart : 10,
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-09-26 16:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class FileBaseWebDownloadQueueManager extends MenuModule {
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.dlQueue = new DownloadQueue(this.client);
|
|
|
|
|
|
|
|
this.menuMethods = {
|
|
|
|
removeItem : (formData, extraArgs, cb) => {
|
|
|
|
const selectedItem = this.dlQueue.items[formData.value.queueItem];
|
|
|
|
if(!selectedItem) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dlQueue.removeItems(selectedItem.fileId);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: broken: does not redraw menu properly - needs fixed!
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.removeItemsFromDownloadQueueView(formData.value.queueItem, cb);
|
|
|
|
},
|
|
|
|
clearQueue : (formData, extraArgs, cb) => {
|
|
|
|
this.dlQueue.clear();
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: broken: does not redraw menu properly - needs fixed!
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.removeItemsFromDownloadQueueView('all', cb);
|
|
|
|
},
|
|
|
|
getBatchLink : (formData, extraArgs, cb) => {
|
|
|
|
return this.generateAndDisplayBatchLink(cb);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
if(0 === this.dlQueue.items.length) {
|
|
|
|
return this.gotoMenu(this.menuConfig.config.emptyQueueMenu || 'fileBaseDownloadManagerEmptyQueue');
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function beforeArt(callback) {
|
|
|
|
return self.beforeArt(callback);
|
|
|
|
},
|
|
|
|
function display(callback) {
|
|
|
|
return self.displayQueueManagerPage(false, callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
return self.finishedLoading();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeItemsFromDownloadQueueView(itemIndex, cb) {
|
|
|
|
const queueView = this.viewControllers.queueManager.getView(MciViewIds.queueManager.queue);
|
|
|
|
if(!queueView) {
|
|
|
|
return cb(Errors.DoesNotExist('Queue view does not exist'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if('all' === itemIndex) {
|
|
|
|
queueView.setItems([]);
|
|
|
|
queueView.setFocusItems([]);
|
|
|
|
} else {
|
|
|
|
queueView.removeItem(itemIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
queueView.redraw();
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayFileInfoForFileEntry(fileEntry) {
|
|
|
|
this.updateCustomViewTextsWithFilter(
|
|
|
|
'queueManager',
|
|
|
|
MciViewIds.queueManager.customRangeStart, fileEntry,
|
2018-06-23 03:26:46 +00:00
|
|
|
{ filter : [ '{webDlLink}', '{webDlExpire}', '{fileName}' ] } // :TODO: Others....
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDownloadQueueView(cb) {
|
|
|
|
const queueView = this.viewControllers.queueManager.getView(MciViewIds.queueManager.queue);
|
|
|
|
if(!queueView) {
|
|
|
|
return cb(Errors.DoesNotExist('Queue view does not exist'));
|
|
|
|
}
|
|
|
|
|
2018-11-18 19:02:57 +00:00
|
|
|
queueView.setItems(this.dlQueue.items);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
queueView.on('index update', idx => {
|
|
|
|
const fileEntry = this.dlQueue.items[idx];
|
|
|
|
this.displayFileInfoForFileEntry(fileEntry);
|
|
|
|
});
|
|
|
|
|
|
|
|
queueView.redraw();
|
|
|
|
this.displayFileInfoForFileEntry(this.dlQueue.items[0]);
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
generateAndDisplayBatchLink(cb) {
|
|
|
|
const expireTime = moment().add(Config().fileBase.web.expireMinutes, 'minutes');
|
|
|
|
|
|
|
|
FileAreaWeb.createAndServeTempBatchDownload(
|
|
|
|
this.client,
|
|
|
|
this.dlQueue.items,
|
|
|
|
{
|
|
|
|
expireTime : expireTime
|
|
|
|
},
|
|
|
|
(err, webBatchDlLink) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: handle not enabled -> display such
|
2018-06-22 05:15:04 +00:00
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const webDlExpireTimeFormat = this.menuConfig.config.webDlExpireTimeFormat || 'YYYY-MMM-DD @ h:mm';
|
|
|
|
|
|
|
|
const formatObj = {
|
2018-06-23 03:26:46 +00:00
|
|
|
webBatchDlLink : ansi.vtxHyperlink(this.client, webBatchDlLink) + webBatchDlLink,
|
|
|
|
webBatchDlExpire : expireTime.format(webDlExpireTimeFormat),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.updateCustomViewTextsWithFilter(
|
|
|
|
'queueManager',
|
|
|
|
MciViewIds.queueManager.customRangeStart,
|
|
|
|
formatObj,
|
|
|
|
{ filter : Object.keys(formatObj).map(k => '{' + k + '}' ) }
|
|
|
|
);
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayQueueManagerPage(clearScreen, cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function prepArtAndViewController(callback) {
|
|
|
|
return self.displayArtAndPrepViewController('queueManager', { clearScreen : clearScreen }, callback);
|
|
|
|
},
|
|
|
|
function prepareQueueDownloadLinks(callback) {
|
|
|
|
const webDlExpireTimeFormat = self.menuConfig.config.webDlExpireTimeFormat || 'YYYY-MMM-DD @ h:mm';
|
|
|
|
|
|
|
|
const config = Config();
|
|
|
|
async.each(self.dlQueue.items, (fileEntry, nextFileEntry) => {
|
|
|
|
FileAreaWeb.getExistingTempDownloadServeItem(self.client, fileEntry, (err, serveItem) => {
|
|
|
|
if(err) {
|
|
|
|
if(ErrNotEnabled === err.reasonCode) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return nextFileEntry(err); // we should have caught this prior
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const expireTime = moment().add(config.fileBase.web.expireMinutes, 'minutes');
|
|
|
|
|
|
|
|
FileAreaWeb.createAndServeTempDownload(
|
|
|
|
self.client,
|
|
|
|
fileEntry,
|
|
|
|
{ expireTime : expireTime },
|
|
|
|
(err, url) => {
|
|
|
|
if(err) {
|
|
|
|
return nextFileEntry(err);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
fileEntry.webDlLinkRaw = url;
|
|
|
|
fileEntry.webDlLink = ansi.vtxHyperlink(self.client, url) + url;
|
|
|
|
fileEntry.webDlExpire = expireTime.format(webDlExpireTimeFormat);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return nextFileEntry(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
fileEntry.webDlLinkRaw = serveItem.url;
|
|
|
|
fileEntry.webDlLink = ansi.vtxHyperlink(self.client, serveItem.url) + serveItem.url;
|
|
|
|
fileEntry.webDlExpire = moment(serveItem.expireTimestamp).format(webDlExpireTimeFormat);
|
2018-06-22 05:15:04 +00:00
|
|
|
return nextFileEntry(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function populateViews(callback) {
|
|
|
|
return self.updateDownloadQueueView(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(cb) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayArtAndPrepViewController(name, options, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const self = this;
|
|
|
|
const config = this.menuConfig.config;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function readyAndDisplayArt(callback) {
|
|
|
|
if(options.clearScreen) {
|
|
|
|
self.client.term.rawWrite(ansi.resetScreen());
|
|
|
|
}
|
|
|
|
|
|
|
|
theme.displayThemedAsset(
|
|
|
|
config.art[name],
|
|
|
|
self.client,
|
|
|
|
{ font : self.menuConfig.font, trailingLF : false },
|
|
|
|
(err, artData) => {
|
|
|
|
return callback(err, artData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function prepeareViewController(artData, callback) {
|
|
|
|
if(_.isUndefined(self.viewControllers[name])) {
|
|
|
|
const vcOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
client : self.client,
|
|
|
|
formId : FormIds[name],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(!_.isUndefined(options.noInput)) {
|
|
|
|
vcOpts.noInput = options.noInput;
|
|
|
|
}
|
|
|
|
|
|
|
|
const vc = self.addViewController(name, new ViewController(vcOpts));
|
|
|
|
|
|
|
|
const loadOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
callingMenu : self,
|
|
|
|
mciMap : artData.mciMap,
|
|
|
|
formId : FormIds[name],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.viewControllers[name].setFocus(true);
|
|
|
|
return callback(null);
|
|
|
|
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-09-26 16:44:15 +00:00
|
|
|
};
|