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);
|
|
|
|
}
|
|
|
|
|
2017-12-12 01:52:42 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-19 21:01:29 +02:00
|
|
|
move(source, dest) {
|
|
|
|
return fs.move(source, dest, { overwrite: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
exists(path) {
|
|
|
|
return fs.pathExists(path);
|
|
|
|
}
|
|
|
|
|
2017-12-12 01:52:42 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-12 01:52:42 +02:00
|
|
|
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);
|
2017-12-21 21:06:08 +02:00
|
|
|
if (!result.bytesRead) return null;
|
2017-12-23 22:21:11 +02:00
|
|
|
buffer = buffer.slice(0, result.bytesRead);
|
2017-12-21 21:06:08 +02:00
|
|
|
if (encoding === 'base64') return buffer.toString('base64');
|
|
|
|
if (encoding === 'ascii') return buffer.toString('ascii');
|
2017-12-12 01:52:42 +02:00
|
|
|
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;
|