1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-03 08:35:29 +02:00
joplin/ReactNativeClient/lib/fs-driver-rn.js
2017-12-28 19:57:21 +00:00

72 lines
1.6 KiB
JavaScript

const RNFS = require('react-native-fs');
class FsDriverRN {
appendFileSync(path, string) {
throw new Error('Not implemented');
}
appendFile(path, string, encoding = 'base64') {
return RNFS.appendFile(path, string, encoding);
}
writeBinaryFile(path, content) {
throw new Error('Not implemented');
}
async move(source, dest) {
return RNFS.moveFile(source, dest);
}
async exists(path) {
return RNFS.exists(path);
}
async open(path, mode) {
// Note: RNFS.read() doesn't provide any way to know if the end of file has been reached.
// So instead we stat the file here and use stat.size to manually check for end of file.
// Bug: https://github.com/itinance/react-native-fs/issues/342
const stat = await RNFS.stat(path);
return {
path: path,
offset: 0,
mode: mode,
stat: stat,
}
}
close(handle) {
return null;
}
readFile(path) {
throw new Error('Not implemented');
}
async unlink(path) {
try {
await RNFS.unlink(path);
} catch (error) {
if (error && error.message && error.message.indexOf('exist') >= 0) {
// Probably { [Error: File does not exist] framesToPop: 1, code: 'EUNSPECIFIED' }
// which unfortunately does not have a proper error code. Can be ignored.
} else {
throw error;
}
}
}
async readFileChunk(handle, length, encoding = 'base64') {
if (handle.offset + length > handle.stat.size) {
length = handle.stat.size - handle.offset;
}
if (!length) return null;
let output = await RNFS.read(handle.path, length, handle.offset, encoding);
handle.offset += length;
return output ? output : null;
}
}
module.exports.FsDriverRN = FsDriverRN;