joplin/ReactNativeClient/lib/file-api.js

93 lines
2.2 KiB
JavaScript
Raw Normal View History

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