2017-06-24 20:06:28 +02:00
|
|
|
import { time } from 'lib/time-utils.js';
|
2017-06-18 01:49:52 +02:00
|
|
|
|
2017-06-13 22:12:08 +02:00
|
|
|
class FileApiDriverMemory {
|
|
|
|
|
2017-06-27 21:26:29 +02:00
|
|
|
constructor() {
|
2017-06-13 22:12:08 +02:00
|
|
|
this.items_ = [];
|
2017-07-18 22:03:07 +02:00
|
|
|
this.deletedItems_ = [];
|
2017-06-13 22:12:08 +02:00
|
|
|
}
|
|
|
|
|
2017-07-19 00:14:20 +02:00
|
|
|
supportsDelta() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-13 22:12:08 +02: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-19 00:06:10 +02:00
|
|
|
let now = time.unixMs();
|
2017-06-13 22:12:08 +02:00
|
|
|
return {
|
|
|
|
path: path,
|
|
|
|
isDir: isDir,
|
2017-06-19 00:06:10 +02:00
|
|
|
updated_time: now, // In milliseconds!!
|
|
|
|
created_time: now, // In milliseconds!!
|
2017-06-13 22:12:08 +02:00
|
|
|
content: '',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
stat(path) {
|
2017-06-18 01:49:52 +02:00
|
|
|
let item = this.itemByPath(path);
|
|
|
|
return Promise.resolve(item ? Object.assign({}, item) : null);
|
2017-06-13 22:12:08 +02:00
|
|
|
}
|
|
|
|
|
2017-06-27 21:48:01 +02:00
|
|
|
setTimestamp(path, timestampMs) {
|
2017-06-18 01:49:52 +02:00
|
|
|
let item = this.itemByPath(path);
|
2017-06-13 22:12:08 +02:00
|
|
|
if (!item) return Promise.reject(new Error('File not found: ' + path));
|
2017-06-27 21:48:01 +02:00
|
|
|
item.updated_time = timestampMs;
|
2017-06-13 22:12:08 +02:00
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-06-29 20:03:16 +02:00
|
|
|
list(path, options) {
|
2017-06-13 22:12:08 +02:00
|
|
|
let output = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < this.items_.length; i++) {
|
|
|
|
let item = this.items_[i];
|
|
|
|
if (item.path == path) continue;
|
|
|
|
if (item.path.indexOf(path + '/') === 0) {
|
|
|
|
let s = item.path.substr(path.length + 1);
|
|
|
|
if (s.split('/').length === 1) {
|
|
|
|
let it = Object.assign({}, item);
|
2017-06-23 21:09:49 +02:00
|
|
|
it.path = it.path.substr(path.length + 1);
|
2017-06-13 22:12:08 +02:00
|
|
|
output.push(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 20:03:16 +02:00
|
|
|
return Promise.resolve({
|
|
|
|
items: output,
|
|
|
|
hasMore: false,
|
|
|
|
context: null,
|
|
|
|
});
|
2017-06-13 22:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get(path) {
|
|
|
|
let item = this.itemByPath(path);
|
2017-06-18 01:49:52 +02:00
|
|
|
if (!item) return Promise.resolve(null);
|
2017-06-13 22:12:08 +02:00
|
|
|
if (item.isDir) return Promise.reject(new Error(path + ' is a directory, not a file'));
|
|
|
|
return Promise.resolve(item.content);
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir(path) {
|
|
|
|
let index = this.itemIndexByPath(path);
|
|
|
|
if (index >= 0) return Promise.resolve();
|
|
|
|
this.items_.push(this.newItem(path, true));
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
put(path, content) {
|
|
|
|
let index = this.itemIndexByPath(path);
|
|
|
|
if (index < 0) {
|
|
|
|
let item = this.newItem(path, false);
|
|
|
|
item.content = content;
|
|
|
|
this.items_.push(item);
|
|
|
|
} else {
|
|
|
|
this.items_[index].content = content;
|
2017-06-19 00:06:10 +02:00
|
|
|
this.items_[index].updated_time = time.unix();
|
2017-06-13 22:12:08 +02:00
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(path) {
|
|
|
|
let index = this.itemIndexByPath(path);
|
|
|
|
if (index >= 0) {
|
2017-07-18 22:03:07 +02:00
|
|
|
let item = Object.assign({}, this.items_[index]);
|
|
|
|
item.isDeleted = true;
|
|
|
|
item.updated_time = time.unixMs();
|
|
|
|
this.deletedItems_.push(item);
|
2017-06-13 22:12:08 +02:00
|
|
|
this.items_.splice(index, 1);
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
move(oldPath, newPath) {
|
|
|
|
let sourceItem = this.itemByPath(oldPath);
|
|
|
|
if (!sourceItem) return Promise.reject(new Error('Path not found: ' + oldPath));
|
|
|
|
this.delete(newPath); // Overwrite if newPath already exists
|
|
|
|
sourceItem.path = newPath;
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-06-13 22:58:17 +02:00
|
|
|
format() {
|
|
|
|
this.items_ = [];
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-07-18 22:03:07 +02:00
|
|
|
async delta(path, options = null) {
|
|
|
|
let limit = 3;
|
|
|
|
|
|
|
|
let output = {
|
|
|
|
hasMore: false,
|
|
|
|
context: {},
|
|
|
|
items: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
let context = options ? options.context : null;
|
|
|
|
let fromTime = 0;
|
|
|
|
|
|
|
|
if (context) fromTime = context.fromTime;
|
|
|
|
|
|
|
|
let sortedItems = this.items_.slice().concat(this.deletedItems_);
|
|
|
|
sortedItems.sort((a, b) => {
|
|
|
|
if (a.updated_time < b.updated_time) return -1;
|
|
|
|
if (a.updated_time > b.updated_time) return +1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
let hasMore = false;
|
|
|
|
let items = [];
|
|
|
|
let maxTime = 0;
|
|
|
|
for (let i = 0; i < sortedItems.length; i++) {
|
|
|
|
let item = sortedItems[i];
|
|
|
|
if (item.updated_time >= fromTime) {
|
|
|
|
item = Object.assign({}, item);
|
|
|
|
item.path = item.path.substr(path.length + 1);
|
|
|
|
items.push(item);
|
|
|
|
if (item.updated_time > maxTime) maxTime = item.updated_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (items.length >= limit) {
|
|
|
|
hasMore = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output.items = items;
|
|
|
|
output.hasMore = hasMore;
|
|
|
|
output.context = { fromTime: maxTime };
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-06-13 22:12:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export { FileApiDriverMemory };
|