2017-02-16 03:27:16 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const printUsageAndSetExitCode = require('./oputil_common.js').printUsageAndSetExitCode;
|
|
|
|
const ExitCodes = require('./oputil_common.js').ExitCodes;
|
|
|
|
const argv = require('./oputil_common.js').argv;
|
|
|
|
const initConfigAndDatabases = require('./oputil_common.js').initConfigAndDatabases;
|
|
|
|
const getHelpFor = require('./oputil_help.js').getHelpFor;
|
|
|
|
const getAreaAndStorage = require('./oputil_common.js').getAreaAndStorage;
|
2017-02-19 02:00:09 +00:00
|
|
|
const Errors = require('../../core/enig_error.js').Errors;
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
const async = require('async');
|
2017-05-20 03:20:19 +00:00
|
|
|
const fs = require('graceful-fs');
|
2017-02-16 03:27:16 +00:00
|
|
|
const paths = require('path');
|
2017-02-19 02:00:09 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
2017-02-25 06:39:31 +00:00
|
|
|
const inq = require('inquirer');
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
exports.handleFileBaseCommand = handleFileBaseCommand;
|
|
|
|
|
|
|
|
/*
|
|
|
|
:TODO:
|
|
|
|
|
|
|
|
Global options:
|
|
|
|
--yes: assume yes
|
|
|
|
--no-prompt: try to avoid user input
|
|
|
|
|
|
|
|
Prompt for import and description before scan
|
|
|
|
* Only after finding duplicate-by-path
|
|
|
|
* Default to filename -> desc if auto import
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
let fileArea; // required during init
|
|
|
|
|
2017-09-09 05:11:01 +00:00
|
|
|
function finalizeEntryAndPersist(isUpdate, fileEntry, descHandler, cb) {
|
2017-02-25 06:39:31 +00:00
|
|
|
async.series(
|
|
|
|
[
|
2017-08-25 02:22:50 +00:00
|
|
|
function getDescFromHandlerIfNeeded(callback) {
|
|
|
|
if((fileEntry.desc && fileEntry.desc.length > 0 ) && !argv['desc-file']) {
|
|
|
|
return callback(null); // we have a desc already and are NOT overriding with desc file
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!descHandler) {
|
|
|
|
return callback(null); // not much we can do!
|
|
|
|
}
|
|
|
|
|
|
|
|
const desc = descHandler.getDescription(fileEntry.fileName);
|
|
|
|
if(desc) {
|
|
|
|
fileEntry.desc = desc;
|
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function getDescFromUserIfNeeded(callback) {
|
2017-09-09 05:11:01 +00:00
|
|
|
if(fileEntry.desc && fileEntry.desc.length > 0 ) {
|
2017-02-25 06:39:31 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
2017-09-09 05:11:01 +00:00
|
|
|
const getDescFromFileName = require('../../core/file_base_area.js').getDescFromFileName;
|
|
|
|
const descFromFile = getDescFromFileName(fileEntry.fileName);
|
|
|
|
|
|
|
|
if(false === argv.prompt) {
|
|
|
|
fileEntry.desc = descFromFile;
|
|
|
|
return callback(null);
|
|
|
|
}
|
2017-03-14 01:54:22 +00:00
|
|
|
|
2017-02-25 06:39:31 +00:00
|
|
|
const questions = [
|
|
|
|
{
|
|
|
|
name : 'desc',
|
|
|
|
message : `Description for ${fileEntry.fileName}:`,
|
|
|
|
type : 'input',
|
2017-09-09 05:11:01 +00:00
|
|
|
default : descFromFile,
|
2017-02-25 06:39:31 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
inq.prompt(questions).then( answers => {
|
|
|
|
fileEntry.desc = answers.desc;
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function persist(callback) {
|
2017-09-09 05:11:01 +00:00
|
|
|
fileEntry.persist(isUpdate, err => {
|
2017-02-25 06:39:31 +00:00
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-30 02:10:34 +00:00
|
|
|
const SCAN_EXCLUDE_FILENAMES = [ 'DESCRIPT.ION', 'FILES.BBS' ];
|
2017-08-25 02:22:50 +00:00
|
|
|
|
|
|
|
function loadDescHandler(path, cb) {
|
|
|
|
const DescIon = require('../../core/descript_ion_file.js');
|
|
|
|
|
|
|
|
// :TODO: support FILES.BBS also
|
|
|
|
|
|
|
|
DescIon.createFromFile(path, (err, descHandler) => {
|
|
|
|
return cb(err, descHandler);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
function scanFileAreaForChanges(areaInfo, options, cb) {
|
|
|
|
|
|
|
|
const storageLocations = fileArea.getAreaStorageLocations(areaInfo).filter(sl => {
|
|
|
|
return options.areaAndStorageInfo.find(asi => {
|
|
|
|
return !asi.storageTag || sl.storageTag === asi.storageTag;
|
|
|
|
});
|
|
|
|
});
|
2017-09-09 05:11:01 +00:00
|
|
|
|
|
|
|
function updateTags(fe) {
|
|
|
|
if(Array.isArray(options.tags)) {
|
|
|
|
fe.hashTags = new Set(options.tags);
|
|
|
|
}
|
|
|
|
}
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
async.eachSeries(storageLocations, (storageLoc, nextLocation) => {
|
2017-08-25 02:22:50 +00:00
|
|
|
async.waterfall(
|
2017-02-16 03:27:16 +00:00
|
|
|
[
|
2017-08-25 02:22:50 +00:00
|
|
|
function initDescFile(callback) {
|
|
|
|
if(options.descFileHandler) {
|
|
|
|
return callback(null, options.descFileHandler); // we're going to use the global handler
|
|
|
|
}
|
|
|
|
|
|
|
|
loadDescHandler(paths.join(storageLoc.dir, 'DESCRIPT.ION'), (err, descHandler) => {
|
|
|
|
return callback(null, descHandler);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function scanPhysFiles(descHandler, callback) {
|
2017-02-16 03:27:16 +00:00
|
|
|
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);
|
|
|
|
|
2017-08-30 02:10:34 +00:00
|
|
|
if(SCAN_EXCLUDE_FILENAMES.includes(fileName.toUpperCase())) {
|
|
|
|
console.info(`Excluding ${fullPath}`);
|
2017-08-25 02:22:50 +00:00
|
|
|
return nextFile(null);
|
|
|
|
}
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
fs.stat(fullPath, (err, stats) => {
|
|
|
|
if(err) {
|
|
|
|
// :TODO: Log me!
|
|
|
|
return nextFile(null); // always try next file
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!stats.isFile()) {
|
|
|
|
return nextFile(null);
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:31:01 +00:00
|
|
|
process.stdout.write(`Scanning ${fullPath}... `);
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
fileArea.scanFile(
|
|
|
|
fullPath,
|
|
|
|
{
|
|
|
|
areaTag : areaInfo.areaTag,
|
|
|
|
storageTag : storageLoc.storageTag
|
|
|
|
},
|
|
|
|
(err, fileEntry, dupeEntries) => {
|
|
|
|
if(err) {
|
|
|
|
console.info(`Error: ${err.message}`);
|
|
|
|
return nextFile(null); // try next anyway
|
2017-09-09 05:11:01 +00:00
|
|
|
}
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2017-09-09 05:11:01 +00:00
|
|
|
//
|
|
|
|
// We'll update the entry if the following conditions are met:
|
|
|
|
// * We have a single duplicate, and:
|
2017-11-18 21:09:17 +00:00
|
|
|
// * --update was passed or the existing entry's desc,
|
2017-11-18 21:14:19 +00:00
|
|
|
// longDesc, or est_release_year meta are blank/empty
|
2017-09-09 05:11:01 +00:00
|
|
|
//
|
2017-09-30 18:34:10 +00:00
|
|
|
if(argv.update && 1 === dupeEntries.length) {
|
2017-09-09 05:11:01 +00:00
|
|
|
const FileEntry = require('../../core/file_entry.js');
|
|
|
|
const existingEntry = new FileEntry();
|
|
|
|
|
|
|
|
return existingEntry.load(dupeEntries[0].fileId, err => {
|
|
|
|
if(err) {
|
|
|
|
console.info('Dupe (cannot update)');
|
|
|
|
return nextFile(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Update only if tags or desc changed
|
|
|
|
//
|
|
|
|
const optTags = Array.isArray(options.tags) ? new Set(options.tags) : existingEntry.hashTags;
|
|
|
|
const tagsEq = _.isEqual(optTags, existingEntry.hashTags);
|
|
|
|
|
|
|
|
if( tagsEq &&
|
|
|
|
fileEntry.desc === existingEntry.desc &&
|
2017-11-18 21:09:17 +00:00
|
|
|
fileEntry.descLong == existingEntry.descLong &&
|
|
|
|
fileEntry.meta.est_release_year == existingEntry.meta.est_release_year)
|
2017-09-09 05:11:01 +00:00
|
|
|
{
|
|
|
|
console.info('Dupe');
|
|
|
|
return nextFile(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.info('Dupe (updating)');
|
2017-11-18 21:09:17 +00:00
|
|
|
|
|
|
|
// don't allow overwrite of values if new version is blank
|
|
|
|
existingEntry.desc = fileEntry.desc || existingEntry.desc;
|
|
|
|
existingEntry.descLong = fileEntry.descLong || existingEntry.descLong;
|
2017-11-18 21:14:19 +00:00
|
|
|
|
|
|
|
if(fileEntry.meta.est_release_year) {
|
|
|
|
existingEntry.meta.est_release_year = fileEntry.meta.est_release_year;
|
|
|
|
}
|
|
|
|
|
2017-09-09 05:11:01 +00:00
|
|
|
updateTags(existingEntry);
|
|
|
|
|
|
|
|
finalizeEntryAndPersist(true, existingEntry, descHandler, err => {
|
|
|
|
return nextFile(err);
|
2017-02-16 03:27:16 +00:00
|
|
|
});
|
|
|
|
});
|
2017-09-09 05:11:01 +00:00
|
|
|
} else if(dupeEntries.length > 0) {
|
|
|
|
console.info('Dupe');
|
|
|
|
return nextFile(null);
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
2017-09-09 05:11:01 +00:00
|
|
|
|
|
|
|
console.info('Done!');
|
|
|
|
updateTags(fileEntry);
|
|
|
|
|
|
|
|
finalizeEntryAndPersist(false, fileEntry, descHandler, err => {
|
|
|
|
return nextFile(err);
|
|
|
|
});
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function scanDbEntries(callback) {
|
|
|
|
// :TODO: Look @ db entries for area that were *not* processed above
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return nextLocation(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-19 02:00:09 +00:00
|
|
|
function dumpAreaInfo(areaInfo, areaAndStorageInfo, cb) {
|
|
|
|
console.info(`areaTag: ${areaInfo.areaTag}`);
|
|
|
|
console.info(`name: ${areaInfo.name}`);
|
|
|
|
console.info(`desc: ${areaInfo.desc}`);
|
|
|
|
|
|
|
|
areaInfo.storage.forEach(si => {
|
|
|
|
console.info(`storageTag: ${si.storageTag} => ${si.dir}`);
|
|
|
|
});
|
|
|
|
console.info('');
|
|
|
|
|
|
|
|
return cb(null);
|
|
|
|
}
|
|
|
|
|
2017-05-24 03:55:22 +00:00
|
|
|
function getFileEntries(pattern, cb) {
|
|
|
|
// spec: FILENAME_WC|FILE_ID|SHA|PARTIAL_SHA
|
2017-02-19 02:00:09 +00:00
|
|
|
const FileEntry = require('../../core/file_entry.js');
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2017-05-24 03:55:22 +00:00
|
|
|
function tryByFileId(callback) {
|
2017-02-21 05:31:01 +00:00
|
|
|
const fileId = parseInt(pattern);
|
|
|
|
if(!/^[0-9]+$/.test(pattern) || isNaN(fileId)) {
|
2017-05-24 03:55:22 +00:00
|
|
|
return callback(null, null); // try SHA
|
2017-02-19 06:05:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const fileEntry = new FileEntry();
|
2017-05-24 03:55:22 +00:00
|
|
|
fileEntry.load(fileId, err => {
|
|
|
|
return callback(null, err ? null : [ fileEntry ] );
|
2017-02-19 02:00:09 +00:00
|
|
|
});
|
|
|
|
},
|
2017-05-24 03:55:22 +00:00
|
|
|
function tryByShaOrPartialSha(entries, callback) {
|
|
|
|
if(entries) {
|
|
|
|
return callback(null, entries); // already got it by FILE_ID
|
2017-02-19 02:00:09 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:31:01 +00:00
|
|
|
FileEntry.findFileBySha(pattern, (err, fileEntry) => {
|
2017-05-24 03:55:22 +00:00
|
|
|
return callback(null, fileEntry ? [ fileEntry ] : null );
|
2017-02-21 05:31:01 +00:00
|
|
|
});
|
2017-05-24 03:55:22 +00:00
|
|
|
},
|
|
|
|
function tryByFileNameWildcard(entries, callback) {
|
|
|
|
if(entries) {
|
|
|
|
return callback(null, entries); // already got by FILE_ID|SHA
|
|
|
|
}
|
|
|
|
|
|
|
|
return FileEntry.findByFileNameWildcard(pattern, callback);
|
2017-05-23 03:35:06 +00:00
|
|
|
}
|
2017-02-21 05:31:01 +00:00
|
|
|
],
|
2017-05-24 03:55:22 +00:00
|
|
|
(err, entries) => {
|
|
|
|
return cb(err, entries);
|
2017-02-21 05:31:01 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dumpFileInfo(shaOrFileId, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getEntry(callback) {
|
2017-05-24 03:55:22 +00:00
|
|
|
getFileEntries(shaOrFileId, (err, entries) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, entries[0]);
|
2017-02-19 02:00:09 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function dumpInfo(fileEntry, callback) {
|
|
|
|
const fullPath = paths.join(fileArea.getAreaStorageDirectoryByTag(fileEntry.storageTag), fileEntry.fileName);
|
|
|
|
|
|
|
|
console.info(`file_id: ${fileEntry.fileId}`);
|
|
|
|
console.info(`sha_256: ${fileEntry.fileSha256}`);
|
2017-02-21 05:31:01 +00:00
|
|
|
console.info(`area_tag: ${fileEntry.areaTag}`);
|
|
|
|
console.info(`storage_tag: ${fileEntry.storageTag}`);
|
2017-02-19 02:00:09 +00:00
|
|
|
console.info(`path: ${fullPath}`);
|
|
|
|
console.info(`hashTags: ${Array.from(fileEntry.hashTags).join(', ')}`);
|
|
|
|
console.info(`uploaded: ${moment(fileEntry.uploadTimestamp).format()}`);
|
|
|
|
|
|
|
|
_.each(fileEntry.meta, (metaValue, metaName) => {
|
|
|
|
console.info(`${metaName}: ${metaValue}`);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(argv['show-desc']) {
|
|
|
|
console.info(`${fileEntry.desc}`);
|
|
|
|
}
|
|
|
|
console.info('');
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function displayFileAreaInfo() {
|
|
|
|
// AREA_TAG[@STORAGE_TAG]
|
|
|
|
// SHA256|PARTIAL
|
|
|
|
// if sha: dump file info
|
|
|
|
// if area/stoarge dump area(s) +
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
return initConfigAndDatabases(callback);
|
|
|
|
},
|
|
|
|
function dumpInfo(callback) {
|
|
|
|
const Config = require('../../core/config.js').config;
|
|
|
|
let suppliedAreas = argv._.slice(2);
|
|
|
|
if(!suppliedAreas || 0 === suppliedAreas.length) {
|
|
|
|
suppliedAreas = _.map(Config.fileBase.areas, (areaInfo, areaTag) => areaTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
const areaAndStorageInfo = getAreaAndStorage(suppliedAreas);
|
|
|
|
|
|
|
|
fileArea = require('../../core/file_base_area.js');
|
|
|
|
|
|
|
|
async.eachSeries(areaAndStorageInfo, (areaAndStorage, nextArea) => {
|
|
|
|
const areaInfo = fileArea.getFileAreaByTag(areaAndStorage.areaTag);
|
|
|
|
if(areaInfo) {
|
|
|
|
return dumpAreaInfo(areaInfo, areaAndStorageInfo, nextArea);
|
|
|
|
} else {
|
|
|
|
return dumpFileInfo(areaAndStorage.areaTag, nextArea);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
process.exitCode = ExitCodes.ERROR;
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
function scanFileAreas() {
|
|
|
|
const options = {};
|
|
|
|
|
|
|
|
const tags = argv.tags;
|
|
|
|
if(tags) {
|
|
|
|
options.tags = tags.split(',');
|
|
|
|
}
|
|
|
|
|
2017-08-25 02:22:50 +00:00
|
|
|
options.descFile = argv['desc-file']; // --desc-file or --desc-file PATH
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
options.areaAndStorageInfo = getAreaAndStorage(argv._.slice(2));
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
return initConfigAndDatabases(callback);
|
|
|
|
},
|
2017-08-25 02:22:50 +00:00
|
|
|
function initGlobalDescHandler(callback) {
|
|
|
|
//
|
|
|
|
// If options.descFile is a String, it represents a FILE|PATH. We'll init
|
|
|
|
// the description handler now. Else, we'll attempt to look for a description
|
|
|
|
// file in each storage location.
|
|
|
|
//
|
|
|
|
if(!_.isString(options.descFile)) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
loadDescHandler(options.descFile, (err, descHandler) => {
|
|
|
|
options.descFileHandler = descHandler;
|
|
|
|
return callback(null);
|
|
|
|
});
|
|
|
|
},
|
2017-02-16 03:27:16 +00:00
|
|
|
function scanAreas(callback) {
|
|
|
|
fileArea = require('../../core/file_base_area.js');
|
|
|
|
|
|
|
|
async.eachSeries(options.areaAndStorageInfo, (areaAndStorage, nextAreaTag) => {
|
|
|
|
const areaInfo = fileArea.getFileAreaByTag(areaAndStorage.areaTag);
|
|
|
|
if(!areaInfo) {
|
|
|
|
return nextAreaTag(new Error(`Invalid file base area tag: ${areaAndStorage.areaTag}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
console.info(`Processing area "${areaInfo.name}":`);
|
|
|
|
|
|
|
|
scanFileAreaForChanges(areaInfo, options, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
process.exitCode = ExitCodes.ERROR;
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-24 05:03:21 +00:00
|
|
|
function expandFileTargets(targets, cb) {
|
|
|
|
let entries = [];
|
|
|
|
|
|
|
|
// Each entry may be PATH|FILE_ID|SHA|AREA_TAG[@STORAGE_TAG]
|
|
|
|
const FileEntry = require('../../core/file_entry.js');
|
|
|
|
|
|
|
|
async.eachSeries(targets, (areaAndStorage, next) => {
|
|
|
|
const areaInfo = fileArea.getFileAreaByTag(areaAndStorage.areaTag);
|
|
|
|
|
|
|
|
if(areaInfo) {
|
|
|
|
// AREA_TAG[@STORAGE_TAG] - all files in area@tag
|
|
|
|
const findFilter = {
|
|
|
|
areaTag : areaAndStorage.areaTag,
|
|
|
|
};
|
|
|
|
|
|
|
|
if(areaAndStorage.storageTag) {
|
|
|
|
findFilter.storageTag = areaAndStorage.storageTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileEntry.findFiles(findFilter, (err, fileIds) => {
|
|
|
|
if(err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.each(fileIds, (fileId, nextFileId) => {
|
|
|
|
const fileEntry = new FileEntry();
|
|
|
|
fileEntry.load(fileId, err => {
|
|
|
|
if(!err) {
|
|
|
|
entries.push(fileEntry);
|
|
|
|
}
|
|
|
|
return nextFileId(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return next(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// FILENAME_WC|FILE_ID|SHA|PARTIAL_SHA
|
|
|
|
// :TODO: FULL_PATH -> entries
|
|
|
|
getFileEntries(areaAndStorage.pattern, (err, fileEntries) => {
|
|
|
|
if(err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
entries = entries.concat(fileEntries);
|
|
|
|
return next(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return cb(err, entries);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:31:01 +00:00
|
|
|
function moveFiles() {
|
|
|
|
//
|
|
|
|
// oputil fb move SRC [SRC2 ...] DST
|
|
|
|
//
|
2017-05-24 03:55:22 +00:00
|
|
|
// SRC: FILENAME_WC|FILE_ID|SHA|AREA_TAG[@STORAGE_TAG]
|
2017-02-21 05:31:01 +00:00
|
|
|
// DST: AREA_TAG[@STORAGE_TAG]
|
|
|
|
//
|
|
|
|
if(argv._.length < 4) {
|
|
|
|
return printUsageAndSetExitCode(getHelpFor('FileBase'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const moveArgs = argv._.slice(2);
|
2017-09-24 05:03:21 +00:00
|
|
|
const src = getAreaAndStorage(moveArgs.slice(0, -1));
|
|
|
|
const dst = getAreaAndStorage(moveArgs.slice(-1))[0];
|
|
|
|
|
2017-02-21 05:31:01 +00:00
|
|
|
let FileEntry;
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
return initConfigAndDatabases( err => {
|
|
|
|
if(!err) {
|
|
|
|
fileArea = require('../../core/file_base_area.js');
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function validateAndExpandSourceAndDest(callback) {
|
|
|
|
const areaInfo = fileArea.getFileAreaByTag(dst.areaTag);
|
|
|
|
if(areaInfo) {
|
|
|
|
dst.areaInfo = areaInfo;
|
|
|
|
} else {
|
|
|
|
return callback(Errors.DoesNotExist('Invalid or unknown destination area'));
|
|
|
|
}
|
|
|
|
|
|
|
|
FileEntry = require('../../core/file_entry.js');
|
|
|
|
|
2017-09-24 05:03:21 +00:00
|
|
|
expandFileTargets(src, (err, srcEntries) => {
|
2017-02-21 05:31:01 +00:00
|
|
|
return callback(err, srcEntries);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function moveEntries(srcEntries, callback) {
|
|
|
|
|
|
|
|
if(!dst.storageTag) {
|
|
|
|
dst.storageTag = dst.areaInfo.storageTags[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const destDir = FileEntry.getAreaStorageDirectoryByTag(dst.storageTag);
|
|
|
|
|
|
|
|
async.eachSeries(srcEntries, (entry, nextEntry) => {
|
|
|
|
const srcPath = entry.filePath;
|
|
|
|
const dstPath = paths.join(destDir, entry.fileName);
|
|
|
|
|
|
|
|
process.stdout.write(`Moving ${srcPath} => ${dstPath}... `);
|
|
|
|
|
|
|
|
FileEntry.moveEntry(entry, dst.areaTag, dst.storageTag, err => {
|
|
|
|
if(err) {
|
|
|
|
console.info(`Failed: ${err.message}`);
|
|
|
|
} else {
|
|
|
|
console.info('Done');
|
|
|
|
}
|
|
|
|
return nextEntry(null); // always try next
|
|
|
|
});
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
2017-09-24 05:03:21 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
process.exitCode = ExitCodes.ERROR;
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
}
|
2017-02-21 05:31:01 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-24 03:55:22 +00:00
|
|
|
function removeFiles() {
|
|
|
|
//
|
2017-09-24 05:03:21 +00:00
|
|
|
// oputil fb rm|remove|del|delete SRC [SRC2 ...]
|
|
|
|
//
|
|
|
|
// SRC: FILENAME_WC|FILE_ID|SHA|AREA_TAG[@STORAGE_TAG]
|
|
|
|
//
|
|
|
|
// AREA_TAG[@STORAGE_TAG] remove all entries matching
|
|
|
|
// supplied area/storage tags
|
|
|
|
//
|
|
|
|
// --phys-file removes backing physical file(s)
|
|
|
|
//
|
|
|
|
if(argv._.length < 3) {
|
|
|
|
return printUsageAndSetExitCode(getHelpFor('FileBase'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const removePhysFile = argv['phys-file'];
|
|
|
|
|
|
|
|
const src = getAreaAndStorage(argv._.slice(2));
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
return initConfigAndDatabases( err => {
|
|
|
|
if(!err) {
|
|
|
|
fileArea = require('../../core/file_base_area.js');
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function expandSources(callback) {
|
|
|
|
expandFileTargets(src, (err, srcEntries) => {
|
|
|
|
return callback(err, srcEntries);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function removeEntries(srcEntries, callback) {
|
|
|
|
const FileEntry = require('../../core/file_entry.js');
|
|
|
|
|
|
|
|
const extraOutput = removePhysFile ? ' (including physical file)' : '';
|
|
|
|
|
|
|
|
async.eachSeries(srcEntries, (entry, nextEntry) => {
|
|
|
|
|
|
|
|
process.stdout.write(`Removing ${entry.filePath}${extraOutput}... `);
|
|
|
|
|
|
|
|
FileEntry.removeEntry(entry, { removePhysFile }, err => {
|
|
|
|
if(err) {
|
|
|
|
console.info(`Failed: ${err.message}`);
|
|
|
|
} else {
|
|
|
|
console.info('Done');
|
|
|
|
}
|
|
|
|
|
|
|
|
return nextEntry(err);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
return callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
process.exitCode = ExitCodes.ERROR;
|
|
|
|
console.error(err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-05-24 03:55:22 +00:00
|
|
|
}
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
function handleFileBaseCommand() {
|
2017-05-24 03:55:22 +00:00
|
|
|
|
|
|
|
function errUsage() {
|
|
|
|
return printUsageAndSetExitCode(
|
|
|
|
getHelpFor('FileBase') + getHelpFor('FileOpsInfo'),
|
|
|
|
ExitCodes.ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-16 03:27:16 +00:00
|
|
|
if(true === argv.help) {
|
2017-05-24 03:55:22 +00:00
|
|
|
return errUsage();
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const action = argv._[1];
|
|
|
|
|
2017-05-24 03:55:22 +00:00
|
|
|
return ({
|
|
|
|
info : displayFileAreaInfo,
|
|
|
|
scan : scanFileAreas,
|
2017-09-24 05:03:21 +00:00
|
|
|
|
|
|
|
mv : moveFiles,
|
2017-05-24 03:55:22 +00:00
|
|
|
move : moveFiles,
|
2017-09-24 05:03:21 +00:00
|
|
|
|
|
|
|
rm : removeFiles,
|
2017-05-24 03:55:22 +00:00
|
|
|
remove : removeFiles,
|
2017-09-24 05:03:21 +00:00
|
|
|
del : removeFiles,
|
|
|
|
delete : removeFiles,
|
2017-05-24 03:55:22 +00:00
|
|
|
}[action] || errUsage)();
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|