1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-06 22:46:29 +02:00
joplin/ReactNativeClient/lib/file-api-driver-memory.js

161 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-03-09 17:49:35 +00:00
const { time } = require("lib/time-utils.js");
const fs = require("fs-extra");
const { basicDelta } = require("lib/file-api");
2017-06-18 00:49:52 +01:00
2017-06-13 20:12:08 +00:00
class FileApiDriverMemory {
constructor() {
2017-06-13 20:12:08 +00:00
this.items_ = [];
2017-07-18 21:03:07 +01:00
this.deletedItems_ = [];
2017-06-13 20:12:08 +00:00
}
encodeContent_(content) {
if (content instanceof Buffer) {
2018-03-09 17:49:35 +00:00
return content.toString("base64");
} else {
2018-03-09 17:49:35 +00:00
return Buffer.from(content).toString("base64");
}
}
decodeContent_(content) {
2018-03-09 17:49:35 +00:00
return Buffer.from(content, "base64").toString("utf-8");
}
2017-06-13 20:12:08 +00:00
itemIndexByPath(path) {
for (let i = 0; i < this.items_.length; i++) {
if (this.items_[i].path == path) return i;
}
return -1;
}
itemByPath(path) {
let index = this.itemIndexByPath(path);
return index < 0 ? null : this.items_[index];
}
newItem(path, isDir = false) {
2017-06-18 23:06:10 +01:00
let now = time.unixMs();
2017-06-13 20:12:08 +00:00
return {
path: path,
isDir: isDir,
2017-06-18 23:06:10 +01:00
updated_time: now, // In milliseconds!!
2018-02-07 20:42:52 +00:00
// created_time: now, // In milliseconds!!
2018-03-09 17:49:35 +00:00
content: "",
2017-06-13 20:12:08 +00:00
};
}
stat(path) {
2017-06-18 00:49:52 +01:00
let item = this.itemByPath(path);
return Promise.resolve(item ? Object.assign({}, item) : null);
2017-06-13 20:12:08 +00:00
}
async setTimestamp(path, timestampMs) {
2017-06-18 00:49:52 +01:00
let item = this.itemByPath(path);
2018-03-09 17:49:35 +00:00
if (!item) return Promise.reject(new Error("File not found: " + path));
2017-06-27 19:48:01 +00:00
item.updated_time = timestampMs;
2017-06-13 20:12:08 +00:00
}
async list(path, options) {
2017-06-13 20:12:08 +00:00
let output = [];
for (let i = 0; i < this.items_.length; i++) {
let item = this.items_[i];
if (item.path == path) continue;
2018-03-09 17:49:35 +00:00
if (item.path.indexOf(path + "/") === 0) {
2017-06-13 20:12:08 +00:00
let s = item.path.substr(path.length + 1);
2018-03-09 17:49:35 +00:00
if (s.split("/").length === 1) {
2017-06-13 20:12:08 +00:00
let it = Object.assign({}, item);
it.path = it.path.substr(path.length + 1);
2017-06-13 20:12:08 +00:00
output.push(it);
}
}
}
2017-06-29 18:03:16 +00:00
return Promise.resolve({
items: output,
hasMore: false,
context: null,
});
2017-06-13 20:12:08 +00:00
}
async get(path, options) {
2017-06-13 20:12:08 +00:00
let item = this.itemByPath(path);
2017-06-18 00:49:52 +01:00
if (!item) return Promise.resolve(null);
2018-03-09 17:49:35 +00:00
if (item.isDir) return Promise.reject(new Error(path + " is a directory, not a file"));
let output = null;
2018-03-09 17:49:35 +00:00
if (options.target === "file") {
await fs.writeFile(options.path, Buffer.from(item.content, "base64"));
} else {
const content = this.decodeContent_(item.content);
output = Promise.resolve(content);
}
return output;
2017-06-13 20:12:08 +00:00
}
async mkdir(path) {
2017-06-13 20:12:08 +00:00
let index = this.itemIndexByPath(path);
if (index >= 0) return;
2017-06-13 20:12:08 +00:00
this.items_.push(this.newItem(path, true));
}
async put(path, content, options = null) {
if (!options) options = {};
2018-03-09 17:49:35 +00:00
if (options.source === "file") content = await fs.readFile(options.path);
2017-06-13 20:12:08 +00:00
let index = this.itemIndexByPath(path);
if (index < 0) {
let item = this.newItem(path, false);
item.content = this.encodeContent_(content);
2017-06-13 20:12:08 +00:00
this.items_.push(item);
} else {
this.items_[index].content = this.encodeContent_(content);
2017-06-18 23:06:10 +01:00
this.items_[index].updated_time = time.unix();
2017-06-13 20:12:08 +00:00
}
}
async delete(path) {
2017-06-13 20:12:08 +00:00
let index = this.itemIndexByPath(path);
if (index >= 0) {
2017-07-18 21:03:07 +01:00
let item = Object.assign({}, this.items_[index]);
item.isDeleted = true;
item.updated_time = time.unixMs();
this.deletedItems_.push(item);
2017-06-13 20:12:08 +00:00
this.items_.splice(index, 1);
}
}
async move(oldPath, newPath) {
2017-06-13 20:12:08 +00:00
let sourceItem = this.itemByPath(oldPath);
2018-03-09 17:49:35 +00:00
if (!sourceItem) return Promise.reject(new Error("Path not found: " + oldPath));
2017-06-13 20:12:08 +00:00
this.delete(newPath); // Overwrite if newPath already exists
sourceItem.path = newPath;
}
async format() {
2017-06-13 20:58:17 +00:00
this.items_ = [];
}
2017-07-18 21:03:07 +01:00
async delta(path, options = null) {
2018-03-09 17:49:35 +00:00
const getStatFn = async path => {
let output = this.items_.slice();
for (let i = 0; i < output.length; i++) {
const item = Object.assign({}, output[i]);
2017-07-18 21:03:07 +01:00
item.path = item.path.substr(path.length + 1);
output[i] = item;
2017-07-18 21:03:07 +01:00
}
return output;
};
2017-07-18 21:03:07 +01:00
const output = await basicDelta(path, getStatFn, options);
2017-07-18 21:03:07 +01:00
return output;
}
async clearRoot() {
this.items_ = [];
}
2017-06-13 20:12:08 +00:00
}
2018-03-09 17:49:35 +00:00
module.exports = { FileApiDriverMemory };