oputil fb rm|remove|del|delete functionality

This commit is contained in:
Bryan Ashby 2017-09-23 23:03:21 -06:00
parent b0260049ba
commit 1ad5b125f5
3 changed files with 178 additions and 62 deletions

View file

@ -11,6 +11,7 @@ const async = require('async');
const _ = require('lodash');
const paths = require('path');
const fse = require('fs-extra');
const { unlink } = require('graceful-fs');
const FILE_TABLE_MEMBERS = [
'file_id', 'area_tag', 'file_sha256', 'file_name', 'storage_tag',
@ -541,6 +542,40 @@ module.exports = class FileEntry {
});
}
static removeEntry(srcFileEntry, options, cb) {
if(!_.isFunction(cb) && _.isFunction(options)) {
cb = options;
options = {};
}
async.series(
[
function removeFromDatabase(callback) {
fileDb.run(
`DELETE FROM file
WHERE file_id = ?;`,
[ srcFileEntry.fileId ],
err => {
return callback(err);
}
);
},
function optionallyRemovePhysicalFile(callback) {
if(true !== options.removePhysFile) {
return callback(null);
}
unlink(srcFileEntry.filePath, err => {
return callback(err);
});
}
],
err => {
return cb(err);
}
);
}
static moveEntry(srcFileEntry, destAreaTag, destStorageTag, destFileName, cb) {
if(!cb && _.isFunction(destFileName)) {
cb = destFileName;
@ -550,7 +585,6 @@ module.exports = class FileEntry {
const srcPath = srcFileEntry.filePath;
const dstDir = FileEntry.getAreaStorageDirectoryByTag(destStorageTag);
if(!dstDir) {
return cb(Errors.Invalid('Invalid storage tag'));
}