2017-09-26 16:44:15 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
2023-08-24 00:06:48 +00:00
|
|
|
const { MenuModule, MenuFlags } = require('./menu_module.js');
|
2022-06-05 20:04:25 +00:00
|
|
|
const DownloadQueue = require('./download_queue.js');
|
|
|
|
const ansi = require('./ansi_term.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const FileAreaWeb = require('./file_area_web.js');
|
|
|
|
const ErrNotEnabled = require('./enig_error.js').ErrorReasons.NotEnabled;
|
|
|
|
const Config = require('./config.js').get;
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2017-09-26 16:44:15 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
queueManager: 0,
|
2017-09-26 16:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
2022-06-05 20:04:25 +00:00
|
|
|
queueManager: {
|
|
|
|
queue: 1,
|
|
|
|
navMenu: 2,
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
customRangeStart: 10,
|
|
|
|
},
|
2017-09-26 16:44:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class FileBaseWebDownloadQueueManager extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
2023-08-24 00:06:48 +00:00
|
|
|
this.setMergedFlag(MenuFlags.NoHistory);
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.dlQueue = new DownloadQueue(this.client);
|
|
|
|
|
|
|
|
this.menuMethods = {
|
2022-06-05 20:04:25 +00:00
|
|
|
removeItem: (formData, extraArgs, cb) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
const selectedItem = this.dlQueue.items[formData.value.queueItem];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!selectedItem) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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!
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.removeItemsFromDownloadQueueView(
|
|
|
|
formData.value.queueItem,
|
|
|
|
cb
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
clearQueue: (formData, extraArgs, cb) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
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);
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
getBatchLink: (formData, extraArgs, cb) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.generateAndDisplayBatchLink(cb);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === this.dlQueue.items.length) {
|
|
|
|
return this.gotoMenu(
|
|
|
|
this.menuConfig.config.emptyQueueMenu ||
|
|
|
|
'fileBaseDownloadManagerEmptyQueue'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function beforeArt(callback) {
|
|
|
|
return self.beforeArt(callback);
|
|
|
|
},
|
|
|
|
function display(callback) {
|
|
|
|
return self.displayQueueManagerPage(false, callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
() => {
|
|
|
|
return self.finishedLoading();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeItemsFromDownloadQueueView(itemIndex, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const queueView = this.viewControllers.queueManager.getView(
|
|
|
|
MciViewIds.queueManager.queue
|
|
|
|
);
|
|
|
|
if (!queueView) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(Errors.DoesNotExist('Queue view does not exist'));
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('all' === itemIndex) {
|
2018-06-22 05:15:04 +00:00
|
|
|
queueView.setItems([]);
|
|
|
|
queueView.setFocusItems([]);
|
|
|
|
} else {
|
|
|
|
queueView.removeItem(itemIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
queueView.redraw();
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayFileInfoForFileEntry(fileEntry) {
|
|
|
|
this.updateCustomViewTextsWithFilter(
|
|
|
|
'queueManager',
|
2022-06-05 20:04:25 +00:00
|
|
|
MciViewIds.queueManager.customRangeStart,
|
|
|
|
fileEntry,
|
|
|
|
{ filter: ['{webDlLink}', '{webDlExpire}', '{fileName}'] } // :TODO: Others....
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDownloadQueueView(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const queueView = this.viewControllers.queueManager.getView(
|
|
|
|
MciViewIds.queueManager.queue
|
|
|
|
);
|
|
|
|
if (!queueView) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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,
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
expireTime: expireTime,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
(err, webBatchDlLink) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: handle not enabled -> display such
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const webDlExpireTimeFormat =
|
|
|
|
this.menuConfig.config.webDlExpireTimeFormat || 'YYYY-MMM-DD @ h:mm';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const formatObj = {
|
2022-06-05 20:04:25 +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,
|
2022-06-05 20:04:25 +00:00
|
|
|
{ filter: Object.keys(formatObj).map(k => '{' + k + '}') }
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayQueueManagerPage(clearScreen, cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function prepArtAndViewController(callback) {
|
2022-06-02 17:12:23 +00:00
|
|
|
return self.displayArtAndPrepViewController(
|
|
|
|
'queueManager',
|
|
|
|
FormIds.queueManager,
|
2022-06-12 20:12:03 +00:00
|
|
|
{ clearScreen: clearScreen },
|
2022-06-02 17:12:23 +00:00
|
|
|
callback
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function prepareQueueDownloadLinks(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const webDlExpireTimeFormat =
|
|
|
|
self.menuConfig.config.webDlExpireTimeFormat ||
|
|
|
|
'YYYY-MMM-DD @ h:mm';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
const config = Config();
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
self.dlQueue.items,
|
|
|
|
(fileEntry, nextFileEntry) => {
|
|
|
|
FileAreaWeb.getExistingTempDownloadServeItem(
|
|
|
|
self.client,
|
|
|
|
fileEntry,
|
|
|
|
(err, serveItem) => {
|
|
|
|
if (err) {
|
|
|
|
if (ErrNotEnabled === err.reasonCode) {
|
|
|
|
return nextFileEntry(err); // we should have caught this prior
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
fileEntry.webDlLinkRaw = url;
|
|
|
|
fileEntry.webDlLink =
|
|
|
|
ansi.vtxHyperlink(self.client, url) +
|
|
|
|
url;
|
|
|
|
fileEntry.webDlExpire =
|
|
|
|
expireTime.format(
|
|
|
|
webDlExpireTimeFormat
|
|
|
|
);
|
|
|
|
|
|
|
|
return nextFileEntry(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function populateViews(callback) {
|
|
|
|
return self.updateDownloadQueueView(callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-09-26 16:44:15 +00:00
|
|
|
};
|