koa-better-validation
Version:
Koa Better Validation is a more up-to-date 'fork' of koa-validation. You can validate request params, querystring values, bodies, and even files.
92 lines (70 loc) • 2.41 kB
JavaScript
;
const fs = require('co-fs-extra');
class FileActions {
constructor(Validator) {
this.validator = Validator;
}
* move(field, file, deleteOnFail, destination, callback) {
try {
yield fs.move(file.path, destination, { clobber: true });
if (!callback) {
return true;
}
if (yield callback(this.validator, file, destination)) {
return true;
}
if (deleteOnFail) {
if (file.path && (yield fs.exists(file.path))) {
yield fs.remove(file.path);
}
}
return false;
} catch (e) {
this.validator.addError(field, 'action', 'move', 'The file could not be moved to the destination provided');
if (deleteOnFail) {
if (file.path && (yield fs.exists(file.path))) {
yield fs.remove(file.path);
}
}
return false;
}
}
* copy(field, file, deleteOnFail, destination, callback) {
try {
yield fs.copy(file.path, destination, { clobber: true });
if (!callback) {
return true;
}
if (yield callback(this.validator, file, destination)) {
return true;
}
if (deleteOnFail) {
if (file.path && (yield fs.exists(file.path))) {
yield fs.remove(file.path);
}
}
return false;
} catch (e) {
this.validator.addError(field, 'action', 'copy', 'The file could not be copied to the destination provided');
if (deleteOnFail) {
if (file.path && (yield fs.exists(file.path))) {
yield fs.remove(file.path);
}
}
return false;
}
}
* remove(field, file, deleteOnFail, args, callback) {
try {
yield fs.remove(file.path);
if (callback) {
return (yield callback(this.validator, file.path));
}
return true;
} catch (e) {
this.validator.addError(field, 'action', 'delete', 'The original uploaded file could not be deleted');
return false;
}
}
}
module.exports = FileActions;