Add 'fb move' to oputil

This commit is contained in:
Bryan Ashby 2017-02-20 22:31:01 -07:00
parent 72b0eafc7b
commit 3af1858c39
5 changed files with 204 additions and 33 deletions

View file

@ -65,7 +65,7 @@ function scanFileAreaForChanges(areaInfo, options, cb) {
return nextFile(null);
}
process.stdout.write(`* Scanning ${fullPath}... `);
process.stdout.write(`Scanning ${fullPath}... `);
fileArea.scanFile(
fullPath,
@ -134,14 +134,15 @@ function dumpAreaInfo(areaInfo, areaAndStorageInfo, cb) {
return cb(null);
}
function dumpFileInfo(shaOrFileId, cb) {
function getSpecificFileEntry(pattern, cb) {
// spec: FILE_ID|SHA|PARTIAL_SHA
const FileEntry = require('../../core/file_entry.js');
async.waterfall(
[
function getByFileId(callback) {
const fileId = parseInt(shaOrFileId);
if(!/^[0-9]+$/.test(shaOrFileId) || isNaN(fileId)) {
const fileId = parseInt(pattern);
if(!/^[0-9]+$/.test(pattern) || isNaN(fileId)) {
return callback(null, null);
}
@ -155,7 +156,22 @@ function dumpFileInfo(shaOrFileId, cb) {
return callback(null, fileEntry); // already got it by sha
}
FileEntry.findFileBySha(shaOrFileId, (err, fileEntry) => {
FileEntry.findFileBySha(pattern, (err, fileEntry) => {
return callback(err, fileEntry);
});
},
],
(err, fileEntry) => {
return cb(err, fileEntry);
}
);
}
function dumpFileInfo(shaOrFileId, cb) {
async.waterfall(
[
function getEntry(callback) {
getSpecificFileEntry(shaOrFileId, (err, fileEntry) => {
return callback(err, fileEntry);
});
},
@ -164,7 +180,8 @@ function dumpFileInfo(shaOrFileId, cb) {
console.info(`file_id: ${fileEntry.fileId}`);
console.info(`sha_256: ${fileEntry.fileSha256}`);
console.info(`area_tag: ${fileEntry.areaTag}`);
console.info(`area_tag: ${fileEntry.areaTag}`);
console.info(`storage_tag: ${fileEntry.storageTag}`);
console.info(`path: ${fullPath}`);
console.info(`hashTags: ${Array.from(fileEntry.hashTags).join(', ')}`);
console.info(`uploaded: ${moment(fileEntry.uploadTimestamp).format()}`);
@ -185,30 +202,6 @@ function dumpFileInfo(shaOrFileId, cb) {
return cb(err);
}
);
/*
FileEntry.findFileBySha(sha, (err, fileEntry) => {
if(err) {
return cb(err);
}
const fullPath = paths.join(fileArea.getAreaStorageDirectoryByTag(fileEntry.storageTag), fileEntry.fileName);
console.info(`file_id: ${fileEntry.fileId}`);
console.info(`sha_256: ${fileEntry.fileSha256}`);
console.info(`area_tag: ${fileEntry.areaTag}`);
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}`);
}
});
*/
}
function displayFileAreaInfo() {
@ -298,6 +291,126 @@ function scanFileAreas() {
);
}
function moveFiles() {
//
// oputil fb move SRC [SRC2 ...] DST
//
// SRC: PATH|FILE_ID|SHA|AREA_TAG[@STORAGE_TAG]
// DST: AREA_TAG[@STORAGE_TAG]
//
if(argv._.length < 4) {
return printUsageAndSetExitCode(getHelpFor('FileBase'), ExitCodes.ERROR);
}
const moveArgs = argv._.slice(2);
let src = getAreaAndStorage(moveArgs.slice(0, -1));
let dst = getAreaAndStorage(moveArgs.slice(-1))[0];
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) {
let srcEntries = [];
const areaInfo = fileArea.getFileAreaByTag(dst.areaTag);
if(areaInfo) {
dst.areaInfo = areaInfo;
} else {
return callback(Errors.DoesNotExist('Invalid or unknown destination area'));
}
// Each SRC may be PATH|FILE_ID|SHA|AREA_TAG[@STORAGE_TAG]
FileEntry = require('../../core/file_entry.js');
async.eachSeries(src, (areaAndStorage, next) => {
//
// If this entry represents a area tag, it means *all files* in that area
//
const areaInfo = fileArea.getFileAreaByTag(areaAndStorage.areaTag);
if(areaInfo) {
src.areaInfo = areaInfo;
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) {
srcEntries.push(fileEntry);
}
return nextFileId(err);
});
},
err => {
return next(err);
});
});
} else {
// PATH|FILE_ID|SHA|PARTIAL_SHA
getSpecificFileEntry(areaAndStorage.pattern, (err, fileEntry) => {
if(err) {
return next(err);
}
srcEntries.push(fileEntry);
return next(null);
});
}
},
err => {
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);
});
}
]
);
}
function handleFileBaseCommand() {
if(true === argv.help) {
return printUsageAndSetExitCode(getHelpFor('FileBase'), ExitCodes.ERROR);
@ -308,6 +421,7 @@ function handleFileBaseCommand() {
switch(action) {
case 'info' : return displayFileAreaInfo();
case 'scan' : return scanFileAreas();
case 'move' : return moveFiles();
default : return printUsageAndSetExitCode(getHelpFor('FileBase'), ExitCodes.ERROR);
}