You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-06-15 23:00:36 +02:00
All: Refactored filesystem sync driver to support mobile
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
const fs = require('fs-extra');
|
||||
const { time } = require('lib/time-utils.js');
|
||||
|
||||
class FsDriverNode {
|
||||
|
||||
@ -15,14 +16,62 @@ class FsDriverNode {
|
||||
return fs.writeFile(path, buffer);
|
||||
}
|
||||
|
||||
move(source, dest) {
|
||||
return fs.move(source, dest, { overwrite: true });
|
||||
writeFile(path, string, encoding = 'base64') {
|
||||
return fs.writeFile(path, string, { encoding: encoding });
|
||||
}
|
||||
|
||||
async move(source, dest) {
|
||||
let lastError = null;
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
try {
|
||||
const output = await fs.move(source, dest, { overwrite: true });
|
||||
return output;
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
// Normally cannot happen with the `overwrite` flag but sometime it still does.
|
||||
// In this case, retry.
|
||||
if (error.code == 'EEXIST') {
|
||||
await time.sleep(1);
|
||||
continue;
|
||||
}
|
||||
throw this.fsErrorToJsError_(error);
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
exists(path) {
|
||||
return fs.pathExists(path);
|
||||
}
|
||||
|
||||
async mkdir(path) {
|
||||
return fs.mkdirp(path);
|
||||
}
|
||||
|
||||
async stat(path) {
|
||||
const s = await fs.stat(path);
|
||||
s.path = path;
|
||||
return s;
|
||||
}
|
||||
|
||||
async setTimestamp(path, timestampDate) {
|
||||
return fs.utimes(path, timestampDate, timestampDate);
|
||||
}
|
||||
|
||||
async readDirStats(path) {
|
||||
let items = await fs.readdir(path);
|
||||
let output = [];
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
let stat = await this.stat(path + '/' + items[i]);
|
||||
if (!stat) continue; // Has been deleted between the readdir() call and now
|
||||
stat.path = stat.path.substr(path.length + 1);
|
||||
output.push(stat);
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
open(path, mode) {
|
||||
return fs.open(path, mode);
|
||||
}
|
||||
@ -31,8 +80,14 @@ class FsDriverNode {
|
||||
return fs.close(handle);
|
||||
}
|
||||
|
||||
readFile(path) {
|
||||
return fs.readFile(path);
|
||||
readFile(path, encoding = 'utf8') {
|
||||
if (encoding === 'Buffer') return fs.readFile(path); // Returns the raw buffer
|
||||
return fs.readFile(path, encoding);
|
||||
}
|
||||
|
||||
// Always overwrite destination
|
||||
async copy(source, dest) {
|
||||
return fs.copy(source, dest, { overwrite: true });
|
||||
}
|
||||
|
||||
async unlink(path) {
|
||||
|
Reference in New Issue
Block a user