2016-09-29 03:54:25 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// enigma-bbs
|
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const DownloadQueue = require('./download_queue.js');
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const FileEntry = require('./file_entry.js');
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const Events = require('./events.js');
|
2018-11-26 02:05:16 +00:00
|
|
|
const UserProps = require('./user_property.js');
|
|
|
|
const SysProps = require('./system_property.js');
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const pty = require('node-pty');
|
|
|
|
const temptmp = require('temptmp').createTrackedSession('transfer_file');
|
|
|
|
const paths = require('path');
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const fse = require('fs-extra');
|
|
|
|
|
|
|
|
// some consts
|
|
|
|
const SYSTEM_EOL = require('os').EOL;
|
|
|
|
const TEMP_SUFFIX = 'enigtf-'; // temp CWD/etc.
|
2017-01-24 06:32:40 +00:00
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
Notes
|
|
|
|
-----------------------------------------------------------------------------
|
2017-02-04 16:20:36 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
See core/config.js for external protocol configuration
|
2017-02-04 16:20:36 +00:00
|
|
|
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
Resources
|
|
|
|
-----------------------------------------------------------------------------
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
ZModem
|
|
|
|
* http://gallium.inria.fr/~doligez/zmodem/zmodem.txt
|
|
|
|
* https://github.com/protomouse/synchronet/blob/master/src/sbbs3/zmodem.c
|
2017-02-04 16:20:36 +00:00
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'Transfer file',
|
|
|
|
desc : 'Sends or receives a file(s)',
|
|
|
|
author : 'NuSkooler',
|
2016-09-29 03:54:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getModule = class TransferFileModule extends MenuModule {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
|
|
|
|
this.config = this.menuConfig.config || {};
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Most options can be set via extraArgs or config block
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const config = Config();
|
|
|
|
if(options.extraArgs) {
|
|
|
|
if(options.extraArgs.protocol) {
|
|
|
|
this.protocolConfig = config.fileTransferProtocols[options.extraArgs.protocol];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.extraArgs.direction) {
|
|
|
|
this.direction = options.extraArgs.direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.extraArgs.sendQueue) {
|
|
|
|
this.sendQueue = options.extraArgs.sendQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.extraArgs.recvFileName) {
|
|
|
|
this.recvFileName = options.extraArgs.recvFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(options.extraArgs.recvDirectory) {
|
|
|
|
this.recvDirectory = options.extraArgs.recvDirectory;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(this.config.protocol) {
|
|
|
|
this.protocolConfig = config.fileTransferProtocols[this.config.protocol];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.config.direction) {
|
|
|
|
this.direction = this.config.direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.config.sendQueue) {
|
|
|
|
this.sendQueue = this.config.sendQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.config.recvFileName) {
|
|
|
|
this.recvFileName = this.config.recvFileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this.config.recvDirectory) {
|
|
|
|
this.recvDirectory = this.config.recvDirectory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.protocolConfig = this.protocolConfig || config.fileTransferProtocols.zmodem8kSz; // try for *something*
|
|
|
|
this.direction = this.direction || 'send';
|
|
|
|
this.sendQueue = this.sendQueue || [];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Ensure sendQueue is an array of objects that contain at least a 'path' member
|
2018-06-22 05:15:04 +00:00
|
|
|
this.sendQueue = this.sendQueue.map(item => {
|
|
|
|
if(_.isString(item)) {
|
|
|
|
return { path : item };
|
|
|
|
} else {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.sentFileIds = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
isSending() {
|
|
|
|
return ('send' === this.direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
restorePipeAfterExternalProc() {
|
|
|
|
if(!this.pipeRestored) {
|
|
|
|
this.pipeRestored = true;
|
|
|
|
|
|
|
|
this.client.restoreDataHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendFiles(cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// assume *sending* can always batch
|
|
|
|
// :TODO: Look into this further
|
2018-06-22 05:15:04 +00:00
|
|
|
const allFiles = this.sendQueue.map(f => f.path);
|
|
|
|
this.executeExternalProtocolHandlerForSend(allFiles, err => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn( { files : allFiles, error : err.message }, 'Error sending file(s)' );
|
|
|
|
} else {
|
|
|
|
const sentFiles = [];
|
|
|
|
this.sendQueue.forEach(f => {
|
|
|
|
f.sent = true;
|
|
|
|
sentFiles.push(f.path);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.client.log.info( { sentFiles : sentFiles }, `Successfully sent ${sentFiles.length} file(s)` );
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
sendFiles(cb) {
|
|
|
|
// :TODO: built in/native protocol support
|
|
|
|
|
|
|
|
if(this.protocolConfig.external.supportsBatch) {
|
|
|
|
const allFiles = this.sendQueue.map(f => f.path);
|
|
|
|
this.executeExternalProtocolHandlerForSend(allFiles, err => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn( { files : allFiles, error : err.message }, 'Error sending file(s)' );
|
|
|
|
} else {
|
|
|
|
const sentFiles = [];
|
|
|
|
this.sendQueue.forEach(f => {
|
|
|
|
f.sent = true;
|
|
|
|
sentFiles.push(f.path);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.client.log.info( { sentFiles : sentFiles }, `Successfully sent ${sentFiles.length} file(s)` );
|
|
|
|
}
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// :TODO: we need to prompt between entries such that users can prepare their clients
|
|
|
|
async.eachSeries(this.sendQueue, (queueItem, next) => {
|
|
|
|
this.executeExternalProtocolHandlerForSend(queueItem.path, err => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn( { file : queueItem.path, error : err.message }, 'Error sending file' );
|
|
|
|
} else {
|
|
|
|
queueItem.sent = true;
|
|
|
|
|
|
|
|
this.client.log.info( { sentFile : queueItem.path }, 'Successfully sent file' );
|
|
|
|
}
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
moveFileWithCollisionHandling(src, dst, cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Move |src| -> |dst| renaming to file(1).ext, file(2).ext, etc.
|
|
|
|
// in the case of collisions.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
const dstPath = paths.dirname(dst);
|
|
|
|
const dstFileExt = paths.extname(dst);
|
|
|
|
const dstFileSuffix = paths.basename(dst, dstFileExt);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
let renameIndex = 0;
|
|
|
|
let movedOk = false;
|
2018-06-22 05:15:04 +00:00
|
|
|
let tryDstPath;
|
|
|
|
|
|
|
|
async.until(
|
2019-07-09 01:27:54 +00:00
|
|
|
(callback) => callback(null, movedOk), // until moved OK
|
2018-06-22 05:15:04 +00:00
|
|
|
(cb) => {
|
|
|
|
if(0 === renameIndex) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// try originally supplied path first
|
2018-06-22 05:15:04 +00:00
|
|
|
tryDstPath = dst;
|
|
|
|
} else {
|
|
|
|
tryDstPath = paths.join(dstPath, `${dstFileSuffix}(${renameIndex})${dstFileExt}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
fse.move(src, tryDstPath, err => {
|
|
|
|
if(err) {
|
|
|
|
if('EEXIST' === err.code) {
|
|
|
|
renameIndex += 1;
|
2018-06-23 03:26:46 +00:00
|
|
|
return cb(null); // keep trying
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
movedOk = true;
|
|
|
|
return cb(null, tryDstPath);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
(err, finalPath) => {
|
|
|
|
return cb(err, finalPath);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
recvFiles(cb) {
|
|
|
|
this.executeExternalProtocolHandlerForRecv(err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.recvFilePaths = [];
|
|
|
|
|
|
|
|
if(this.recvFileName) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// file name specified - we expect a single file in |this.recvDirectory|
|
|
|
|
// by the name of |this.recvFileName|
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const recvFullPath = paths.join(this.recvDirectory, this.recvFileName);
|
|
|
|
fs.stat(recvFullPath, (err, stats) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!stats.isFile()) {
|
|
|
|
return cb(Errors.Invalid('Expected file entry in recv directory'));
|
|
|
|
}
|
|
|
|
|
|
|
|
this.recvFilePaths.push(recvFullPath);
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Blind Upload (recv): files in |this.recvDirectory| should be named appropriately already
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
fs.readdir(this.recvDirectory, (err, files) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// stat each to grab files only
|
2018-06-22 05:15:04 +00:00
|
|
|
async.each(files, (fileName, nextFile) => {
|
|
|
|
const recvFullPath = paths.join(this.recvDirectory, fileName);
|
|
|
|
|
|
|
|
fs.stat(recvFullPath, (err, stats) => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn('Failed to stat file', { path : recvFullPath } );
|
2018-06-23 03:26:46 +00:00
|
|
|
return nextFile(null); // just try the next one
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(stats.isFile()) {
|
|
|
|
this.recvFilePaths.push(recvFullPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextFile(null);
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pathWithTerminatingSeparator(path) {
|
|
|
|
if(path && paths.sep !== path.charAt(path.length - 1)) {
|
|
|
|
path = path + paths.sep;
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
prepAndBuildSendArgs(filePaths, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const externalArgs = this.protocolConfig.external['sendArgs'];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getTempFileListPath(callback) {
|
|
|
|
const hasFileList = externalArgs.find(ea => (ea.indexOf('{fileListPath}') > -1) );
|
|
|
|
if(!hasFileList) {
|
|
|
|
return callback(null, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
temptmp.open( { prefix : TEMP_SUFFIX, suffix : '.txt' }, (err, tempFileInfo) => {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(err); // failed to create it
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 04:09:27 +00:00
|
|
|
fs.write(tempFileInfo.fd, filePaths.join(SYSTEM_EOL), err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
fs.close(tempFileInfo.fd, err => {
|
|
|
|
return callback(err, tempFileInfo.path);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createArgs(tempFileListPath, callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// initial args: ignore {filePaths} as we must break that into it's own sep array items
|
2018-06-22 05:15:04 +00:00
|
|
|
const args = externalArgs.map(arg => {
|
|
|
|
return '{filePaths}' === arg ? arg : stringFormat(arg, {
|
2018-06-23 03:26:46 +00:00
|
|
|
fileListPath : tempFileListPath || '',
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const filePathsPos = args.indexOf('{filePaths}');
|
|
|
|
if(filePathsPos > -1) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// replace {filePaths} with 0:n individual entries in |args|
|
2018-06-22 05:15:04 +00:00
|
|
|
args.splice.apply( args, [ filePathsPos, 1 ].concat(filePaths) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, args);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, args) => {
|
|
|
|
return cb(err, args);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
prepAndBuildRecvArgs(cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const argsKey = this.recvFileName ? 'recvArgsNonBatch' : 'recvArgs';
|
|
|
|
const externalArgs = this.protocolConfig.external[argsKey];
|
|
|
|
const args = externalArgs.map(arg => stringFormat(arg, {
|
|
|
|
uploadDir : this.recvDirectory,
|
|
|
|
fileName : this.recvFileName || '',
|
2018-06-22 05:15:04 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
return cb(null, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
executeExternalProtocolHandler(args, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const external = this.protocolConfig.external;
|
|
|
|
const cmd = external[`${this.direction}Cmd`];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-05-27 04:55:51 +00:00
|
|
|
// support for handlers that need IACs taken care of over Telnet/etc.
|
|
|
|
const processIACs =
|
|
|
|
external.processIACs ||
|
|
|
|
external.escapeTelnet; // deprecated name
|
|
|
|
|
|
|
|
// :TODO: we should only do this when over Telnet (or derived, such as WebSockets)?
|
|
|
|
|
|
|
|
const IAC = Buffer.from([255]);
|
|
|
|
const EscapedIAC = Buffer.from([255, 255]);
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.log.debug(
|
|
|
|
{ cmd : cmd, args : args, tempDir : this.recvDirectory, direction : this.direction },
|
|
|
|
'Executing external protocol'
|
|
|
|
);
|
|
|
|
|
|
|
|
const spawnOpts = {
|
2018-06-23 03:26:46 +00:00
|
|
|
cols : this.client.term.termWidth,
|
|
|
|
rows : this.client.term.termHeight,
|
|
|
|
cwd : this.recvDirectory,
|
|
|
|
encoding : null, // don't bork our data!
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const externalProc = pty.spawn(cmd, args, spawnOpts);
|
|
|
|
|
2020-05-06 23:47:13 +00:00
|
|
|
let dataHits = 0;
|
|
|
|
const updateActivity = () => {
|
|
|
|
if (0 === (dataHits++ % 4)) {
|
|
|
|
this.client.explicitActivityTimeUpdate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.client.setTemporaryDirectDataHandler(data => {
|
2020-05-06 23:47:13 +00:00
|
|
|
updateActivity();
|
2020-05-28 03:05:19 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// needed for things like sz/rz
|
2020-05-27 04:55:51 +00:00
|
|
|
if(processIACs) {
|
|
|
|
let iacPos = data.indexOf(EscapedIAC);
|
|
|
|
if (-1 === iacPos) {
|
|
|
|
return externalProc.write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// at least one double (escaped) IAC
|
|
|
|
let lastPos = 0;
|
|
|
|
while (iacPos > -1) {
|
|
|
|
let rem = iacPos - lastPos;
|
|
|
|
if (rem >= 0) {
|
|
|
|
externalProc.write(data.slice(lastPos, iacPos + 1));
|
|
|
|
}
|
|
|
|
lastPos = iacPos + 2;
|
|
|
|
iacPos = data.indexOf(EscapedIAC, lastPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastPos < data.length) {
|
|
|
|
externalProc.write(data.slice(lastPos));
|
|
|
|
}
|
|
|
|
// const tmp = data.toString('binary').replace(/\xff{2}/g, '\xff'); // de-escape
|
|
|
|
// externalProc.write(Buffer.from(tmp, 'binary'));
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
externalProc.write(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-16 02:56:31 +00:00
|
|
|
externalProc.onData(data => {
|
2020-05-06 23:47:13 +00:00
|
|
|
updateActivity();
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// needed for things like sz/rz
|
2020-05-27 04:55:51 +00:00
|
|
|
if(processIACs) {
|
|
|
|
let iacPos = data.indexOf(IAC);
|
|
|
|
if (-1 === iacPos) {
|
|
|
|
return this.client.term.rawWrite(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has at least a single IAC
|
|
|
|
let lastPos = 0;
|
|
|
|
while (iacPos !== -1) {
|
|
|
|
if (iacPos - lastPos > 0) {
|
|
|
|
this.client.term.rawWrite(data.slice(lastPos, iacPos));
|
|
|
|
}
|
|
|
|
this.client.term.rawWrite(EscapedIAC);
|
|
|
|
lastPos = iacPos + 1;
|
|
|
|
iacPos = data.indexOf(IAC, lastPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastPos < data.length) {
|
|
|
|
this.client.term.rawWrite(data.slice(lastPos));
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
this.client.term.rawWrite(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
externalProc.once('close', () => {
|
|
|
|
return this.restorePipeAfterExternalProc();
|
|
|
|
});
|
|
|
|
|
|
|
|
externalProc.once('exit', (exitCode) => {
|
|
|
|
this.client.log.debug( { cmd : cmd, args : args, exitCode : exitCode }, 'Process exited' );
|
|
|
|
|
|
|
|
this.restorePipeAfterExternalProc();
|
|
|
|
externalProc.removeAllListeners();
|
|
|
|
|
|
|
|
return cb(exitCode ? Errors.ExternalProcess(`Process exited with exit code ${exitCode}`, 'EBADEXIT') : null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
executeExternalProtocolHandlerForSend(filePaths, cb) {
|
|
|
|
if(!Array.isArray(filePaths)) {
|
|
|
|
filePaths = [ filePaths ];
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prepAndBuildSendArgs(filePaths, (err, args) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.executeExternalProtocolHandler(args, err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
executeExternalProtocolHandlerForRecv(cb) {
|
|
|
|
this.prepAndBuildRecvArgs( (err, args) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.executeExternalProtocolHandler(args, err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getMenuResult() {
|
|
|
|
if(this.isSending()) {
|
|
|
|
return { sentFileIds : this.sentFileIds };
|
|
|
|
} else {
|
|
|
|
return { recvFilePaths : this.recvFilePaths };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSendStats(cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
let downloadBytes = 0;
|
|
|
|
let downloadCount = 0;
|
|
|
|
let fileIds = [];
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.each(this.sendQueue, (queueItem, next) => {
|
|
|
|
if(!queueItem.sent) {
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(queueItem.fileId) {
|
|
|
|
fileIds.push(queueItem.fileId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isNumber(queueItem.byteSize)) {
|
|
|
|
downloadCount += 1;
|
|
|
|
downloadBytes += queueItem.byteSize;
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// we just have a path - figure it out
|
2018-06-22 05:15:04 +00:00
|
|
|
fs.stat(queueItem.path, (err, stats) => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn( { error : err.message, path : queueItem.path }, 'File stat failed' );
|
|
|
|
} else {
|
|
|
|
downloadCount += 1;
|
|
|
|
downloadBytes += stats.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(null);
|
|
|
|
});
|
|
|
|
}, () => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// All stats/meta currently updated via fire & forget - if this is ever a issue, we can wait for callbacks
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.incrementUserStat(this.client.user, UserProps.FileDlTotalCount, downloadCount);
|
|
|
|
StatLog.incrementUserStat(this.client.user, UserProps.FileDlTotalBytes, downloadBytes);
|
|
|
|
|
|
|
|
StatLog.incrementSystemStat(SysProps.FileDlTotalCount, downloadCount);
|
|
|
|
StatLog.incrementSystemStat(SysProps.FileDlTotalBytes, downloadBytes);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-11-26 22:53:21 +00:00
|
|
|
StatLog.incrementNonPersistentSystemStat(SysProps.FileDlTodayCount, downloadCount);
|
|
|
|
StatLog.incrementNonPersistentSystemStat(SysProps.FileDlTodayBytes, downloadBytes);
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
fileIds.forEach(fileId => {
|
|
|
|
FileEntry.incrementAndPersistMetaValue(fileId, 'dl_count', 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRecvStats(cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
let uploadBytes = 0;
|
|
|
|
let uploadCount = 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.each(this.recvFilePaths, (filePath, next) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// we just have a path - figure it out
|
2018-06-22 05:15:04 +00:00
|
|
|
fs.stat(filePath, (err, stats) => {
|
|
|
|
if(err) {
|
|
|
|
this.client.log.warn( { error : err.message, path : filePath }, 'File stat failed' );
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
uploadCount += 1;
|
2018-06-22 05:15:04 +00:00
|
|
|
uploadBytes += stats.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(null);
|
|
|
|
});
|
|
|
|
}, () => {
|
2018-11-26 02:05:16 +00:00
|
|
|
StatLog.incrementUserStat(this.client.user, UserProps.FileUlTotalCount, uploadCount);
|
|
|
|
StatLog.incrementUserStat(this.client.user, UserProps.FileUlTotalBytes, uploadBytes);
|
|
|
|
|
|
|
|
StatLog.incrementSystemStat(SysProps.FileUlTotalCount, uploadCount);
|
|
|
|
StatLog.incrementSystemStat(SysProps.FileUlTotalBytes, uploadBytes);
|
2020-11-26 22:53:21 +00:00
|
|
|
|
|
|
|
StatLog.incrementNonPersistentSystemStat(SysProps.FileUlTodayCount, uploadCount);
|
|
|
|
StatLog.incrementNonPersistentSystemStat(SysProps.FileUlTodayBytes, uploadBytes);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
initSequence() {
|
|
|
|
const self = this;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: break this up to send|recv
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function validateConfig(callback) {
|
|
|
|
if(self.isSending()) {
|
|
|
|
if(!Array.isArray(self.sendQueue)) {
|
|
|
|
self.sendQueue = [ self.sendQueue ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function transferFiles(callback) {
|
|
|
|
if(self.isSending()) {
|
|
|
|
self.sendFiles( err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sentFileIds = [];
|
|
|
|
self.sendQueue.forEach(queueItem => {
|
|
|
|
if(queueItem.sent && queueItem.fileId) {
|
|
|
|
sentFileIds.push(queueItem.fileId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(sentFileIds.length > 0) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// remove items we sent from the D/L queue
|
2018-06-22 05:15:04 +00:00
|
|
|
const dlQueue = new DownloadQueue(self.client);
|
|
|
|
const dlFileEntries = dlQueue.removeItems(sentFileIds);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// fire event for downloaded entries
|
2018-06-22 05:15:04 +00:00
|
|
|
Events.emit(
|
|
|
|
Events.getSystemEvents().UserDownload,
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
user : self.client.user,
|
|
|
|
files : dlFileEntries
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
self.sentFileIds = sentFileIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.recvFiles( err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function cleanupTempFiles(callback) {
|
|
|
|
temptmp.cleanup( paths => {
|
|
|
|
Log.debug( { paths : paths, sessionId : temptmp.sessionId }, 'Temporary files cleaned up' );
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function updateUserAndSystemStats(callback) {
|
|
|
|
if(self.isSending()) {
|
|
|
|
return self.updateSendStats(callback);
|
|
|
|
} else {
|
|
|
|
return self.updateRecvStats(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.warn( { error : err.message }, 'File transfer error');
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.prevMenu();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
};
|