Work on Events missed files (see prev)

This commit is contained in:
Bryan Ashby 2018-06-03 17:02:28 -06:00
parent b273101b61
commit c142a9c3d3
8 changed files with 68 additions and 37 deletions

View File

@ -77,7 +77,10 @@ function addNewClient(client, clientSock) {
client.log.info(connInfo, 'Client connected'); client.log.info(connInfo, 'Client connected');
Events.emit('codes.l33t.enigma.system.connected', { client : client, connectionCount : clientConnections.length } ); Events.emit(
Events.getSystemEvents().ClientConnected,
{ client : client, connectionCount : clientConnections.length }
);
return id; return id;
} }
@ -97,7 +100,10 @@ function removeClient(client) {
'Client disconnected' 'Client disconnected'
); );
Events.emit('codes.l33t.enigma.system.disconnected', { client : client, connectionCount : clientConnections.length } ); Events.emit(
Events.getSystemEvents().ClientDisconnected,
{ client : client, connectionCount : clientConnections.length }
);
} }
} }

View File

@ -177,7 +177,7 @@ function connectEntry(client, nextMenu) {
displayBanner(term); displayBanner(term);
// fire event // fire event
Events.emit('codes.l33t.enigma.system.term_detected', { client : client } ); Events.emit(Events.getSystemEvents().TermDetected, { client : client } );
setTimeout( () => { setTimeout( () => {
return client.menuStack.goto(nextMenu); return client.menuStack.goto(nextMenu);

View File

@ -3,6 +3,9 @@
const FileEntry = require('./file_entry.js'); const FileEntry = require('./file_entry.js');
// deps
const { partition } = require('lodash');
module.exports = class DownloadQueue { module.exports = class DownloadQueue {
constructor(client) { constructor(client) {
this.client = client; this.client = client;
@ -24,21 +27,22 @@ module.exports = class DownloadQueue {
this.client.user.downloadQueue = []; this.client.user.downloadQueue = [];
} }
toggle(fileEntry) { toggle(fileEntry, systemFile=false) {
if(this.isQueued(fileEntry)) { if(this.isQueued(fileEntry)) {
this.client.user.downloadQueue = this.client.user.downloadQueue.filter(e => fileEntry.fileId !== e.fileId); this.client.user.downloadQueue = this.client.user.downloadQueue.filter(e => fileEntry.fileId !== e.fileId);
} else { } else {
this.add(fileEntry); this.add(fileEntry, systemFile);
} }
} }
add(fileEntry) { add(fileEntry, systemFile=false) {
this.client.user.downloadQueue.push({ this.client.user.downloadQueue.push({
fileId : fileEntry.fileId, fileId : fileEntry.fileId,
areaTag : fileEntry.areaTag, areaTag : fileEntry.areaTag,
fileName : fileEntry.fileName, fileName : fileEntry.fileName,
path : fileEntry.filePath, path : fileEntry.filePath,
byteSize : fileEntry.meta.byte_size || 0, byteSize : fileEntry.meta.byte_size || 0,
systemFile : systemFile,
}); });
} }
@ -47,7 +51,9 @@ module.exports = class DownloadQueue {
fileIds = [ fileIds ]; fileIds = [ fileIds ];
} }
this.client.user.downloadQueue = this.client.user.downloadQueue.filter(e => ( -1 === fileIds.indexOf(e.fileId) ) ); const [ remain, removed ] = partition(this.client.user.downloadQueue, e => ( -1 === fileIds.indexOf(e.fileId) ));
this.client.user.downloadQueue = remain;
return removed;
} }
isQueued(entryOrId) { isQueued(entryOrId) {

View File

@ -4,25 +4,20 @@
const paths = require('path'); const paths = require('path');
const events = require('events'); const events = require('events');
const Log = require('./logger.js').log; const Log = require('./logger.js').log;
const SystemEvents = require('./system_events.js');
// deps // deps
const _ = require('lodash'); const _ = require('lodash');
const async = require('async'); const async = require('async');
const glob = require('glob'); const glob = require('glob');
const SYSTEM_EVENTS = {
ClientConnected : 'codes.l33t.enigma.system.connected',
ClientDisconnected : 'codes.l33t.enigma.system.disconnected',
TermDetected : 'codes.l33t.enigma.term_detected',
};
module.exports = new class Events extends events.EventEmitter { module.exports = new class Events extends events.EventEmitter {
constructor() { constructor() {
super(); super();
} }
getSystemEvents() { getSystemEvents() {
return SYSTEM_EVENTS; return SystemEvents;
} }
addListener(event, listener) { addListener(event, listener) {

View File

@ -14,6 +14,7 @@ const User = require('./user.js');
const Log = require('./logger.js').log; const Log = require('./logger.js').log;
const getConnectionByUserId = require('./client_connections.js').getConnectionByUserId; const getConnectionByUserId = require('./client_connections.js').getConnectionByUserId;
const webServerPackageName = require('./servers/content/web.js').moduleInfo.packageName; const webServerPackageName = require('./servers/content/web.js').moduleInfo.packageName;
const Events = require('./events.js');
// deps // deps
const hashids = require('hashids'); const hashids = require('hashids');
@ -337,7 +338,7 @@ class FileAreaWebAccess {
resp.on('finish', () => { resp.on('finish', () => {
// transfer completed fully // transfer completed fully
this.updateDownloadStatsForUserIdAndSystem(servedItem.userId, stats.size); this.updateDownloadStatsForUserIdAndSystem(servedItem.userId, stats.size, [ fileEntry ]);
}); });
const headers = { const headers = {
@ -382,24 +383,21 @@ class FileAreaWebAccess {
); );
}, },
function loadFileEntries(fileIds, callback) { function loadFileEntries(fileIds, callback) {
const filePaths = []; async.map(fileIds, (fileId, nextFileId) => {
async.eachSeries(fileIds, (fileId, nextFileId) => {
const fileEntry = new FileEntry(); const fileEntry = new FileEntry();
fileEntry.load(fileId, err => { fileEntry.load(fileId, err => {
if(!err) { return nextFileId(err, fileEntry);
filePaths.push(fileEntry.filePath);
}
return nextFileId(err);
}); });
}, err => { }, (err, fileEntries) => {
if(err) { if(err) {
return callback(Errors.DoesNotExist('Coudl not load file IDs for batch')); return callback(Errors.DoesNotExist('Could not load file IDs for batch'));
} }
return callback(null, filePaths); return callback(null, fileEntries);
}); });
}, },
function createAndServeStream(filePaths, callback) { function createAndServeStream(fileEntries, callback) {
const filePaths = fileEntries.map(fe => fe.filePath);
Log.trace( { filePaths : filePaths }, 'Creating zip archive for batch web request'); Log.trace( { filePaths : filePaths }, 'Creating zip archive for batch web request');
const zipFile = new yazl.ZipFile(); const zipFile = new yazl.ZipFile();
@ -430,7 +428,7 @@ class FileAreaWebAccess {
resp.on('finish', () => { resp.on('finish', () => {
// transfer completed fully // transfer completed fully
self.updateDownloadStatsForUserIdAndSystem(servedItem.userId, finalZipSize); self.updateDownloadStatsForUserIdAndSystem(servedItem.userId, finalZipSize, fileEntries);
}); });
const batchFileName = `batch_${servedItem.hashId}.zip`; const batchFileName = `batch_${servedItem.hashId}.zip`;
@ -457,7 +455,7 @@ class FileAreaWebAccess {
); );
} }
updateDownloadStatsForUserIdAndSystem(userId, dlBytes, cb) { updateDownloadStatsForUserIdAndSystem(userId, dlBytes, fileEntries) {
async.waterfall( async.waterfall(
[ [
function fetchActiveUser(callback) { function fetchActiveUser(callback) {
@ -477,14 +475,19 @@ class FileAreaWebAccess {
StatLog.incrementSystemStat('dl_total_count', 1); StatLog.incrementSystemStat('dl_total_count', 1);
StatLog.incrementSystemStat('dl_total_bytes', dlBytes); StatLog.incrementSystemStat('dl_total_bytes', dlBytes);
return callback(null, user);
},
function sendEvent(user, callback) {
Events.emit(
Events.getSystemEvents().UserDownload,
{
user : user,
files : fileEntries,
}
);
return callback(null); return callback(null);
} }
], ]
err => {
if(cb) {
return cb(err);
}
}
); );
} }
} }

View File

@ -223,7 +223,7 @@ exports.getModule = class FileBaseListExport extends MenuModule {
if(!err) { if(!err) {
// queue it! // queue it!
const dlQueue = new DownloadQueue(self.client); const dlQueue = new DownloadQueue(self.client);
dlQueue.add(newEntry); dlQueue.add(newEntry, true); // true=systemFile
// clean up after ourselves when the session ends // clean up after ourselves when the session ends
const thisClientId = self.client.session.id; const thisClientId = self.client.session.id;

View File

@ -10,6 +10,7 @@ const DownloadQueue = require('./download_queue.js');
const StatLog = require('./stat_log.js'); const StatLog = require('./stat_log.js');
const FileEntry = require('./file_entry.js'); const FileEntry = require('./file_entry.js');
const Log = require('./logger.js').log; const Log = require('./logger.js').log;
const Events = require('./events.js');
// deps // deps
const async = require('async'); const async = require('async');
@ -545,7 +546,16 @@ exports.getModule = class TransferFileModule extends MenuModule {
if(sentFileIds.length > 0) { if(sentFileIds.length > 0) {
// remove items we sent from the D/L queue // remove items we sent from the D/L queue
const dlQueue = new DownloadQueue(self.client); const dlQueue = new DownloadQueue(self.client);
dlQueue.removeItems(sentFileIds); const dlFileEntries = dlQueue.removeItems(sentFileIds);
// fire event for downloaded entries
Events.emit(
Events.getSystemEvents().UserDownload,
{
user : self.client.user,
files : dlFileEntries
}
);
self.sentFileIds = sentFileIds; self.sentFileIds = sentFileIds;
} }

View File

@ -16,6 +16,7 @@ const Log = require('./logger.js').log;
const Errors = require('./enig_error.js').Errors; const Errors = require('./enig_error.js').Errors;
const FileEntry = require('./file_entry.js'); const FileEntry = require('./file_entry.js');
const isAnsi = require('./string_util.js').isAnsi; const isAnsi = require('./string_util.js').isAnsi;
const Events = require('./events.js');
// deps // deps
const async = require('async'); const async = require('async');
@ -567,8 +568,18 @@ exports.getModule = class UploadModule extends MenuModule {
// here as I/O can take quite a bit of time. Log any failures. // here as I/O can take quite a bit of time. Log any failures.
// //
self.moveAndPersistUploadsToDatabase(scanResults.newEntries); self.moveAndPersistUploadsToDatabase(scanResults.newEntries);
return callback(null); return callback(null, scanResults.newEntries);
}, },
function sendEvent(uploadedEntries, callback) {
Events.emit(
Events.getSystemEvents().UserUpload,
{
user : self.client.user,
files : uploadedEntries,
}
);
return callback(null);
}
], ],
err => { err => {
if(err) { if(err) {