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';
|
2022-10-13 23:02:06 +02:00
|
|
|
const DocumentPicker = require('react-native-document-picker').default;
|
|
|
|
import { openDocument } from '@joplin/react-native-saf-x';
|
|
|
|
import RNSAF, { Encoding, DocumentFileDetail, openDocumentTree } from '@joplin/react-native-saf-x';
|
|
|
|
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-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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-01-08 20:57:40 +02:00
|
|
|
// Encoding can be either "utf8" or "base64"
|
2021-01-27 19:42:58 +02:00
|
|
|
public appendFile(path: string, content: any, encoding = 'base64') {
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
|
|
|
return RNSAF.writeFile(path, content, { encoding: encoding as Encoding, append: true });
|
|
|
|
}
|
2020-01-08 20:57:40 +02:00
|
|
|
return RNFS.appendFile(path, content, encoding);
|
2017-12-12 01:52:42 +02:00
|
|
|
}
|
|
|
|
|
2020-01-08 20:57:40 +02:00
|
|
|
// Encoding can be either "utf8" or "base64"
|
2021-01-27 19:42:58 +02:00
|
|
|
public writeFile(path: string, content: any, encoding = 'base64') {
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
|
|
|
return RNSAF.writeFile(path, content, { encoding: encoding as Encoding });
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public readFile(path: string, encoding = 'utf8') {
|
2018-03-09 22:59:12 +02:00
|
|
|
if (encoding === 'Buffer') throw new Error('Raw buffer output not supported for FsDriverRN.readFile');
|
2022-07-10 16:26:24 +02:00
|
|
|
if (isScopedUri(path)) {
|
|
|
|
return RNSAF.readFile(path, { encoding: encoding as Encoding });
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 19:42:58 +02:00
|
|
|
public async readFileChunk(handle: any, length: number, encoding = 'base64') {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** always returns an array */
|
2023-02-16 11:23:19 +02:00
|
|
|
public async pickDocument(options: { multiple: false }) {
|
2022-10-13 23:02:06 +02:00
|
|
|
const { multiple = false } = options || {};
|
|
|
|
let result;
|
|
|
|
try {
|
|
|
|
if (this.isUsingAndroidSAF()) {
|
|
|
|
result = await openDocument({ multiple });
|
|
|
|
if (!result) {
|
|
|
|
// to catch the error down below using the 'cancel' keyword
|
|
|
|
throw new Error('User canceled document picker');
|
|
|
|
}
|
|
|
|
result = result.map(r => {
|
|
|
|
(r.type as string) = r.mime;
|
|
|
|
((r as any).fileCopyUri as string) = r.uri;
|
|
|
|
return r;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// the result is an array
|
|
|
|
if (multiple) {
|
|
|
|
result = await DocumentPicker.pickMultiple();
|
|
|
|
} else {
|
|
|
|
result = [await DocumentPicker.pick()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (DocumentPicker.isCancel(error) || error?.message?.includes('cancel')) {
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2022-10-13 23:02:06 +02:00
|
|
|
console.info('pickDocuments: user has cancelled');
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-27 19:42:58 +02:00
|
|
|
}
|