1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-02-01 19:15:01 +02:00

112 lines
2.7 KiB
JavaScript
Raw Normal View History

const { isHidden } = require('lib/path-utils.js');
const { Logger } = require('lib/logger.js');
2017-06-11 22:11:14 +01:00
class FileApi {
constructor(baseDir, driver) {
this.baseDir_ = baseDir;
this.driver_ = driver;
2017-06-23 22:32:24 +01:00
this.logger_ = new Logger();
2017-07-24 18:58:11 +00:00
this.syncTargetId_ = null;
2017-06-11 22:11:14 +01:00
}
driver() {
return this.driver_;
}
2017-07-24 18:58:11 +00:00
setSyncTargetId(v) {
this.syncTargetId_ = v;
}
syncTargetId() {
if (this.syncTargetId_ === null) throw new Error('syncTargetId has not been set!!');
return this.syncTargetId_;
}
2017-06-23 22:32:24 +01:00
setLogger(l) {
this.logger_ = l;
}
logger() {
return this.logger_;
2017-06-22 22:52:27 +01: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 22:11:14 +01: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 22:56:27 +01:00
}
mkdir(path) {
this.logger().debug('mkdir ' + this.fullPath_(path));
return this.driver_.mkdir(this.fullPath_(path));
}
2017-06-11 22:11:14 +01:00
2017-06-15 00:14:15 +01:00
stat(path) {
this.logger().debug('stat ' + this.fullPath_(path));
2017-06-15 00:14:15 +01:00
return this.driver_.stat(this.fullPath_(path)).then((output) => {
if (!output) return output;
output.path = path;
return output;
});
}
2017-07-06 22:30:45 +01:00
get(path, options = null) {
if (!options) options = {};
2017-07-02 13:02:07 +01:00
if (!options.encoding) options.encoding = 'utf8';
this.logger().debug('get ' + this.fullPath_(path));
2017-07-02 13:02:07 +01:00
return this.driver_.get(this.fullPath_(path), options);
2017-06-11 22:11:14 +01:00
}
2017-08-01 23:40:14 +02:00
put(path, content, options = null) {
this.logger().debug('put ' + this.fullPath_(path));
2017-08-01 23:40:14 +02:00
return this.driver_.put(this.fullPath_(path), content, options);
2017-06-11 22:11:14 +01: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 22:11:14 +01: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 22:11:14 +01:00
2017-06-13 20:58:17 +00:00
format() {
return this.driver_.format();
}
2017-07-18 19:57:49 +00:00
delta(path, options = null) {
this.logger().debug('delta ' + this.fullPath_(path));
return this.driver_.delta(this.fullPath_(path), options);
}
2017-06-11 22:11:14 +01:00
}
2017-11-03 00:13:17 +00:00
module.exports = { FileApi };