2017-01-02 04:53:04 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// enigma-bbs
|
2023-08-24 00:06:48 +00:00
|
|
|
const { MenuModule, MenuFlags } = require('./menu_module');
|
2022-06-05 20:04:25 +00:00
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const getSortedAvailableFileAreas =
|
|
|
|
require('./file_base_area.js').getSortedAvailableFileAreas;
|
|
|
|
const getAreaDefaultStorageDirectory =
|
|
|
|
require('./file_base_area.js').getAreaDefaultStorageDirectory;
|
|
|
|
const scanFile = require('./file_base_area.js').scanFile;
|
|
|
|
const getFileAreaByTag = require('./file_base_area.js').getFileAreaByTag;
|
|
|
|
const getDescFromFileName = require('./file_base_area.js').getDescFromFileName;
|
|
|
|
const ansiGoto = require('./ansi_term.js').goto;
|
|
|
|
const moveFileWithCollisionHandling =
|
|
|
|
require('./file_util.js').moveFileWithCollisionHandling;
|
|
|
|
const pathWithTerminatingSeparator =
|
|
|
|
require('./file_util.js').pathWithTerminatingSeparator;
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const FileEntry = require('./file_entry.js');
|
|
|
|
const isAnsi = require('./string_util.js').isAnsi;
|
|
|
|
const Events = require('./events.js');
|
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 temptmp = require('temptmp').createTrackedSession('upload');
|
|
|
|
const paths = require('path');
|
|
|
|
const sanatizeFilename = require('sanitize-filename');
|
2017-01-02 04:53:04 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2022-06-05 20:04:25 +00:00
|
|
|
name: 'Upload',
|
|
|
|
desc: 'Module for classic file uploads',
|
|
|
|
author: 'NuSkooler',
|
2017-01-02 04:53:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const FormIds = {
|
2022-06-05 20:04:25 +00:00
|
|
|
options: 0,
|
|
|
|
processing: 1,
|
|
|
|
fileDetails: 2,
|
|
|
|
dupes: 3,
|
2017-01-02 04:53:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const MciViewIds = {
|
2022-06-05 20:04:25 +00:00
|
|
|
options: {
|
|
|
|
area: 1, // area selection
|
|
|
|
uploadType: 2, // blind vs specify filename
|
|
|
|
fileName: 3, // for non-blind; not editable for blind
|
|
|
|
navMenu: 4, // next/cancel/etc.
|
|
|
|
errMsg: 5, // errors (e.g. filename cannot be blank)
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
processing: {
|
|
|
|
calcHashIndicator: 1,
|
|
|
|
archiveListIndicator: 2,
|
|
|
|
descFileIndicator: 3,
|
|
|
|
logStep: 4,
|
|
|
|
customRangeStart: 10, // 10+ = customs
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
fileDetails: {
|
|
|
|
desc: 1, // defaults to 'desc' (e.g. from FILE_ID.DIZ)
|
|
|
|
tags: 2, // tag(s) for item
|
|
|
|
estYear: 3,
|
|
|
|
accept: 4, // accept fields & continue
|
|
|
|
customRangeStart: 10, // 10+ = customs
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
dupes: {
|
|
|
|
dupeList: 1,
|
|
|
|
},
|
2017-01-02 04:53:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class UploadModule 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);
|
|
|
|
|
2019-01-10 03:06:55 +00:00
|
|
|
this.interrupt = MenuModule.InterruptTypes.Never;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.has(options, 'lastMenuResult.recvFilePaths')) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.recvFilePaths = options.lastMenuResult.recvFilePaths;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.availAreas = getSortedAvailableFileAreas(this.client, { writeAcs: true });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
this.menuMethods = {
|
2022-06-05 20:04:25 +00:00
|
|
|
optionsNavContinue: (formData, extraArgs, cb) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.performUpload(cb);
|
|
|
|
},
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
fileDetailsContinue: (formData, extraArgs, cb) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// see displayFileDetailsPageForUploadEntry() for this hackery:
|
2018-06-22 05:15:04 +00:00
|
|
|
cb(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.fileDetailsCurrentEntrySubmitCallback(null, formData.value); // move on to the next entry, if any
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// validation
|
2022-06-05 20:04:25 +00:00
|
|
|
validateNonBlindFileName: (fileName, cb) => {
|
|
|
|
if (0 === fileName.length) {
|
2018-06-26 00:08:41 +00:00
|
|
|
return cb(new Error('Filename cannot be empty'));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
fileName = sanatizeFilename(fileName); // remove unsafe chars, path info, etc.
|
|
|
|
if (0 === fileName.length) {
|
|
|
|
// sanatize nuked everything?
|
2018-06-26 00:08:41 +00:00
|
|
|
return cb(new Error('Invalid filename'));
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 00:08:41 +00:00
|
|
|
// At least SEXYZ doesn't like non-blind names that start with a number - it becomes confused ;-(
|
2022-06-05 20:04:25 +00:00
|
|
|
if (/^[0-9].*$/.test(fileName)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(new Error('Invalid filename'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
viewValidationListener: (err, cb) => {
|
|
|
|
const errView = this.viewControllers.options.getView(
|
|
|
|
MciViewIds.options.errMsg
|
|
|
|
);
|
|
|
|
if (errView) {
|
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
errView.setText(err.message);
|
|
|
|
} else {
|
|
|
|
errView.clearText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getSaveState() {
|
2018-06-23 03:26:46 +00:00
|
|
|
// if no areas, we're falling back due to lack of access/areas avail to upload to
|
2022-08-18 20:55:57 +00:00
|
|
|
if (this.availAreas.length > 0 && this.viewControllers.options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return {
|
2022-06-05 20:04:25 +00:00
|
|
|
uploadType: this.uploadType,
|
|
|
|
tempRecvDirectory: this.tempRecvDirectory,
|
|
|
|
areaInfo:
|
|
|
|
this.availAreas[
|
|
|
|
this.viewControllers.options
|
|
|
|
.getView(MciViewIds.options.area)
|
|
|
|
.getData()
|
|
|
|
],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreSavedState(savedState) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (savedState.areaInfo) {
|
|
|
|
this.uploadType = savedState.uploadType;
|
|
|
|
this.areaInfo = savedState.areaInfo;
|
|
|
|
this.tempRecvDirectory = savedState.tempRecvDirectory;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
isBlindUpload() {
|
|
|
|
return 'blind' === this.uploadType;
|
|
|
|
}
|
|
|
|
isFileTransferComplete() {
|
|
|
|
return !_.isUndefined(this.recvFilePaths);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === this.availAreas.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
return this.gotoMenu(
|
|
|
|
this.menuConfig.config.noUploadAreasAvailMenu ||
|
|
|
|
'fileBaseNoUploadAreasAvail'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function before(callback) {
|
|
|
|
return self.beforeArt(callback);
|
|
|
|
},
|
|
|
|
function display(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.isFileTransferComplete()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return self.displayProcessingPage(callback);
|
|
|
|
} else {
|
|
|
|
return self.displayOptionsPage(callback);
|
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
() => {
|
|
|
|
return self.finishedLoading();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
finishedLoading() {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.isFileTransferComplete()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.processUploadedFiles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
performUpload(cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
temptmp.mkdir({ prefix: 'enigul-' }, (err, tempRecvDirectory) => {
|
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// need a terminator for various external protocols
|
2018-06-22 05:15:04 +00:00
|
|
|
this.tempRecvDirectory = pathWithTerminatingSeparator(tempRecvDirectory);
|
|
|
|
|
|
|
|
const modOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
extraArgs: {
|
|
|
|
recvDirectory: this.tempRecvDirectory, // we'll move files from here to their area container once processed/confirmed
|
|
|
|
direction: 'recv',
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!this.isBlindUpload()) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// data has been sanatized at this point
|
2022-06-05 20:04:25 +00:00
|
|
|
modOpts.extraArgs.recvFileName = this.viewControllers.options
|
|
|
|
.getView(MciViewIds.options.fileName)
|
|
|
|
.getData();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Move along to protocol selection -> file transfer
|
|
|
|
// Upon completion, we'll re-enter the module with some file paths handed to us
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
return this.gotoMenu(
|
2022-06-05 20:04:25 +00:00
|
|
|
this.menuConfig.config.fileTransferProtocolSelection ||
|
|
|
|
'fileTransferProtocolSelection',
|
2018-06-22 05:15:04 +00:00
|
|
|
modOpts,
|
|
|
|
cb
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
continueNonBlindUpload(cb) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateScanStepInfoViews(stepInfo) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: add some blinking (e.g. toggle items) indicators - see OBV.DOC
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const fmtObj = Object.assign({}, stepInfo);
|
2018-06-22 05:15:04 +00:00
|
|
|
let stepIndicatorFmt = '';
|
|
|
|
let logStepFmt;
|
|
|
|
|
|
|
|
const fmtConfig = this.menuConfig.config;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const indicatorStates = fmtConfig.indicatorStates || ['|', '/', '-', '\\'];
|
2018-06-23 03:26:46 +00:00
|
|
|
const indicatorFinished = fmtConfig.indicatorFinished || '√';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const indicator = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
const self = this;
|
|
|
|
|
|
|
|
function updateIndicator(mci, isFinished) {
|
|
|
|
indicator.mci = mci;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (isFinished) {
|
2018-06-22 05:15:04 +00:00
|
|
|
indicator.text = indicatorFinished;
|
|
|
|
} else {
|
|
|
|
self.scanStatus.indicatorPos += 1;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.scanStatus.indicatorPos >= indicatorStates.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
self.scanStatus.indicatorPos = 0;
|
|
|
|
}
|
|
|
|
indicator.text = indicatorStates[self.scanStatus.indicatorPos];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (stepInfo.step) {
|
|
|
|
case 'start':
|
|
|
|
logStepFmt = stepIndicatorFmt =
|
|
|
|
fmtConfig.scanningStartFormat || 'Scanning {fileName}';
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'hash_update':
|
|
|
|
stepIndicatorFmt =
|
|
|
|
fmtConfig.calcHashFormat ||
|
|
|
|
'Calculating hash/checksums: {calcHashPercent}%';
|
2018-06-22 05:15:04 +00:00
|
|
|
updateIndicator(MciViewIds.processing.calcHashIndicator);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'hash_finish':
|
|
|
|
stepIndicatorFmt =
|
|
|
|
fmtConfig.calcHashCompleteFormat ||
|
|
|
|
'Finished calculating hash/checksums';
|
2018-06-22 05:15:04 +00:00
|
|
|
updateIndicator(MciViewIds.processing.calcHashIndicator, true);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'archive_list_start':
|
|
|
|
stepIndicatorFmt =
|
|
|
|
fmtConfig.extractArchiveListFormat || 'Extracting archive list';
|
2018-06-22 05:15:04 +00:00
|
|
|
updateIndicator(MciViewIds.processing.archiveListIndicator);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'archive_list_finish':
|
2018-06-22 05:15:04 +00:00
|
|
|
fmtObj.archivedFileCount = stepInfo.archiveEntries.length;
|
2022-06-05 20:04:25 +00:00
|
|
|
stepIndicatorFmt =
|
|
|
|
fmtConfig.extractArchiveListFinishFormat ||
|
|
|
|
'Archive list extracted ({archivedFileCount} files)';
|
2018-06-22 05:15:04 +00:00
|
|
|
updateIndicator(MciViewIds.processing.archiveListIndicator, true);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'archive_list_failed':
|
|
|
|
stepIndicatorFmt =
|
|
|
|
fmtConfig.extractArchiveListFailedFormat ||
|
|
|
|
'Archive list extraction failed';
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'desc_files_start':
|
|
|
|
stepIndicatorFmt =
|
|
|
|
fmtConfig.processingDescFilesFormat || 'Processing description files';
|
2018-06-22 05:15:04 +00:00
|
|
|
updateIndicator(MciViewIds.processing.descFileIndicator);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'desc_files_finish':
|
|
|
|
stepIndicatorFmt =
|
|
|
|
fmtConfig.processingDescFilesFinishFormat ||
|
|
|
|
'Finished processing description files';
|
2018-06-22 05:15:04 +00:00
|
|
|
updateIndicator(MciViewIds.processing.descFileIndicator, true);
|
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
case 'finished':
|
|
|
|
logStepFmt = stepIndicatorFmt =
|
|
|
|
fmtConfig.scanningStartFormat || 'Finished';
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fmtObj.stepIndicatorText = stringFormat(stepIndicatorFmt, fmtObj);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.hasProcessingArt) {
|
|
|
|
this.updateCustomViewTextsWithFilter(
|
|
|
|
'processing',
|
|
|
|
MciViewIds.processing.customRangeStart,
|
|
|
|
fmtObj,
|
|
|
|
{ appendMultiLine: true }
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (indicator.mci && indicator.text) {
|
2018-06-22 05:15:04 +00:00
|
|
|
this.setViewText('processing', indicator.mci, indicator.text);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (logStepFmt) {
|
|
|
|
this.setViewText(
|
|
|
|
'processing',
|
|
|
|
MciViewIds.processing.logStep,
|
|
|
|
stringFormat(logStepFmt, fmtObj),
|
|
|
|
{ appendMultiLine: true }
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.client.term.pipeWrite(fmtObj.stepIndicatorText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scanFiles(cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
const results = {
|
2022-06-05 20:04:25 +00:00
|
|
|
newEntries: [],
|
|
|
|
dupes: [],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.debug('Scanning upload(s)', { paths: this.recvFilePaths });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
let currentFileNum = 0;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.eachSeries(
|
|
|
|
this.recvFilePaths,
|
|
|
|
(filePath, nextFilePath) => {
|
|
|
|
// :TODO: virus scanning/etc. should occur around here
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
currentFileNum += 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.scanStatus = {
|
|
|
|
indicatorPos: 0,
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const scanOpts = {
|
|
|
|
areaTag: self.areaInfo.areaTag,
|
|
|
|
storageTag: self.areaInfo.storageTags[0],
|
|
|
|
hashTags: self.areaInfo.hashTags,
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
function handleScanStep(stepInfo, nextScanStep) {
|
|
|
|
stepInfo.totalFileNum = self.recvFilePaths.length;
|
|
|
|
stepInfo.currentFileNum = currentFileNum;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.updateScanStepInfoViews(stepInfo);
|
|
|
|
return nextScanStep(null);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.debug('Scanning file', { filePath: filePath });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
scanFile(
|
|
|
|
filePath,
|
|
|
|
scanOpts,
|
|
|
|
handleScanStep,
|
|
|
|
(err, fileEntry, dupeEntries) => {
|
|
|
|
if (err) {
|
|
|
|
return nextFilePath(err);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// new or dupe?
|
|
|
|
if (dupeEntries.length > 0) {
|
|
|
|
// 1:n dupes found
|
|
|
|
self.client.log.debug('Duplicate file(s) found', {
|
|
|
|
dupeEntries: dupeEntries,
|
|
|
|
});
|
|
|
|
|
|
|
|
results.dupes = results.dupes.concat(dupeEntries);
|
|
|
|
} else {
|
|
|
|
// new one
|
|
|
|
results.newEntries.push(fileEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextFilePath(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, results);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanupTempFiles() {
|
2022-06-05 20:04:25 +00:00
|
|
|
temptmp.cleanup(paths => {
|
|
|
|
Log.debug(
|
|
|
|
{ paths: paths, sessionId: temptmp.sessionId },
|
|
|
|
'Temporary files cleaned up'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
moveAndPersistUploadsToDatabase(newEntries) {
|
|
|
|
const areaStorageDir = getAreaDefaultStorageDirectory(this.areaInfo);
|
|
|
|
const self = this;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.eachSeries(
|
|
|
|
newEntries,
|
|
|
|
(newEntry, nextEntry) => {
|
|
|
|
const src = paths.join(self.tempRecvDirectory, newEntry.fileName);
|
|
|
|
const dst = paths.join(areaStorageDir, newEntry.fileName);
|
|
|
|
|
|
|
|
moveFileWithCollisionHandling(src, dst, (err, finalPath) => {
|
|
|
|
if (err) {
|
|
|
|
self.client.log.error('Failed moving physical upload file', {
|
|
|
|
error: err.message,
|
|
|
|
fileName: newEntry.fileName,
|
|
|
|
source: src,
|
|
|
|
dest: dst,
|
|
|
|
});
|
|
|
|
return nextEntry(null); // still try next file
|
|
|
|
} else if (dst !== finalPath) {
|
|
|
|
// name changed; adjust before persist
|
|
|
|
newEntry.fileName = paths.basename(finalPath);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.log.debug('Moved upload to area', { path: finalPath });
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
// persist to DB
|
|
|
|
newEntry.persist(err => {
|
|
|
|
if (err) {
|
|
|
|
self.client.log.error(
|
|
|
|
'Failed persisting upload to database',
|
|
|
|
{ path: finalPath, error: err.message }
|
|
|
|
);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return nextEntry(null); // still try next file
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
|
|
|
() => {
|
|
|
|
//
|
|
|
|
// Finally, we can remove any temp files that we may have created
|
|
|
|
//
|
|
|
|
self.cleanupTempFiles();
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prepDetailsForUpload(scanResults, cb) {
|
2022-06-05 20:04:25 +00:00
|
|
|
async.eachSeries(
|
|
|
|
scanResults.newEntries,
|
|
|
|
(newEntry, nextEntry) => {
|
|
|
|
newEntry.meta.upload_by_username = this.client.user.username;
|
|
|
|
newEntry.meta.upload_by_user_id = this.client.user.userId;
|
|
|
|
|
|
|
|
this.displayFileDetailsPageForUploadEntry(newEntry, (err, newValues) => {
|
|
|
|
if (err) {
|
|
|
|
return nextEntry(err);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!newEntry.descIsAnsi) {
|
|
|
|
newEntry.desc = _.trimEnd(newValues.shortDesc);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (newValues.estYear.length > 0) {
|
|
|
|
newEntry.meta.est_release_year = newValues.estYear;
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (newValues.tags.length > 0) {
|
|
|
|
newEntry.setHashTags(newValues.tags);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return nextEntry(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
delete this.fileDetailsCurrentEntrySubmitCallback;
|
|
|
|
return cb(err, scanResults);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayDupesPage(dupes, cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If we have custom art to show, use it - else just dump basic info.
|
|
|
|
// Pause at the end in either case.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function prepArtAndViewController(callback) {
|
|
|
|
self.prepViewControllerWithArt(
|
|
|
|
'dupes',
|
|
|
|
FormIds.dupes,
|
2022-06-05 20:04:25 +00:00
|
|
|
{ clearScreen: true, trailingLF: false },
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
|
|
|
self.client.term.pipeWrite(
|
|
|
|
'|00|07Duplicate upload(s) found:\n'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, null);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const dupeListView = self.viewControllers.dupes.getView(
|
|
|
|
MciViewIds.dupes.dupeList
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, dupeListView);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function prepDupeObjects(dupeListView, callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// update dupe objects with additional info that can be used for formatString() and the like
|
2022-06-05 20:04:25 +00:00
|
|
|
async.each(
|
|
|
|
dupes,
|
|
|
|
(dupe, nextDupe) => {
|
|
|
|
FileEntry.loadBasicEntry(dupe.fileId, dupe, err => {
|
|
|
|
if (err) {
|
|
|
|
return nextDupe(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const areaInfo = getFileAreaByTag(dupe.areaTag);
|
|
|
|
if (areaInfo) {
|
|
|
|
dupe.areaName = areaInfo.name;
|
|
|
|
dupe.areaDesc = areaInfo.desc;
|
|
|
|
}
|
|
|
|
return nextDupe(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err, dupeListView);
|
|
|
|
}
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
function populateDupeInfo(dupeListView, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const dupeInfoFormat =
|
|
|
|
self.menuConfig.config.dupeInfoFormat ||
|
|
|
|
'{fileName} @ {areaName}';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (dupeListView) {
|
|
|
|
dupeListView.setItems(
|
|
|
|
dupes.map(dupe => stringFormat(dupeInfoFormat, dupe))
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
dupeListView.redraw();
|
|
|
|
} else {
|
|
|
|
dupes.forEach(dupe => {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.client.term.pipeWrite(
|
|
|
|
`${stringFormat(dupeInfoFormat, dupe)}\n`
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function pause(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return self.pausePrompt(
|
|
|
|
{ row: self.client.term.termHeight },
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
processUploadedFiles() {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// For each file uploaded, we need to process & gather information
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function prepNonBlind(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.isBlindUpload()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// For non-blind uploads, batch is not supported, we expect a single file
|
|
|
|
// in |recvFilePaths|. If not, it's an error (we don't want to process the wrong thing)
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.recvFilePaths.length > 1) {
|
|
|
|
self.client.log.warn(
|
|
|
|
{ recvFilePaths: self.recvFilePaths },
|
|
|
|
'Non-blind upload received 2:n files'
|
|
|
|
);
|
|
|
|
return callback(
|
|
|
|
Errors.UnexpectedState(
|
|
|
|
`Non-blind upload expected single file but got received ${self.recvFilePaths.length}`
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function scan(callback) {
|
|
|
|
return self.scanFiles(callback);
|
|
|
|
},
|
|
|
|
function pause(scanResults, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.hasProcessingArt) {
|
|
|
|
self.client.term.rawWrite(
|
|
|
|
ansiGoto(self.client.term.termHeight, 1)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
self.client.term.write('\n');
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.pausePrompt(() => {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, scanResults);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function displayDupes(scanResults, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === scanResults.dupes.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null, scanResults);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.displayDupesPage(scanResults.dupes, () => {
|
|
|
|
return callback(null, scanResults);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function prepDetails(scanResults, callback) {
|
|
|
|
return self.prepDetailsForUpload(scanResults, callback);
|
|
|
|
},
|
|
|
|
function startMovingAndPersistingToDatabase(scanResults, callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// *Start* the process of moving files from their current |tempRecvDirectory|
|
|
|
|
// locations -> their final area destinations. Don't make the user wait
|
|
|
|
// here as I/O can take quite a bit of time. Log any failures.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
self.moveAndPersistUploadsToDatabase(scanResults.newEntries);
|
|
|
|
return callback(null, scanResults.newEntries);
|
|
|
|
},
|
|
|
|
function sendEvent(uploadedEntries, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
Events.emit(Events.getSystemEvents().UserUpload, {
|
|
|
|
user: self.client.user,
|
|
|
|
files: uploadedEntries,
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
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 (err) {
|
|
|
|
self.client.log.warn('File upload error encountered', {
|
|
|
|
error: err.message,
|
|
|
|
});
|
|
|
|
self.cleanupTempFiles(); // normally called after moveAndPersistUploadsToDatabase() is completed.
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self.prevMenu();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayOptionsPage(cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function prepArtAndViewController(callback) {
|
|
|
|
return self.prepViewControllerWithArt(
|
|
|
|
'options',
|
|
|
|
FormIds.options,
|
2022-06-05 20:04:25 +00:00
|
|
|
{ clearScreen: true, trailingLF: false },
|
2018-06-22 05:15:04 +00:00
|
|
|
callback
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function populateViews(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const areaSelectView = self.viewControllers.options.getView(
|
|
|
|
MciViewIds.options.area
|
|
|
|
);
|
|
|
|
areaSelectView.setItems(
|
|
|
|
self.availAreas.map(areaInfo => areaInfo.name)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const uploadTypeView = self.viewControllers.options.getView(
|
|
|
|
MciViewIds.options.uploadType
|
|
|
|
);
|
|
|
|
const fileNameView = self.viewControllers.options.getView(
|
|
|
|
MciViewIds.options.fileName
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const blindFileNameText =
|
|
|
|
self.menuConfig.config.blindFileNameText ||
|
|
|
|
'(blind - filename ignored)';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
uploadTypeView.on('index update', idx => {
|
2022-06-05 20:04:25 +00:00
|
|
|
self.uploadType = 0 === idx ? 'blind' : 'non-blind';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.isBlindUpload()) {
|
2018-06-22 05:15:04 +00:00
|
|
|
fileNameView.setText(blindFileNameText);
|
|
|
|
fileNameView.acceptsFocus = false;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else {
|
2018-06-22 05:15:04 +00:00
|
|
|
fileNameView.clearText();
|
|
|
|
fileNameView.acceptsFocus = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// sanatize filename for display when leaving the view
|
2018-06-22 05:15:04 +00:00
|
|
|
self.viewControllers.options.on('leave', prevView => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (prevView.id === MciViewIds.options.fileName) {
|
|
|
|
fileNameView.setText(
|
|
|
|
sanatizeFilename(fileNameView.getData())
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
self.uploadType = 'blind';
|
2022-06-05 20:04:25 +00:00
|
|
|
uploadTypeView.setFocusItemIndex(0); // default to blind
|
2018-06-22 05:15:04 +00:00
|
|
|
fileNameView.setText(blindFileNameText);
|
|
|
|
areaSelectView.redraw();
|
|
|
|
|
|
|
|
return callback(null);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
displayProcessingPage(cb) {
|
|
|
|
return this.prepViewControllerWithArt(
|
|
|
|
'processing',
|
|
|
|
FormIds.processing,
|
2022-06-05 20:04:25 +00:00
|
|
|
{ clearScreen: true, trailingLF: false },
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note: this art is not required
|
2018-06-22 05:15:04 +00:00
|
|
|
this.hasProcessingArt = !err;
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fileEntryHasDetectedDesc(fileEntry) {
|
2022-06-05 20:04:25 +00:00
|
|
|
return fileEntry.desc && fileEntry.desc.length > 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
displayFileDetailsPageForUploadEntry(fileEntry, cb) {
|
|
|
|
const self = this;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function prepArtAndViewController(callback) {
|
|
|
|
return self.prepViewControllerWithArt(
|
|
|
|
'fileDetails',
|
|
|
|
FormIds.fileDetails,
|
2022-06-05 20:04:25 +00:00
|
|
|
{ clearScreen: true, trailingLF: false },
|
2018-06-22 05:15:04 +00:00
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function populateViews(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
const descView = self.viewControllers.fileDetails.getView(
|
|
|
|
MciViewIds.fileDetails.desc
|
|
|
|
);
|
|
|
|
const tagsView = self.viewControllers.fileDetails.getView(
|
|
|
|
MciViewIds.fileDetails.tags
|
|
|
|
);
|
|
|
|
const yearView = self.viewControllers.fileDetails.getView(
|
|
|
|
MciViewIds.fileDetails.estYear
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
self.updateCustomViewTextsWithFilter(
|
|
|
|
'fileDetails',
|
|
|
|
MciViewIds.fileDetails.customRangeStart,
|
|
|
|
fileEntry
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
tagsView.setText(Array.from(fileEntry.hashTags).join(',')); // :TODO: optional 'hashTagsSep' like file list/browse
|
2018-06-22 05:15:04 +00:00
|
|
|
yearView.setText(fileEntry.meta.est_release_year || '');
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (isAnsi(fileEntry.desc)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
fileEntry.descIsAnsi = true;
|
|
|
|
|
|
|
|
return descView.setAnsi(
|
|
|
|
fileEntry.desc,
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
prepped: false,
|
|
|
|
forceLineTerm: true,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
() => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
descView,
|
|
|
|
'preview',
|
|
|
|
MciViewIds.fileDetails.tags
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const hasDesc = self.fileEntryHasDetectedDesc(fileEntry);
|
|
|
|
descView.setText(
|
2022-06-05 20:04:25 +00:00
|
|
|
hasDesc
|
|
|
|
? fileEntry.desc
|
|
|
|
: getDescFromFileName(fileEntry.fileName),
|
|
|
|
{ scrollMode: 'top' } // override scroll mode; we want to be @ top
|
|
|
|
);
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
descView,
|
|
|
|
'edit',
|
|
|
|
hasDesc
|
|
|
|
? MciViewIds.fileDetails.tags
|
|
|
|
: MciViewIds.fileDetails.desc
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function finalizeViews(descView, descViewMode, focusId, callback) {
|
|
|
|
descView.setPropertyValue('mode', descViewMode);
|
2022-06-05 20:04:25 +00:00
|
|
|
descView.acceptsFocus = 'preview' === descViewMode ? false : true;
|
2018-06-22 05:15:04 +00:00
|
|
|
self.viewControllers.fileDetails.switchFocus(focusId);
|
|
|
|
return callback(null);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// we only call |cb| here if there is an error
|
|
|
|
// else, wait for the current from to be submit - then call -
|
|
|
|
// this way we'll move on to the next file entry when ready
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
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
|
|
|
self.fileDetailsCurrentEntrySubmitCallback = cb; // stash for moduleMethods.fileDetailsContinue
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2017-01-02 04:53:04 +00:00
|
|
|
};
|