1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/ReactNativeClient/src/file-api.js

112 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-06-15 01:14:15 +02:00
import { promiseChain } from 'src/promise-utils.js';
2017-06-11 23:11:14 +02:00
class FileApi {
constructor(baseDir, driver) {
this.baseDir_ = baseDir;
this.driver_ = driver;
}
2017-06-13 22:12:08 +02:00
fullPath_(path) {
let output = this.baseDir_;
if (path != '') output += '/' + path;
return output;
}
2017-06-15 20:18:48 +02:00
scopeItemToBaseDir_(item) {
let output = Object.assign({}, item);
output.path = item.path.substr(this.baseDir_.length + 1);
return output;
}
scopeItemsToBaseDir_(items) {
let output = [];
for (let i = 0; i < items.length; i++) {
output.push(this.scopeItemToBaseDir_(items[i]));
}
return output;
}
2017-06-15 01:14:15 +02:00
listDirectories() {
return this.driver_.list(this.fullPath_('')).then((items) => {
let output = [];
for (let i = 0; i < items.length; i++) {
2017-06-15 20:18:48 +02:00
if (items[i].isDir) output.push(this.scopeItemToBaseDir_(items[i]));
2017-06-15 01:14:15 +02:00
}
return output;
});
}
list(path = '', recursive = false, context = null) {
2017-06-13 22:12:08 +02:00
let fullPath = this.fullPath_(path);
2017-06-15 01:14:15 +02:00
return this.driver_.list(fullPath).then((items) => {
2017-06-15 20:18:48 +02:00
items = this.scopeItemsToBaseDir_(items);
2017-06-11 23:11:14 +02:00
if (recursive) {
let chain = [];
for (let i = 0; i < items.length; i++) {
let item = items[i];
if (!item.isDir) continue;
chain.push(() => {
2017-06-13 22:12:08 +02:00
return this.list(item.path, true).then((children) => {
2017-06-11 23:11:14 +02:00
for (let j = 0; j < children.length; j++) {
let md = children[j];
2017-06-13 22:12:08 +02:00
md.path = item.path + '/' + md.path;
2017-06-11 23:11:14 +02:00
items.push(md);
}
});
});
}
return promiseChain(chain).then(() => {
return items;
});
} else {
return items;
}
});
}
2017-06-13 22:12:08 +02:00
setTimestamp(path, timestamp) {
return this.driver_.setTimestamp(this.fullPath_(path), timestamp);
2017-06-12 23:56:27 +02:00
}
2017-06-11 23:11:14 +02:00
mkdir(path) {
2017-06-15 01:14:15 +02:00
console.info('mkdir ' + path);
2017-06-13 22:12:08 +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) {
console.info('stat ' + path);
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-15 01:14:15 +02:00
console.info('get ' + 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-15 01:14:15 +02:00
console.info('put ' + 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-13 22:12:08 +02:00
return this.driver_.delete(this.fullPath_(path));
2017-06-11 23:11:14 +02:00
}
move(oldPath, newPath) {
2017-06-13 22:12:08 +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 };