2017-06-24 20:06:28 +02:00
|
|
|
import { isHidden } from 'lib/path-utils.js';
|
|
|
|
import { Logger } from 'lib/logger.js';
|
2017-06-11 23:11:14 +02:00
|
|
|
|
|
|
|
class FileApi {
|
|
|
|
|
|
|
|
constructor(baseDir, driver) {
|
|
|
|
this.baseDir_ = baseDir;
|
|
|
|
this.driver_ = driver;
|
2017-06-23 23:32:24 +02:00
|
|
|
this.logger_ = new Logger();
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
setLogger(l) {
|
|
|
|
this.logger_ = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
2017-06-22 23:52:27 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 22:12:08 +02:00
|
|
|
fullPath_(path) {
|
|
|
|
let output = this.baseDir_;
|
|
|
|
if (path != '') output += '/' + path;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-06-27 21:26:29 +02:00
|
|
|
// DRIVER MUST RETURN PATHS RELATIVE TO `path`
|
2017-06-23 20:51:02 +02:00
|
|
|
list(path = '', options = null) {
|
|
|
|
if (!options) options = {};
|
|
|
|
if (!('includeHidden' in options)) options.includeHidden = false;
|
|
|
|
|
2017-06-23 23:32:24 +02:00
|
|
|
this.logger().debug('list');
|
2017-06-18 01:49:52 +02:00
|
|
|
return this.driver_.list(this.baseDir_).then((items) => {
|
2017-06-23 20:51:02 +02:00
|
|
|
if (!options.includeHidden) {
|
|
|
|
let temp = [];
|
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
if (!isHidden(items[i].path)) temp.push(items[i]);
|
|
|
|
}
|
|
|
|
items = temp;
|
|
|
|
}
|
|
|
|
return items;
|
2017-06-11 23:11:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-27 21:48:01 +02:00
|
|
|
setTimestamp(path, timestampMs) {
|
2017-06-27 21:26:29 +02:00
|
|
|
this.logger().debug('setTimestamp ' + this.fullPath_(path));
|
2017-06-27 21:48:01 +02:00
|
|
|
return this.driver_.setTimestamp(this.fullPath_(path), timestampMs);
|
2017-06-12 23:56:27 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 20:51:02 +02:00
|
|
|
mkdir(path) {
|
2017-06-27 21:26:29 +02:00
|
|
|
this.logger().debug('mkdir ' + this.fullPath_(path));
|
2017-06-23 20:51:02 +02:00
|
|
|
return this.driver_.mkdir(this.fullPath_(path));
|
|
|
|
}
|
2017-06-11 23:11:14 +02:00
|
|
|
|
2017-06-15 01:14:15 +02:00
|
|
|
stat(path) {
|
2017-06-27 21:26:29 +02:00
|
|
|
this.logger().debug('stat ' + this.fullPath_(path));
|
2017-06-15 01:14:15 +02:00
|
|
|
return this.driver_.stat(this.fullPath_(path)).then((output) => {
|
|
|
|
if (!output) return output;
|
|
|
|
output.path = path;
|
|
|
|
return output;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
get(path) {
|
2017-06-27 21:26:29 +02:00
|
|
|
this.logger().debug('get ' + this.fullPath_(path));
|
2017-06-13 22:12:08 +02:00
|
|
|
return this.driver_.get(this.fullPath_(path));
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
put(path, content) {
|
2017-06-27 21:26:29 +02:00
|
|
|
this.logger().debug('put ' + this.fullPath_(path));
|
2017-06-13 22:12:08 +02:00
|
|
|
return this.driver_.put(this.fullPath_(path), content);
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
delete(path) {
|
2017-06-27 21:26:29 +02:00
|
|
|
this.logger().debug('delete ' + this.fullPath_(path));
|
2017-06-13 22:12:08 +02:00
|
|
|
return this.driver_.delete(this.fullPath_(path));
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-23 20:51:02 +02:00
|
|
|
move(oldPath, newPath) {
|
2017-06-27 21:26:29 +02:00
|
|
|
this.logger().debug('move ' + this.fullPath_(oldPath) + ' => ' + this.fullPath_(newPath));
|
2017-06-23 20:51:02 +02:00
|
|
|
return this.driver_.move(this.fullPath_(oldPath), this.fullPath_(newPath));
|
|
|
|
}
|
2017-06-11 23:11:14 +02:00
|
|
|
|
2017-06-13 22:58:17 +02:00
|
|
|
format() {
|
|
|
|
return this.driver_.format();
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { FileApi };
|