2022-07-10 16:26:24 +02:00
|
|
|
import FsDriverBase, { ReadDirStatsOptions } from '@joplin/lib/fs-driver-base';
|
2023-05-29 14:15:53 +02:00
|
|
|
const RNFetchBlob = require('rn-fetch-blob').default;
|
2023-07-18 15:58:06 +02:00
|
|
|
import * as RNFS from 'react-native-fs';
|
2023-10-22 12:51:31 +02:00
|
|
|
import RNSAF, { DocumentFileDetail, openDocumentTree } from '@joplin/react-native-saf-x';
|
2022-10-13 23:02:06 +02:00
|
|
|
import { Platform } from 'react-native';
|
2023-07-18 15:58:06 +02:00
|
|
|
import * as tar from 'tar-stream';
|
|
|
|
import { resolve } from 'path';
|
|
|
|
import { Buffer } from 'buffer';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2023-11-10 16:22:26 +02:00
|
|
|
import JoplinError from '@joplin/lib/JoplinError';
|
2023-07-18 15:58:06 +02:00
|
|
|
|
|
|
|
const logger = Logger.create('fs-driver-rn');
|
2022-07-10 16:26:24 +02:00
|
|
|
|
|
|
|
const ANDROID_URI_PREFIX = 'content://';
|
|
|
|
|
|
|
|
function isScopedUri(path: string) {
|
|
|
|
return path.includes(ANDROID_URI_PREFIX);
|
|
|
|
}
|
2017-12-12 01:52:42 +02:00
|
|
|
|
2023-10-22 12:51:31 +02:00
|
|
|
// Encodings supported by rn-fetch-blob, RNSAF, and
|
|
|
|
// RNFS.
|
|
|
|
// See also
|
|
|
|
// - https://github.com/itinance/react-native-fs#readfilefilepath-string-encoding-string-promisestring
|
|
|
|
// - https://github.com/joltup/rn-fetch-blob/blob/cf9e8843599de92031df2660d5a1da18491fa3c0/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java#L1049
|
|
|
|
export enum SupportedEncoding {
|
|
|
|
Utf8 = 'utf8',
|
|
|
|
Ascii = 'ascii',
|
|
|
|
Base64 = 'base64',
|
|
|
|
}
|
|
|
|
const supportedEncodings = Object.values<string>(SupportedEncoding);
|
|
|
|
|
|
|
|
// Converts some encodings specifiers that work with NodeJS into encodings
|
|
|
|
// that work with RNSAF, RNFetchBlob.fs, and RNFS.
|
|
|
|
//
|
|
|
|
// Throws if an encoding can't be normalized.
|
|
|
|
const normalizeEncoding = (encoding: string): SupportedEncoding => {
|
|
|
|
encoding = encoding.toLowerCase();
|
|
|
|
|
|
|
|
// rn-fetch-blob and RNSAF require the exact string "utf8", but NodeJS (and thus
|
|
|
|
// fs-driver-node) support variants on this like "UtF-8" and "utf-8". Convert them:
|
|
|
|
if (encoding === 'utf-8') {
|
|
|
|
encoding = 'utf8';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportedEncodings.includes(encoding)) {
|
|
|
|
throw new Error(`Unsupported encoding: ${encoding}.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return encoding as SupportedEncoding;
|
|
|
|
};
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
export default class FsDriverRN extends FsDriverBase {
|
|
|
|
public appendFileSync() {
|
2018-03-09 22:59:12 +02:00
|
|
|
throw new Error('Not implemented');
|
2017-12-12 01:52:42 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 12:51:31 +02:00
|
|
|
// Requires that the file already exists.
|
|
|
|
// TODO: Update for compatibility with fs-driver-node's appendFile (which does not
|
|
|
|
// require that the file exists).
|
|
|
|
public appendFile(path: string, content: any, rawEncoding = 'base64') {
|
|
|
|
const encoding = normalizeEncoding(rawEncoding);
|
|
|
|
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
2023-10-22 12:51:31 +02:00
|
|
|
return RNSAF.writeFile(path, content, { encoding, append: true });
|
2022-07-10 16:26:24 +02:00
|
|
|
}
|
2020-01-08 20:57:40 +02:00
|
|
|
return RNFS.appendFile(path, content, encoding);
|
2017-12-12 01:52:42 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 12:51:31 +02:00
|
|
|
// Encoding can be either "utf8", "utf-8", or "base64"
|
|
|
|
public writeFile(path: string, content: any, rawEncoding = 'base64') {
|
|
|
|
const encoding = normalizeEncoding(rawEncoding);
|
|
|
|
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
2023-10-22 12:51:31 +02:00
|
|
|
return RNSAF.writeFile(path, content, { encoding: encoding });
|
2022-07-10 16:26:24 +02:00
|
|
|
}
|
2023-10-22 12:51:31 +02:00
|
|
|
|
2020-01-08 20:57:40 +02:00
|
|
|
// We need to use rn-fetch-blob here due to this bug:
|
|
|
|
// https://github.com/itinance/react-native-fs/issues/700
|
2023-05-29 14:15:53 +02:00
|
|
|
return RNFetchBlob.fs.writeFile(path, content, encoding);
|
2018-01-17 20:51:15 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 23:15:58 +02:00
|
|
|
// same as rm -rf
|
2021-01-27 19:42:58 +02:00
|
|
|
public async remove(path: string) {
|
2018-03-18 01:00:01 +02:00
|
|
|
return await this.unlink(path);
|
2018-01-25 23:15:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-17 23:01:41 +02:00
|
|
|
// Returns a format compatible with Node.js format
|
2021-01-27 19:42:58 +02:00
|
|
|
private rnfsStatToStd_(stat: any, path: string) {
|
2022-07-10 16:26:24 +02:00
|
|
|
let birthtime;
|
|
|
|
const mtime = stat.lastModified ? new Date(stat.lastModified) : stat.mtime;
|
|
|
|
if (stat.lastModified) {
|
|
|
|
birthtime = new Date(stat.lastModified);
|
|
|
|
} else if (stat.ctime) {
|
|
|
|
// Confusingly, "ctime" normally means "change time" but here it's used as "creation time". Also sometimes it is null
|
|
|
|
birthtime = stat.ctime;
|
|
|
|
} else {
|
|
|
|
birthtime = stat.mtime;
|
|
|
|
}
|
2018-01-17 23:01:41 +02:00
|
|
|
return {
|
2022-07-10 16:26:24 +02:00
|
|
|
birthtime,
|
|
|
|
mtime,
|
|
|
|
isDirectory: () => stat.type ? stat.type === 'directory' : stat.isDirectory(),
|
2018-01-17 23:01:41 +02:00
|
|
|
path: path,
|
|
|
|
size: stat.size,
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-01-17 23:01:41 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async readDirStats(path: string, options: any = null) {
|
2018-02-25 23:08:32 +02:00
|
|
|
if (!options) options = {};
|
2018-03-09 22:59:12 +02:00
|
|
|
if (!('recursive' in options)) options.recursive = false;
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2022-07-10 16:26:24 +02:00
|
|
|
const isScoped = isScopedUri(path);
|
|
|
|
|
2023-07-18 15:58:06 +02:00
|
|
|
let stats: any[] = [];
|
2019-12-29 19:58:40 +02:00
|
|
|
try {
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScoped) {
|
|
|
|
stats = await RNSAF.listFiles(path);
|
|
|
|
} else {
|
|
|
|
stats = await RNFS.readDir(path);
|
|
|
|
}
|
2019-12-29 19:58:40 +02:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(`Could not read directory: ${path}: ${error.message}`);
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
let output: any[] = [];
|
2022-07-10 16:26:24 +02:00
|
|
|
for (let i = 0; i < stats.length; i++) {
|
|
|
|
const stat = stats[i];
|
|
|
|
const relativePath = (isScoped ? stat.uri : stat.path).substr(path.length + 1);
|
2023-07-18 15:58:06 +02:00
|
|
|
const standardStat = this.rnfsStatToStd_(stat, relativePath);
|
|
|
|
output.push(standardStat);
|
2018-02-25 23:08:32 +02:00
|
|
|
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScoped) {
|
2023-07-18 15:58:06 +02:00
|
|
|
// readUriDirStatsHandleRecursion_ expects stat to have a URI property.
|
|
|
|
// Use the original stat.
|
2022-07-10 16:26:24 +02:00
|
|
|
output = await this.readUriDirStatsHandleRecursion_(stat, output, options);
|
|
|
|
} else {
|
2023-07-18 15:58:06 +02:00
|
|
|
output = await this.readDirStatsHandleRecursion_(path, standardStat, output, options);
|
2022-07-10 16:26:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected async readUriDirStatsHandleRecursion_(stat: DocumentFileDetail, output: DocumentFileDetail[], options: ReadDirStatsOptions) {
|
|
|
|
if (options.recursive && stat.type === 'directory') {
|
|
|
|
const subStats = await this.readDirStats(stat.uri, options);
|
|
|
|
for (let j = 0; j < subStats.length; j++) {
|
|
|
|
const subStat = subStats[j];
|
|
|
|
output.push(subStat);
|
|
|
|
}
|
2018-01-17 23:01:41 +02:00
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async move(source: string, dest: string) {
|
2022-08-29 16:29:28 +02:00
|
|
|
if (isScopedUri(source) || isScopedUri(dest)) {
|
2022-07-10 16:26:24 +02:00
|
|
|
await RNSAF.moveFile(source, dest, { replaceIfDestinationExists: true });
|
|
|
|
}
|
2017-12-28 21:57:21 +02:00
|
|
|
return RNFS.moveFile(source, dest);
|
|
|
|
}
|
|
|
|
|
2023-06-13 19:06:54 +02:00
|
|
|
public async rename(source: string, dest: string) {
|
|
|
|
if (isScopedUri(source) || isScopedUri(dest)) {
|
|
|
|
await RNSAF.rename(source, dest);
|
|
|
|
}
|
|
|
|
return RNFS.moveFile(source, dest);
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async exists(path: string) {
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
|
|
|
return RNSAF.exists(path);
|
|
|
|
}
|
2017-12-28 21:57:21 +02:00
|
|
|
return RNFS.exists(path);
|
2017-12-19 21:01:29 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async mkdir(path: string) {
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
|
|
|
await RNSAF.mkdir(path);
|
|
|
|
return;
|
|
|
|
}
|
2023-07-18 15:58:06 +02:00
|
|
|
|
|
|
|
// Also creates parent directories: Works like mkdir -p
|
2018-01-17 23:01:41 +02:00
|
|
|
return RNFS.mkdir(path);
|
2018-01-17 20:51:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async stat(path: string) {
|
2018-01-17 23:01:41 +02:00
|
|
|
try {
|
2022-07-10 16:26:24 +02:00
|
|
|
let r;
|
|
|
|
if (isScopedUri(path)) {
|
|
|
|
r = await RNSAF.stat(path);
|
|
|
|
} else {
|
|
|
|
r = await RNFS.stat(path);
|
|
|
|
}
|
2018-01-17 23:01:41 +02:00
|
|
|
return this.rnfsStatToStd_(r, path);
|
|
|
|
} catch (error) {
|
2023-07-18 15:58:06 +02:00
|
|
|
if (error && (error.code === 'ENOENT' || !(await this.exists(path)))) {
|
2018-01-17 23:01:41 +02:00
|
|
|
// Probably { [Error: File does not exist] framesToPop: 1, code: 'EUNSPECIFIED' }
|
2023-07-18 15:58:06 +02:00
|
|
|
// or { [Error: The file {file} couldn’t be opened because there is no such file.], code: 'ENSCOCOAERRORDOMAIN260' }
|
2018-01-17 23:01:41 +02:00
|
|
|
// which unfortunately does not have a proper error code. Can be ignored.
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 20:51:15 +02:00
|
|
|
}
|
|
|
|
|
2018-01-17 23:01:41 +02:00
|
|
|
// NOTE: DOES NOT WORK - no error is thrown and the function is called with the right
|
|
|
|
// arguments but the function returns `false` and the timestamp is not set.
|
|
|
|
// Current setTimestamp is not really used so keep it that way, but careful if it
|
|
|
|
// becomes needed.
|
2021-01-27 19:42:58 +02:00
|
|
|
public async setTimestamp() {
|
2018-01-17 23:01:41 +02:00
|
|
|
// return RNFS.touch(path, timestampDate, timestampDate);
|
2018-01-17 20:51:15 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async open(path: string, mode: number) {
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
|
|
|
throw new Error('open() not implemented in FsDriverAndroid');
|
|
|
|
}
|
2017-12-12 01:52:42 +02:00
|
|
|
// 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
|
2018-01-17 20:51:15 +02:00
|
|
|
const stat = await this.stat(path);
|
2017-12-12 01:52:42 +02:00
|
|
|
return {
|
|
|
|
path: path,
|
|
|
|
offset: 0,
|
|
|
|
mode: mode,
|
|
|
|
stat: stat,
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2017-12-12 01:52:42 +02:00
|
|
|
}
|
|
|
|
|
2021-09-06 18:05:27 +02:00
|
|
|
public close(): Promise<void> {
|
2021-01-27 19:42:58 +02:00
|
|
|
// Nothing
|
2021-09-06 18:05:27 +02:00
|
|
|
return null;
|
2017-12-12 01:52:42 +02:00
|
|
|
}
|
|
|
|
|
2023-10-22 12:51:31 +02:00
|
|
|
public readFile(path: string, rawEncoding = 'utf8') {
|
|
|
|
const encoding = normalizeEncoding(rawEncoding);
|
|
|
|
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
2023-10-22 12:51:31 +02:00
|
|
|
return RNSAF.readFile(path, { encoding: encoding });
|
2022-07-10 16:26:24 +02:00
|
|
|
}
|
2018-01-17 20:51:15 +02:00
|
|
|
return RNFS.readFile(path, encoding);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always overwrite destination
|
2021-01-27 19:42:58 +02:00
|
|
|
public async copy(source: string, dest: string) {
|
2018-01-17 20:51:15 +02:00
|
|
|
let retry = false;
|
|
|
|
try {
|
2022-08-29 16:29:28 +02:00
|
|
|
if (isScopedUri(source) || isScopedUri(dest)) {
|
2022-07-10 16:26:24 +02:00
|
|
|
await RNSAF.copyFile(source, dest, { replaceIfDestinationExists: true });
|
|
|
|
return;
|
|
|
|
}
|
2018-01-17 20:51:15 +02:00
|
|
|
await RNFS.copyFile(source, dest);
|
|
|
|
} catch (error) {
|
|
|
|
// On iOS it will throw an error if the file already exist
|
|
|
|
retry = true;
|
|
|
|
await this.unlink(dest);
|
|
|
|
}
|
|
|
|
|
2022-08-29 16:29:28 +02:00
|
|
|
if (retry) {
|
|
|
|
if (isScopedUri(source) || isScopedUri(dest)) {
|
|
|
|
await RNSAF.copyFile(source, dest, { replaceIfDestinationExists: true });
|
|
|
|
} else {
|
|
|
|
await RNFS.copyFile(source, dest);
|
|
|
|
}
|
|
|
|
}
|
2017-12-12 01:52:42 +02:00
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async unlink(path: string) {
|
2017-12-12 01:52:42 +02:00
|
|
|
try {
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
|
|
|
await RNSAF.unlink(path);
|
|
|
|
return;
|
|
|
|
}
|
2017-12-12 01:52:42 +02:00
|
|
|
await RNFS.unlink(path);
|
|
|
|
} catch (error) {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (error && ((error.message && error.message.indexOf('exist') >= 0) || error.code === 'ENOENT')) {
|
2017-12-12 01:52:42 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-22 12:51:31 +02:00
|
|
|
public async readFileChunk(handle: any, length: number, rawEncoding = 'base64') {
|
2023-11-10 16:22:26 +02:00
|
|
|
if (!handle?.stat) {
|
|
|
|
throw new JoplinError('File does not exist (reading file chunk).', 'ENOENT');
|
|
|
|
}
|
|
|
|
|
2023-10-22 12:51:31 +02:00
|
|
|
const encoding = normalizeEncoding(rawEncoding);
|
|
|
|
|
2017-12-12 01:52:42 +02:00
|
|
|
if (handle.offset + length > handle.stat.size) {
|
|
|
|
length = handle.stat.size - handle.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!length) return null;
|
2020-03-14 01:46:14 +02:00
|
|
|
const output = await RNFS.read(handle.path, length, handle.offset, encoding);
|
2019-07-30 09:35:42 +02:00
|
|
|
// eslint-disable-next-line require-atomic-updates
|
2017-12-12 01:52:42 +02:00
|
|
|
handle.offset += length;
|
|
|
|
return output ? output : null;
|
|
|
|
}
|
2020-10-09 19:35:46 +02:00
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public resolve(path: string) {
|
2020-10-09 19:35:46 +02:00
|
|
|
throw new Error(`Not implemented: resolve(): ${path}`);
|
|
|
|
}
|
2020-10-21 01:23:55 +02:00
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public resolveRelativePathWithinDir(_baseDir: string, relativePath: string) {
|
2020-10-21 01:23:55 +02:00
|
|
|
throw new Error(`Not implemented: resolveRelativePathWithinDir(): ${relativePath}`);
|
|
|
|
}
|
2017-12-12 01:52:42 +02:00
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async md5File(path: string): Promise<string> {
|
|
|
|
throw new Error(`Not implemented: md5File(): ${path}`);
|
|
|
|
}
|
2022-10-13 23:02:06 +02:00
|
|
|
|
2023-07-18 15:58:06 +02:00
|
|
|
public async tarExtract(_options: any) {
|
|
|
|
throw new Error('Not implemented: tarExtract');
|
|
|
|
}
|
|
|
|
|
|
|
|
public async tarCreate(options: any, filePaths: string[]) {
|
|
|
|
// Choose a default cwd if not given
|
|
|
|
const cwd = options.cwd ?? RNFS.DocumentDirectoryPath;
|
|
|
|
const file = resolve(cwd, options.file);
|
|
|
|
|
|
|
|
if (await this.exists(file)) {
|
|
|
|
throw new Error('Error! Destination already exists');
|
|
|
|
}
|
|
|
|
|
|
|
|
const pack = tar.pack();
|
|
|
|
|
|
|
|
for (const path of filePaths) {
|
|
|
|
const absPath = resolve(cwd, path);
|
|
|
|
const stat = await this.stat(absPath);
|
|
|
|
const sizeBytes: number = stat.size;
|
|
|
|
|
|
|
|
const entry = pack.entry({ name: path, size: sizeBytes }, (error) => {
|
|
|
|
if (error) {
|
|
|
|
logger.error(`Tar error: ${error}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const chunkSize = 1024 * 100; // 100 KiB
|
|
|
|
for (let offset = 0; offset < sizeBytes; offset += chunkSize) {
|
|
|
|
// The RNFS documentation suggests using base64 for binary files.
|
|
|
|
const part = await RNFS.read(absPath, chunkSize, offset, 'base64');
|
|
|
|
entry.write(Buffer.from(part, 'base64'));
|
|
|
|
}
|
|
|
|
entry.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
pack.finalize();
|
|
|
|
|
|
|
|
// The streams used by tar-stream seem not to support a chunk size
|
|
|
|
// (it seems despite the typings provided).
|
|
|
|
let data: number[]|null = null;
|
|
|
|
while ((data = pack.read()) !== null) {
|
|
|
|
const buff = Buffer.from(data);
|
|
|
|
const base64Data = buff.toString('base64');
|
|
|
|
await this.appendFile(file, base64Data, 'base64');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 23:02:06 +02:00
|
|
|
public async getExternalDirectoryPath(): Promise<string | undefined> {
|
|
|
|
let directory;
|
|
|
|
if (this.isUsingAndroidSAF()) {
|
|
|
|
const doc = await openDocumentTree(true);
|
|
|
|
if (doc?.uri) {
|
|
|
|
directory = doc?.uri;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
directory = RNFS.ExternalDirectoryPath;
|
|
|
|
}
|
|
|
|
return directory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public isUsingAndroidSAF() {
|
|
|
|
return Platform.OS === 'android' && Platform.Version > 28;
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
}
|