1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-27 08:21:03 +02:00
joplin/ReactNativeClient/lib/fs-driver-node.js

59 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-07-06 00:29:03 +02:00
const fs = require('fs-extra');
2017-07-05 23:52:31 +02:00
class FsDriverNode {
appendFileSync(path, string) {
return fs.appendFileSync(path, string);
}
appendFile(path, string, encoding = 'base64') {
return fs.appendFile(path, string, { encoding: encoding });
}
2017-07-05 23:52:31 +02:00
writeBinaryFile(path, content) {
let buffer = new Buffer(content);
return fs.writeFile(path, buffer);
}
move(source, dest) {
return fs.move(source, dest, { overwrite: true });
}
exists(path) {
return fs.pathExists(path);
}
open(path, mode) {
return fs.open(path, mode);
}
close(handle) {
return fs.close(handle);
}
2017-07-05 23:52:31 +02:00
readFile(path) {
return fs.readFile(path);
}
async unlink(path) {
try {
await fs.unlink(path);
} catch (error) {
if (error.code === 'ENOENT') return; // Don't throw if the file does not exist
throw error;
}
}
async readFileChunk(handle, length, encoding = 'base64') {
let buffer = new Buffer(length);
2017-12-23 22:21:11 +02:00
const result = await fs.read(handle, buffer, 0, length, null);
if (!result.bytesRead) return null;
2017-12-23 22:21:11 +02:00
buffer = buffer.slice(0, result.bytesRead);
if (encoding === 'base64') return buffer.toString('base64');
if (encoding === 'ascii') return buffer.toString('ascii');
throw new Error('Unsupported encoding: ' + encoding);
}
2017-07-05 23:52:31 +02:00
}
2017-07-06 00:29:03 +02:00
module.exports.FsDriverNode = FsDriverNode;