2016-09-29 03:54:25 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½
|
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const sortAreasOrConfs = require('./conf_area_util.js').sortAreasOrConfs;
|
|
|
|
const FileEntry = require('./file_entry.js');
|
|
|
|
const FileDb = require('./database.js').dbs.file;
|
|
|
|
const ArchiveUtil = require('./archive_util.js');
|
|
|
|
const CRC32 = require('./crc.js').CRC32;
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const resolveMimeType = require('./mime_util.js').resolveMimeType;
|
|
|
|
const stringFormat = require('./string_format.js');
|
|
|
|
const wordWrapText = require('./word_wrap.js').wordWrapText;
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const _ = require('lodash');
|
|
|
|
const async = require('async');
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const paths = require('path');
|
|
|
|
const temptmp = require('temptmp').createTrackedSession('file_area');
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const execFile = require('child_process').execFile;
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
exports.startup = startup;
|
|
|
|
exports.isInternalArea = isInternalArea;
|
|
|
|
exports.getAvailableFileAreas = getAvailableFileAreas;
|
|
|
|
exports.getAvailableFileAreaTags = getAvailableFileAreaTags;
|
|
|
|
exports.getSortedAvailableFileAreas = getSortedAvailableFileAreas;
|
|
|
|
exports.isValidStorageTag = isValidStorageTag;
|
|
|
|
exports.getAreaStorageDirectoryByTag = getAreaStorageDirectoryByTag;
|
|
|
|
exports.getAreaDefaultStorageDirectory = getAreaDefaultStorageDirectory;
|
|
|
|
exports.getAreaStorageLocations = getAreaStorageLocations;
|
|
|
|
exports.getDefaultFileAreaTag = getDefaultFileAreaTag;
|
|
|
|
exports.getFileAreaByTag = getFileAreaByTag;
|
|
|
|
exports.getFileEntryPath = getFileEntryPath;
|
|
|
|
exports.changeFileAreaWithOptions = changeFileAreaWithOptions;
|
|
|
|
exports.scanFile = scanFile;
|
|
|
|
exports.scanFileAreaForChanges = scanFileAreaForChanges;
|
|
|
|
exports.getDescFromFileName = getDescFromFileName;
|
|
|
|
exports.getAreaStats = getAreaStats;
|
|
|
|
exports.cleanUpTempSessionItems = cleanUpTempSessionItems;
|
|
|
|
|
|
|
|
// for scheduler:
|
|
|
|
exports.updateAreaStatsScheduledEvent = updateAreaStatsScheduledEvent;
|
|
|
|
|
|
|
|
const WellKnownAreaTags = exports.WellKnownAreaTags = {
|
|
|
|
Invalid : '',
|
|
|
|
MessageAreaAttach : 'system_message_attachment',
|
|
|
|
TempDownloads : 'system_temporary_download',
|
2016-10-01 19:25:32 +00:00
|
|
|
};
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2018-03-04 04:47:04 +00:00
|
|
|
function startup(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cleanUpTempSessionItems(cb);
|
2018-03-04 04:47:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 04:53:04 +00:00
|
|
|
function isInternalArea(areaTag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return [ WellKnownAreaTags.MessageAreaAttach, WellKnownAreaTags.TempDownloads ].includes(areaTag);
|
2017-01-02 04:53:04 +00:00
|
|
|
}
|
|
|
|
|
2016-09-29 03:54:25 +00:00
|
|
|
function getAvailableFileAreas(client, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options = options || { };
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// perform ACS check per conf & omit internal if desired
|
2018-06-22 05:15:04 +00:00
|
|
|
const allAreas = _.map(Config().fileBase.areas, (areaInfo, areaTag) => Object.assign(areaInfo, { areaTag : areaTag } ));
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.omitBy(allAreas, areaInfo => {
|
|
|
|
if(!options.includeSystemInternal && isInternalArea(areaInfo.areaTag)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(options.skipAcsCheck) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return false; // no ACS checks (below)
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2017-09-11 02:51:30 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(options.writeAcs && !client.acs.hasFileAreaWrite(areaInfo)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
return true; // omit
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-12-31 21:50:29 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return !client.acs.hasFileAreaRead(areaInfo);
|
|
|
|
});
|
2016-09-29 03:54:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 16:40:30 +00:00
|
|
|
function getAvailableFileAreaTags(client, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.map(getAvailableFileAreas(client, options), area => area.areaTag);
|
2017-09-26 16:40:30 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 19:25:32 +00:00
|
|
|
function getSortedAvailableFileAreas(client, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const areas = _.map(getAvailableFileAreas(client, options), v => v);
|
|
|
|
sortAreasOrConfs(areas);
|
|
|
|
return areas;
|
2016-10-01 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 04:07:22 +00:00
|
|
|
function getDefaultFileAreaTag(client, disableAcsCheck) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
let defaultArea = _.findKey(config.fileBase, o => o.default);
|
|
|
|
if(defaultArea) {
|
|
|
|
const area = config.fileBase.areas[defaultArea];
|
|
|
|
if(true === disableAcsCheck || client.acs.hasFileAreaRead(area)) {
|
|
|
|
return defaultArea;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// just use anything we can
|
|
|
|
defaultArea = _.findKey(config.fileBase.areas, (area, areaTag) => {
|
|
|
|
return WellKnownAreaTags.MessageAreaAttach !== areaTag && (true === disableAcsCheck || client.acs.hasFileAreaRead(area));
|
|
|
|
});
|
|
|
|
|
|
|
|
return defaultArea;
|
2016-10-01 19:25:32 +00:00
|
|
|
}
|
2016-09-29 03:54:25 +00:00
|
|
|
|
|
|
|
function getFileAreaByTag(areaTag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const areaInfo = Config().fileBase.areas[areaTag];
|
|
|
|
if(areaInfo) {
|
2018-06-23 03:26:46 +00:00
|
|
|
areaInfo.areaTag = areaTag; // convienence!
|
|
|
|
areaInfo.storage = getAreaStorageLocations(areaInfo);
|
2018-06-22 05:15:04 +00:00
|
|
|
return areaInfo;
|
|
|
|
}
|
2016-10-01 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function changeFileAreaWithOptions(client, areaTag, options, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getArea(callback) {
|
|
|
|
const area = getFileAreaByTag(areaTag);
|
|
|
|
return callback(area ? null : Errors.Invalid('Invalid file areaTag'), area);
|
|
|
|
},
|
|
|
|
function validateAccess(area, callback) {
|
|
|
|
if(!client.acs.hasFileAreaRead(area)) {
|
|
|
|
return callback(Errors.AccessDenied('No access to this area'));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function changeArea(area, callback) {
|
|
|
|
if(true === options.persist) {
|
|
|
|
client.user.persistProperty('file_area_tag', areaTag, err => {
|
|
|
|
return callback(err, area);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
client.user.properties['file_area_tag'] = areaTag;
|
|
|
|
return callback(null, area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, area) => {
|
|
|
|
if(!err) {
|
|
|
|
client.log.info( { areaTag : areaTag, area : area }, 'Current file area changed');
|
|
|
|
} else {
|
|
|
|
client.log.warn( { areaTag : areaTag, area : area, error : err.message }, 'Could not change file area');
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2016-10-01 19:25:32 +00:00
|
|
|
}
|
2016-10-03 03:40:37 +00:00
|
|
|
|
2017-03-09 05:37:02 +00:00
|
|
|
function isValidStorageTag(storageTag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return storageTag in Config().fileBase.storageTags;
|
2017-03-09 05:37:02 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 01:58:56 +00:00
|
|
|
function getAreaStorageDirectoryByTag(storageTag) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
const storageLocation = (storageTag && config.fileBase.storageTags[storageTag]);
|
2016-12-07 01:58:56 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return paths.resolve(config.fileBase.areaStoragePrefix, storageLocation || '');
|
2017-01-02 04:53:04 +00:00
|
|
|
}
|
2016-12-07 01:58:56 +00:00
|
|
|
|
2017-01-02 04:53:04 +00:00
|
|
|
function getAreaDefaultStorageDirectory(areaInfo) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return getAreaStorageDirectoryByTag(areaInfo.storageTags[0]);
|
2016-12-07 01:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getAreaStorageLocations(areaInfo) {
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const storageTags = Array.isArray(areaInfo.storageTags) ?
|
|
|
|
areaInfo.storageTags :
|
|
|
|
[ areaInfo.storageTags || '' ];
|
2016-12-07 01:58:56 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const avail = Config().fileBase.storageTags;
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return _.compact(storageTags.map(storageTag => {
|
|
|
|
if(avail[storageTag]) {
|
|
|
|
return {
|
2018-06-23 03:26:46 +00:00
|
|
|
storageTag : storageTag,
|
|
|
|
dir : getAreaStorageDirectoryByTag(storageTag),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}));
|
2016-10-03 03:40:37 +00:00
|
|
|
}
|
|
|
|
|
2016-10-25 03:49:45 +00:00
|
|
|
function getFileEntryPath(fileEntry) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const areaInfo = getFileAreaByTag(fileEntry.areaTag);
|
|
|
|
if(areaInfo) {
|
|
|
|
return paths.join(areaInfo.storageDirectory, fileEntry.fileName);
|
|
|
|
}
|
2016-10-25 03:49:45 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 05:09:29 +00:00
|
|
|
function getExistingFileEntriesBySha256(sha256, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const entries = [];
|
2016-10-03 03:40:37 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
FileDb.each(
|
|
|
|
`SELECT file_id, area_tag
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM file
|
|
|
|
WHERE file_sha256=?;`,
|
2018-06-22 05:15:04 +00:00
|
|
|
[ sha256 ],
|
|
|
|
(err, fileRow) => {
|
|
|
|
if(fileRow) {
|
|
|
|
entries.push({
|
2018-06-23 03:26:46 +00:00
|
|
|
fileId : fileRow.file_id,
|
|
|
|
areaTag : fileRow.area_tag,
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, entries);
|
|
|
|
}
|
|
|
|
);
|
2016-10-03 03:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: This is bascially sliceAtEOF() from art.js .... DRY!
|
2016-10-06 05:22:59 +00:00
|
|
|
function sliceAtSauceMarker(data) {
|
2018-06-23 03:26:46 +00:00
|
|
|
let eof = data.length;
|
|
|
|
const stopPos = Math.max(data.length - (256), 0); // 256 = 2 * sizeof(SAUCE)
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
for(let i = eof - 1; i > stopPos; i--) {
|
|
|
|
if(0x1a === data[i]) {
|
|
|
|
eof = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data.slice(0, eof);
|
2016-10-06 05:22:59 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 03:03:04 +00:00
|
|
|
function attemptSetEstimatedReleaseDate(fileEntry) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: yearEstPatterns RegExp's should be cached - we can do this @ Config (re)load time
|
|
|
|
const patterns = Config().fileBase.yearEstPatterns.map( p => new RegExp(p, 'gmi'));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
function getMatch(input) {
|
|
|
|
if(input) {
|
|
|
|
let m;
|
|
|
|
for(let i = 0; i < patterns.length; ++i) {
|
|
|
|
m = patterns[i].exec(input);
|
|
|
|
if(m) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We attempt detection in short -> long order
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Throw out anything that is current_year + 2 (we give some leway)
|
|
|
|
// with the assumption that must be wrong.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const maxYear = moment().add(2, 'year').year();
|
|
|
|
const match = getMatch(fileEntry.desc) || getMatch(fileEntry.descLong);
|
|
|
|
|
|
|
|
if(match && match[1]) {
|
|
|
|
let year;
|
|
|
|
if(2 === match[1].length) {
|
|
|
|
year = parseInt(match[1]);
|
|
|
|
if(year) {
|
|
|
|
if(year > 70) {
|
|
|
|
year += 1900;
|
|
|
|
} else {
|
|
|
|
year += 2000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
year = parseInt(match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(year && year <= maxYear) {
|
|
|
|
fileEntry.meta.est_release_year = year;
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 05:22:59 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// a simple log proxy for when we call from oputil.js
|
2018-09-11 20:44:22 +00:00
|
|
|
const maybeLog = (obj, msg, level) => {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(Log) {
|
2018-09-11 20:44:22 +00:00
|
|
|
Log[level](obj, msg);
|
|
|
|
} else if ('error' === level) {
|
|
|
|
console.error(`${msg}: ${JSON.stringify(obj)}`); // eslint-disable-line no-console
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-09-11 20:44:22 +00:00
|
|
|
};
|
2017-02-09 05:53:48 +00:00
|
|
|
|
2018-09-11 20:44:22 +00:00
|
|
|
const logDebug = (obj, msg) => maybeLog(obj, msg, 'debug');
|
|
|
|
const logTrace = (obj, msg) => maybeLog(obj, msg, 'trace');
|
|
|
|
const logError = (obj, msg) => maybeLog(obj, msg, 'error');
|
2018-06-26 01:09:08 +00:00
|
|
|
|
2017-05-21 17:45:50 +00:00
|
|
|
function extractAndProcessDescFiles(fileEntry, filePath, archiveEntries, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function extractDescFiles(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: would be nice if these RegExp's were cached
|
|
|
|
// :TODO: this is long winded...
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
const extractList = [];
|
|
|
|
|
|
|
|
const shortDescFile = archiveEntries.find( e => {
|
|
|
|
return config.fileBase.fileNamePatterns.desc.find( pat => new RegExp(pat, 'i').test(e.fileName) );
|
|
|
|
});
|
|
|
|
|
|
|
|
if(shortDescFile) {
|
|
|
|
extractList.push(shortDescFile.fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
const longDescFile = archiveEntries.find( e => {
|
|
|
|
return config.fileBase.fileNamePatterns.descLong.find( pat => new RegExp(pat, 'i').test(e.fileName) );
|
|
|
|
});
|
|
|
|
|
|
|
|
if(longDescFile) {
|
|
|
|
extractList.push(longDescFile.fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(0 === extractList.length) {
|
|
|
|
return callback(null, [] );
|
|
|
|
}
|
|
|
|
|
|
|
|
temptmp.mkdir( { prefix : 'enigextract-' }, (err, tempDir) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const archiveUtil = ArchiveUtil.getInstance();
|
|
|
|
archiveUtil.extractTo(filePath, tempDir, fileEntry.meta.archive_type, extractList, err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const descFiles = {
|
2018-06-23 03:26:46 +00:00
|
|
|
desc : shortDescFile ? paths.join(tempDir, shortDescFile.fileName) : null,
|
|
|
|
descLong : longDescFile ? paths.join(tempDir, longDescFile.fileName) : null,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return callback(null, descFiles);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function readDescFiles(descFiles, callback) {
|
|
|
|
const config = Config();
|
|
|
|
async.each(Object.keys(descFiles), (descType, next) => {
|
|
|
|
const path = descFiles[descType];
|
|
|
|
if(!path) {
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.stat(path, (err, stats) => {
|
|
|
|
if(err) {
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// skip entries that are too large
|
2018-06-22 05:15:04 +00:00
|
|
|
const maxFileSizeKey = `max${_.upperFirst(descType)}FileByteSize`;
|
|
|
|
if(config.fileBase[maxFileSizeKey] && stats.size > config.fileBase[maxFileSizeKey]) {
|
|
|
|
logDebug( { byteSize : stats.size, maxByteSize : config.fileBase[maxFileSizeKey] }, `Skipping "${descType}"; Too large` );
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.readFile(path, (err, data) => {
|
|
|
|
if(err || !data) {
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Assume FILE_ID.DIZ, NFO files, etc. are CP437.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: This isn't really always the case - how to handle this? We could do a quick detection...
|
|
|
|
fileEntry[descType] = iconv.decode(sliceAtSauceMarker(data, 0x1a), 'cp437');
|
|
|
|
fileEntry[`${descType}Src`] = 'descFile';
|
2018-06-22 05:15:04 +00:00
|
|
|
return next(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, () => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// cleanup but don't wait
|
2018-06-22 05:15:04 +00:00
|
|
|
temptmp.cleanup( paths => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// note: don't use client logger here - may not be avail
|
2018-09-11 20:44:22 +00:00
|
|
|
logTrace( { paths : paths, sessionId : temptmp.sessionId }, 'Cleaned up temporary files' );
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2017-05-21 17:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function extractAndProcessSingleArchiveEntry(fileEntry, filePath, archiveEntries, cb) {
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function extractToTemp(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: we may want to skip this if the compressed file is too large...
|
2018-06-22 05:15:04 +00:00
|
|
|
temptmp.mkdir( { prefix : 'enigextract-' }, (err, tempDir) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const archiveUtil = ArchiveUtil.getInstance();
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// ensure we only extract one - there should only be one anyway -- we also just need the fileName
|
2018-06-22 05:15:04 +00:00
|
|
|
const extractList = archiveEntries.slice(0, 1).map(entry => entry.fileName);
|
|
|
|
|
|
|
|
archiveUtil.extractTo(filePath, tempDir, fileEntry.meta.archive_type, extractList, err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, paths.join(tempDir, extractList[0]));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function processSingleExtractedFile(extractedFile, callback) {
|
|
|
|
populateFileEntryInfoFromFile(fileEntry, extractedFile, err => {
|
|
|
|
if(!fileEntry.desc) {
|
2018-06-23 03:26:46 +00:00
|
|
|
fileEntry.desc = getDescFromFileName(filePath);
|
|
|
|
fileEntry.descSrc = 'fileName';
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2017-05-21 17:45:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function populateFileEntryWithArchive(fileEntry, filePath, stepInfo, iterator, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
const archiveUtil = ArchiveUtil.getInstance();
|
|
|
|
const archiveType = fileEntry.meta.archive_type; // we set this previous to populateFileEntryWithArchive()
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getArchiveFileList(callback) {
|
|
|
|
stepInfo.step = 'archive_list_start';
|
|
|
|
|
|
|
|
iterator(err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
archiveUtil.listEntries(filePath, archiveType, (err, entries) => {
|
|
|
|
if(err) {
|
|
|
|
stepInfo.step = 'archive_list_failed';
|
|
|
|
} else {
|
|
|
|
stepInfo.step = 'archive_list_finish';
|
|
|
|
stepInfo.archiveEntries = entries || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator(iterErr => {
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback( iterErr, entries || [] ); // ignore original |err| here
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function processDescFilesStart(entries, callback) {
|
|
|
|
stepInfo.step = 'desc_files_start';
|
|
|
|
iterator(err => {
|
|
|
|
return callback(err, entries);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function extractDescFromArchive(entries, callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// If we have a -single- entry in the archive, extract that file
|
|
|
|
// and try retrieving info in the non-archive manor. This should
|
|
|
|
// work for things like zipped up .pdf files.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Otherwise, try to find particular desc files such as FILE_ID.DIZ
|
|
|
|
// and README.1ST
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const archDescHandler = (1 === entries.length) ? extractAndProcessSingleArchiveEntry : extractAndProcessDescFiles;
|
|
|
|
archDescHandler(fileEntry, filePath, entries, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function attemptReleaseYearEstimation(callback) {
|
|
|
|
attemptSetEstimatedReleaseDate(fileEntry);
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function processDescFilesFinish(callback) {
|
|
|
|
stepInfo.step = 'desc_files_finish';
|
|
|
|
return iterator(callback);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2016-10-03 03:40:37 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 22:06:04 +00:00
|
|
|
function getInfoExtractUtilForDesc(mimeType, filePath, descType) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
let fileType = _.get(config, [ 'fileTypes', mimeType ] );
|
2018-06-02 22:06:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(Array.isArray(fileType)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// further refine by extention
|
2018-06-22 05:15:04 +00:00
|
|
|
fileType = fileType.find(ft => paths.extname(filePath) === ft.ext);
|
|
|
|
}
|
2018-06-02 22:06:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isObject(fileType)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-02 22:06:04 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
let util = _.get(fileType, `${descType}DescUtil`);
|
|
|
|
if(!_.isString(util)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-20 00:41:13 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
util = _.get(config, [ 'infoExtractUtils', util ]);
|
|
|
|
if(!util || !_.isString(util.cmd)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-20 00:41:13 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return util;
|
2017-05-20 00:41:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 17:45:50 +00:00
|
|
|
function populateFileEntryInfoFromFile(fileEntry, filePath, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const mimeType = resolveMimeType(filePath);
|
|
|
|
if(!mimeType) {
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.eachSeries( [ 'short', 'long' ], (descType, nextDesc) => {
|
|
|
|
const util = getInfoExtractUtilForDesc(mimeType, filePath, descType);
|
|
|
|
if(!util) {
|
|
|
|
return nextDesc(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
const args = (util.args || [ '{filePath}'] ).map( arg => stringFormat(arg, { filePath : filePath } ) );
|
|
|
|
|
|
|
|
execFile(util.cmd, args, { timeout : 1000 * 30 }, (err, stdout) => {
|
|
|
|
if(err || !stdout) {
|
|
|
|
const reason = err ? err.message : 'No description produced';
|
|
|
|
logDebug(
|
|
|
|
{ reason : reason, cmd : util.cmd, args : args },
|
|
|
|
`${_.upperFirst(descType)} description command failed`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
stdout = (stdout || '').trim();
|
|
|
|
if(stdout.length > 0) {
|
|
|
|
const key = 'short' === descType ? 'desc' : 'descLong';
|
|
|
|
if('desc' === key) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Word wrap short descriptions to FILE_ID.DIZ spec
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// "...no more than 45 characters long"
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// See http://www.textfiles.com/computers/fileid.txt
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
stdout = (wordWrapText( stdout, { width : 45 } ).wrapped || []).join('\n');
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
fileEntry[key] = stdout;
|
|
|
|
fileEntry[`${key}Src`] = 'infoTool';
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextDesc(null);
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
return cb(null);
|
|
|
|
});
|
2017-05-21 17:45:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 05:09:29 +00:00
|
|
|
function populateFileEntryNonArchive(fileEntry, filePath, stepInfo, iterator, cb) {
|
2017-05-14 02:59:42 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function processDescFilesStart(callback) {
|
|
|
|
stepInfo.step = 'desc_files_start';
|
|
|
|
return iterator(callback);
|
|
|
|
},
|
|
|
|
function getDescriptions(callback) {
|
|
|
|
populateFileEntryInfoFromFile(fileEntry, filePath, err => {
|
|
|
|
if(!fileEntry.desc) {
|
2018-06-23 03:26:46 +00:00
|
|
|
fileEntry.desc = getDescFromFileName(filePath);
|
|
|
|
fileEntry.descSrc = 'fileName';
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function processDescFilesFinish(callback) {
|
|
|
|
stepInfo.step = 'desc_files_finish';
|
|
|
|
return iterator(callback);
|
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2016-10-07 03:03:04 +00:00
|
|
|
}
|
|
|
|
|
2016-10-03 03:40:37 +00:00
|
|
|
function addNewFileEntry(fileEntry, filePath, cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Use detectTypeWithBuf() once avail - we *just* read some file data
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function addNewDbRecord(callback) {
|
|
|
|
return fileEntry.persist(callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
2017-01-12 05:51:00 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 05:09:29 +00:00
|
|
|
const HASH_NAMES = [ 'sha1', 'sha256', 'md5', 'crc32' ];
|
|
|
|
|
|
|
|
function scanFile(filePath, options, iterator, cb) {
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(3 === arguments.length && _.isFunction(iterator)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
cb = iterator;
|
|
|
|
iterator = null;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else if(2 === arguments.length && _.isFunction(options)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
cb = options;
|
|
|
|
iterator = null;
|
|
|
|
options = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const fileEntry = new FileEntry({
|
2018-06-23 03:26:46 +00:00
|
|
|
areaTag : options.areaTag,
|
|
|
|
meta : options.meta,
|
|
|
|
hashTags : options.hashTags, // Set() or Array
|
|
|
|
fileName : paths.basename(filePath),
|
|
|
|
storageTag : options.storageTag,
|
|
|
|
fileSha256 : options.sha256, // caller may know this already
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const stepInfo = {
|
2018-06-23 03:26:46 +00:00
|
|
|
filePath : filePath,
|
|
|
|
fileName : paths.basename(filePath),
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const callIter = (next) => {
|
|
|
|
return iterator ? iterator(stepInfo, next) : next(null);
|
|
|
|
};
|
|
|
|
|
|
|
|
const readErrorCallIter = (origError, next) => {
|
2018-06-23 03:26:46 +00:00
|
|
|
stepInfo.step = 'read_error';
|
|
|
|
stepInfo.error = origError.message;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
callIter( () => {
|
|
|
|
return next(origError);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
let lastCalcHashPercent;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// don't re-calc hashes for any we already have in |options|
|
2018-06-22 05:15:04 +00:00
|
|
|
const hashesToCalc = HASH_NAMES.filter(hn => {
|
|
|
|
if('sha256' === hn && fileEntry.fileSha256) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(`file_${hn}` in fileEntry.meta) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function startScan(callback) {
|
|
|
|
fs.stat(filePath, (err, stats) => {
|
|
|
|
if(err) {
|
|
|
|
return readErrorCallIter(err, callback);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
stepInfo.step = 'start';
|
|
|
|
stepInfo.byteSize = fileEntry.meta.byte_size = stats.size;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
return callIter(callback);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function processPhysicalFileGeneric(callback) {
|
|
|
|
stepInfo.bytesProcessed = 0;
|
|
|
|
|
|
|
|
const hashes = {};
|
|
|
|
hashesToCalc.forEach(hashName => {
|
|
|
|
if('crc32' === hashName) {
|
|
|
|
hashes.crc32 = new CRC32;
|
|
|
|
} else {
|
|
|
|
hashes[hashName] = crypto.createHash(hashName);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const updateHashes = (data) => {
|
|
|
|
for(let i = 0; i < hashesToCalc.length; ++i) {
|
|
|
|
hashes[hashesToCalc[i]].update(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Note that we are not using fs.createReadStream() here:
|
|
|
|
// While convenient, it is quite a bit slower -- which adds
|
|
|
|
// up to many seconds in time for larger files.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const chunkSize = 1024 * 64;
|
|
|
|
const buffer = new Buffer(chunkSize);
|
|
|
|
|
|
|
|
fs.open(filePath, 'r', (err, fd) => {
|
|
|
|
if(err) {
|
|
|
|
return readErrorCallIter(err, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextChunk = () => {
|
|
|
|
fs.read(fd, buffer, 0, chunkSize, null, (err, bytesRead) => {
|
|
|
|
if(err) {
|
2018-06-26 01:09:08 +00:00
|
|
|
return fs.close(fd, closeErr => {
|
|
|
|
if(closeErr) {
|
|
|
|
logError( { filePath, error : err.message }, 'Failed to close file');
|
|
|
|
}
|
|
|
|
return readErrorCallIter(err, callback);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(0 === bytesRead) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// done - finalize
|
2018-06-22 05:15:04 +00:00
|
|
|
fileEntry.meta.byte_size = stepInfo.bytesProcessed;
|
|
|
|
|
|
|
|
for(let i = 0; i < hashesToCalc.length; ++i) {
|
|
|
|
const hashName = hashesToCalc[i];
|
|
|
|
if('sha256' === hashName) {
|
|
|
|
stepInfo.sha256 = fileEntry.fileSha256 = hashes.sha256.digest('hex');
|
|
|
|
} else if('sha1' === hashName || 'md5' === hashName) {
|
|
|
|
stepInfo[hashName] = fileEntry.meta[`file_${hashName}`] = hashes[hashName].digest('hex');
|
|
|
|
} else if('crc32' === hashName) {
|
|
|
|
stepInfo.crc32 = fileEntry.meta.file_crc32 = hashes.crc32.finalize().toString(16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stepInfo.step = 'hash_finish';
|
2018-06-26 01:09:08 +00:00
|
|
|
return fs.close(fd, closeErr => {
|
|
|
|
if(closeErr) {
|
|
|
|
logError( { filePath, error : err.message }, 'Failed to close file');
|
|
|
|
}
|
|
|
|
return callIter(callback);
|
|
|
|
});
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
stepInfo.bytesProcessed += bytesRead;
|
|
|
|
stepInfo.calcHashPercent = Math.round(((stepInfo.bytesProcessed / stepInfo.byteSize) * 100));
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Only send 'hash_update' step update if we have a noticable percentage change in progress
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const data = bytesRead < chunkSize ? buffer.slice(0, bytesRead) : buffer;
|
|
|
|
if(!iterator || stepInfo.calcHashPercent === lastCalcHashPercent) {
|
|
|
|
updateHashes(data);
|
|
|
|
return nextChunk();
|
|
|
|
} else {
|
|
|
|
lastCalcHashPercent = stepInfo.calcHashPercent;
|
2018-06-23 03:26:46 +00:00
|
|
|
stepInfo.step = 'hash_update';
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
callIter(err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateHashes(data);
|
|
|
|
return nextChunk();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
nextChunk();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function processPhysicalFileByType(callback) {
|
|
|
|
const archiveUtil = ArchiveUtil.getInstance();
|
|
|
|
|
|
|
|
archiveUtil.detectType(filePath, (err, archiveType) => {
|
|
|
|
if(archiveType) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// save this off
|
2018-06-22 05:15:04 +00:00
|
|
|
fileEntry.meta.archive_type = archiveType;
|
|
|
|
|
|
|
|
populateFileEntryWithArchive(fileEntry, filePath, stepInfo, callIter, err => {
|
|
|
|
if(err) {
|
|
|
|
populateFileEntryNonArchive(fileEntry, filePath, stepInfo, callIter, err => {
|
|
|
|
if(err) {
|
|
|
|
logDebug( { error : err.message }, 'Non-archive file entry population failed');
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
populateFileEntryNonArchive(fileEntry, filePath, stepInfo, callIter, err => {
|
|
|
|
if(err) {
|
|
|
|
logDebug( { error : err.message }, 'Non-archive file entry population failed');
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(null); // ignore err
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function fetchExistingEntry(callback) {
|
|
|
|
getExistingFileEntriesBySha256(fileEntry.fileSha256, (err, dupeEntries) => {
|
|
|
|
return callback(err, dupeEntries);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function finished(dupeEntries, callback) {
|
|
|
|
stepInfo.step = 'finished';
|
|
|
|
callIter( () => {
|
|
|
|
return callback(null, dupeEntries);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, dupeEntries) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, fileEntry, dupeEntries);
|
|
|
|
}
|
|
|
|
);
|
2016-10-03 03:40:37 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 05:51:20 +00:00
|
|
|
function scanFileAreaForChanges(areaInfo, options, iterator, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(3 === arguments.length && _.isFunction(iterator)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
cb = iterator;
|
|
|
|
iterator = null;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else if(2 === arguments.length && _.isFunction(options)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
cb = options;
|
|
|
|
iterator = null;
|
|
|
|
options = {};
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const storageLocations = getAreaStorageLocations(areaInfo);
|
|
|
|
|
|
|
|
async.eachSeries(storageLocations, (storageLoc, nextLocation) => {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function scanPhysFiles(callback) {
|
|
|
|
const physDir = storageLoc.dir;
|
|
|
|
|
|
|
|
fs.readdir(physDir, (err, files) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.eachSeries(files, (fileName, nextFile) => {
|
|
|
|
const fullPath = paths.join(physDir, fileName);
|
|
|
|
|
|
|
|
fs.stat(fullPath, (err, stats) => {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Log me!
|
|
|
|
return nextFile(null); // always try next file
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!stats.isFile()) {
|
|
|
|
return nextFile(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
scanFile(
|
|
|
|
fullPath,
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
areaTag : areaInfo.areaTag,
|
|
|
|
storageTag : storageLoc.storageTag
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
iterator,
|
|
|
|
(err, fileEntry, dupeEntries) => {
|
|
|
|
if(err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Log me!!!
|
|
|
|
return nextFile(null); // try next anyway
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(dupeEntries.length > 0) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Handle duplidates -- what to do here???
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
if(Array.isArray(options.tags)) {
|
|
|
|
options.tags.forEach(tag => {
|
|
|
|
fileEntry.hashTags.add(tag);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
addNewFileEntry(fileEntry, fullPath, err => {
|
2018-06-23 03:26:46 +00:00
|
|
|
// pass along error; we failed to insert a record in our DB or something else bad
|
2018-06-22 05:15:04 +00:00
|
|
|
return nextFile(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function scanDbEntries(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Look @ db entries for area that were *not* processed above
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return nextLocation(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
2016-12-07 01:58:56 +00:00
|
|
|
}
|
2017-02-25 06:39:31 +00:00
|
|
|
|
|
|
|
function getDescFromFileName(fileName) {
|
2018-06-30 19:03:08 +00:00
|
|
|
//
|
|
|
|
// Example filenames:
|
|
|
|
//
|
|
|
|
// input desired output
|
|
|
|
// -----------------------------------------------------------------------------------------
|
|
|
|
// Nintendo_Power_Issue_011_March-April_1990.cbr Nintendo Power Issue 011 March-April 1990
|
|
|
|
// Atari User Issue 3 (July 1985).pdf Atari User Issue 3 (July 1985)
|
|
|
|
// Out_Of_The_Shadows_010__1953_.cbz Out Of The Shadows 010 1953
|
|
|
|
// ABC A Basic Compiler 1.03 [pro].atr ABC A Basic Compiler 1.03 [pro]
|
|
|
|
// 221B Baker Street v1.0 (1987)(Datasoft)(Side B)[cr The Bounty].zip 221B Baker Street v1.0 (1987)(Datasoft)(Side B)[cr the Bounty]
|
|
|
|
//
|
|
|
|
// See also:
|
|
|
|
// * https://scenerules.org/
|
|
|
|
//
|
|
|
|
|
|
|
|
const ext = paths.extname(fileName);
|
|
|
|
const name = paths.basename(fileName, ext);
|
|
|
|
const asIsRe = /([vV]?(?:[0-9]{1,4})(?:\.[0-9]{1,4})+[-+]?(?:[a-z]{1,4})?)|(Incl\.)|(READ\.NFO)/g;
|
|
|
|
|
|
|
|
const normalize = (s) => {
|
|
|
|
return _.upperFirst(s.replace(/[-_.+]/g, ' ').replace(/\s+/g, ' '));
|
|
|
|
};
|
|
|
|
|
|
|
|
let out = '';
|
|
|
|
let m;
|
|
|
|
let pos;
|
|
|
|
do {
|
|
|
|
pos = asIsRe.lastIndex;
|
|
|
|
m = asIsRe.exec(name);
|
|
|
|
if(m) {
|
|
|
|
if(m.index > pos) {
|
|
|
|
out += normalize(name.slice(pos, m.index));
|
|
|
|
}
|
|
|
|
out += m[0]; // as-is
|
|
|
|
}
|
|
|
|
} while(0 != asIsRe.lastIndex);
|
|
|
|
|
|
|
|
if(pos < name.length) {
|
|
|
|
out += normalize(name.slice(pos));
|
|
|
|
}
|
2017-02-25 06:39:31 +00:00
|
|
|
|
2018-06-30 19:03:08 +00:00
|
|
|
return out;
|
2017-09-09 17:48:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return an object of stats about an area(s)
|
2017-09-09 17:48:43 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// {
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// totalFiles : <totalFileCount>,
|
|
|
|
// totalBytes : <totalByteSize>,
|
|
|
|
// areas : {
|
|
|
|
// <areaTag> : {
|
|
|
|
// files : <fileCount>,
|
|
|
|
// bytes : <byteSize>
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2017-09-09 17:48:43 +00:00
|
|
|
//
|
2018-01-15 19:22:11 +00:00
|
|
|
function getAreaStats(cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
FileDb.all(
|
|
|
|
`SELECT DISTINCT f.area_tag, COUNT(f.file_id) AS total_files, SUM(m.meta_value) AS total_byte_size
|
2018-06-23 03:26:46 +00:00
|
|
|
FROM file f, file_meta m
|
|
|
|
WHERE f.file_id = m.file_id AND m.meta_name='byte_size'
|
|
|
|
GROUP BY f.area_tag;`,
|
2018-06-22 05:15:04 +00:00
|
|
|
(err, statRows) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!statRows || 0 === statRows.length) {
|
|
|
|
return cb(Errors.DoesNotExist('No file areas to acquire stats from'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(
|
|
|
|
null,
|
|
|
|
statRows.reduce( (stats, v) => {
|
|
|
|
stats.totalFiles = (stats.totalFiles || 0) + v.total_files;
|
|
|
|
stats.totalBytes = (stats.totalBytes || 0) + v.total_byte_size;
|
|
|
|
|
|
|
|
stats.areas = stats.areas || {};
|
|
|
|
|
|
|
|
stats.areas[v.area_tag] = {
|
2018-06-23 03:26:46 +00:00
|
|
|
files : v.total_files,
|
|
|
|
bytes : v.total_byte_size,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
return stats;
|
|
|
|
}, {})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2017-09-09 17:48:43 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// method exposed for event scheduler
|
2017-09-09 17:48:43 +00:00
|
|
|
function updateAreaStatsScheduledEvent(args, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
getAreaStats( (err, stats) => {
|
|
|
|
if(!err) {
|
|
|
|
StatLog.setNonPeristentSystemStat('file_base_area_stats', stats);
|
|
|
|
}
|
2017-09-09 17:48:43 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(err);
|
|
|
|
});
|
2018-03-04 04:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function cleanUpTempSessionItems(cb) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// find (old) temporary session items and nuke 'em
|
2018-06-22 05:15:04 +00:00
|
|
|
const filter = {
|
2018-06-23 03:26:46 +00:00
|
|
|
areaTag : WellKnownAreaTags.TempDownloads,
|
|
|
|
metaPairs : [
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
name : 'session_temp_dl',
|
|
|
|
value : 1
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
|
|
|
FileEntry.findFiles(filter, (err, fileIds) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.each(fileIds, (fileId, nextFileId) => {
|
|
|
|
const fileEntry = new FileEntry();
|
|
|
|
fileEntry.load(fileId, err => {
|
|
|
|
if(err) {
|
|
|
|
Log.warn( { fileId }, 'Failed loading temporary session download item for cleanup');
|
|
|
|
return nextFileId(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileEntry.removeEntry(fileEntry, { removePhysFile : true }, err => {
|
|
|
|
if(err) {
|
|
|
|
Log.warn( { fileId : fileEntry.fileId, filePath : fileEntry.filePath }, 'Failed to clean up temporary session download item');
|
|
|
|
}
|
|
|
|
return nextFileId(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
return cb(null);
|
|
|
|
});
|
|
|
|
});
|
2017-02-25 06:39:31 +00:00
|
|
|
}
|