1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00
joplin/lib/file-api.js
2017-06-29 18:03:16 +00:00

91 lines
2.1 KiB
JavaScript

import { isHidden } from 'lib/path-utils.js';
import { Logger } from 'lib/logger.js';
class FileApi {
constructor(baseDir, driver) {
this.baseDir_ = baseDir;
this.driver_ = driver;
this.logger_ = new Logger();
}
setLogger(l) {
this.logger_ = l;
}
logger() {
return this.logger_;
}
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;
if (!('context' in options)) options.context = null;
this.logger().debug('list ' + this.baseDir_);
return this.driver_.list(this.baseDir_, options).then((result) => {
if (!options.includeHidden) {
let temp = [];
for (let i = 0; i < result.items.length; i++) {
if (!isHidden(result.items[i].path)) temp.push(result.items[i]);
}
result.items = temp;
}
return result;
});
}
setTimestamp(path, timestampMs) {
this.logger().debug('setTimestamp ' + this.fullPath_(path));
return this.driver_.setTimestamp(this.fullPath_(path), timestampMs);
}
mkdir(path) {
this.logger().debug('mkdir ' + this.fullPath_(path));
return this.driver_.mkdir(this.fullPath_(path));
}
stat(path) {
this.logger().debug('stat ' + this.fullPath_(path));
return this.driver_.stat(this.fullPath_(path)).then((output) => {
if (!output) return output;
output.path = path;
return output;
});
}
get(path) {
this.logger().debug('get ' + this.fullPath_(path));
return this.driver_.get(this.fullPath_(path));
}
put(path, content) {
this.logger().debug('put ' + this.fullPath_(path));
return this.driver_.put(this.fullPath_(path), content);
}
delete(path) {
this.logger().debug('delete ' + this.fullPath_(path));
return this.driver_.delete(this.fullPath_(path));
}
move(oldPath, newPath) {
this.logger().debug('move ' + this.fullPath_(oldPath) + ' => ' + this.fullPath_(newPath));
return this.driver_.move(this.fullPath_(oldPath), this.fullPath_(newPath));
}
format() {
return this.driver_.format();
}
}
export { FileApi };