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

164 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-06-11 23:11:14 +02:00
import fs from 'fs';
import fse from 'fs-extra';
2017-06-24 20:06:28 +02:00
import { promiseChain } from 'lib/promise-utils.js';
2017-06-11 23:11:14 +02:00
import moment from 'moment';
class FileApiDriverLocal {
stat(path) {
return new Promise((resolve, reject) => {
fs.stat(path, (error, s) => {
if (error) {
2017-06-15 01:14:15 +02:00
if (error.code == 'ENOENT') {
resolve(null);
} else {
reject(error);
}
2017-06-11 23:11:14 +02:00
return;
}
2017-06-15 01:14:15 +02:00
resolve(this.metadataFromStats_(path, s));
2017-06-11 23:11:14 +02:00
});
});
}
2017-06-27 21:48:01 +02:00
statTimeToTimestampMs_(time) {
2017-06-11 23:11:14 +02:00
let m = moment(time, 'YYYY-MM-DDTHH:mm:ss.SSSZ');
if (!m.isValid()) {
throw new Error('Invalid date: ' + time);
}
2017-06-27 21:48:01 +02:00
return m.toDate().getTime();
2017-06-11 23:11:14 +02:00
}
2017-06-12 23:56:27 +02:00
metadataFromStats_(path, stats) {
2017-06-11 23:11:14 +02:00
return {
2017-06-12 23:56:27 +02:00
path: path,
2017-06-27 21:48:01 +02:00
created_time: this.statTimeToTimestampMs_(stats.birthtime),
updated_time: this.statTimeToTimestampMs_(stats.mtime),
2017-06-19 00:06:10 +02:00
created_time_orig: stats.birthtime,
updated_time_orig: stats.mtime,
2017-06-11 23:11:14 +02:00
isDir: stats.isDirectory(),
};
}
2017-06-27 21:48:01 +02:00
setTimestamp(path, timestampMs) {
2017-06-12 23:56:27 +02:00
return new Promise((resolve, reject) => {
2017-06-27 21:48:01 +02:00
let t = Math.floor(timestampMs / 1000);
fs.utimes(path, t, t, (error) => {
2017-06-12 23:56:27 +02:00
if (error) {
reject(error);
return;
}
resolve();
});
});
}
2017-06-11 23:11:14 +02:00
list(path) {
return new Promise((resolve, reject) => {
fs.readdir(path, (error, items) => {
if (error) {
reject(error);
return;
}
let chain = [];
for (let i = 0; i < items.length; i++) {
chain.push((output) => {
if (!output) output = [];
return this.stat(path + '/' + items[i]).then((stat) => {
stat.path = items[i];
2017-06-15 01:14:15 +02:00
output.push(stat);
2017-06-11 23:11:14 +02:00
return output;
});
});
}
return promiseChain(chain).then((results) => {
if (!results) results = [];
resolve(results);
}).catch((error) => {
reject(error);
});
});
});
}
get(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (error, content) => {
if (error) {
2017-06-15 01:14:15 +02:00
if (error.code == 'ENOENT') {
// Return null in this case so that it's possible to get a file
// without checking if it exists first.
resolve(null);
} else {
reject(error);
}
2017-06-11 23:11:14 +02:00
return;
}
return resolve(content);
});
});
}
mkdir(path) {
return new Promise((resolve, reject) => {
fs.exists(path, (exists) => {
if (exists) {
resolve();
return;
}
const mkdirp = require('mkdirp');
mkdirp(path, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
});
}
put(path, content) {
return new Promise((resolve, reject) => {
fs.writeFile(path, content, function(error) {
if (error) {
reject(error);
} else {
resolve();
}
});
});
}
delete(path) {
return new Promise((resolve, reject) => {
fs.unlink(path, function(error) {
if (error) {
if (error && error.code == 'ENOENT') {
// File doesn't exist - it's fine
resolve();
} else {
reject(error);
}
} else {
resolve();
}
});
});
}
move(oldPath, newPath) {
2017-06-27 21:48:01 +02:00
return fse.move(oldPath, newPath, { overwrite: true });
2017-06-11 23:11:14 +02:00
}
2017-06-13 22:58:17 +02:00
format() {
throw new Error('Not supported');
}
2017-06-11 23:11:14 +02:00
}
export { FileApiDriverLocal };