1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Android: Fixes #6779: Fixed android filesystem sync (resources) (#6789)

This commit is contained in:
javad mnjd 2022-08-29 18:59:28 +04:30 committed by GitHub
parent 6a4eb33093
commit de94c35c0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 10 deletions

View File

@ -107,10 +107,8 @@ export default class FsDriverRN extends FsDriverBase {
}
public async move(source: string, dest: string) {
if (isScopedUri(source) && isScopedUri(dest)) {
if (isScopedUri(source) || isScopedUri(dest)) {
await RNSAF.moveFile(source, dest, { replaceIfDestinationExists: true });
} else if (isScopedUri(source) || isScopedUri(dest)) {
throw new Error('Move between different storage types not supported');
}
return RNFS.moveFile(source, dest);
}
@ -191,11 +189,9 @@ export default class FsDriverRN extends FsDriverBase {
public async copy(source: string, dest: string) {
let retry = false;
try {
if (isScopedUri(source) && isScopedUri(dest)) {
if (isScopedUri(source) || isScopedUri(dest)) {
await RNSAF.copyFile(source, dest, { replaceIfDestinationExists: true });
return;
} else if (isScopedUri(source) || isScopedUri(dest)) {
throw new Error('Move between different storage types not supported');
}
await RNFS.copyFile(source, dest);
} catch (error) {
@ -204,7 +200,13 @@ export default class FsDriverRN extends FsDriverBase {
await this.unlink(dest);
}
if (retry) await RNFS.copyFile(source, dest);
if (retry) {
if (isScopedUri(source) || isScopedUri(dest)) {
await RNSAF.copyFile(source, dest, { replaceIfDestinationExists: true });
} else {
await RNFS.copyFile(source, dest);
}
}
}
public async unlink(path: string) {

View File

@ -169,7 +169,10 @@ public class SafXModule extends ReactContextBaseJavaModule {
try {
DocumentFile doc = this.documentHelper.goToDocument(uriString, false, true);
boolean result = doc.delete();
promise.resolve(result);
if (!result) {
throw new Exception("Failed to unlink file. Unknown error.");
}
promise.resolve(true);
} catch (FileNotFoundException e) {
promise.reject("ENOENT", e.getLocalizedMessage());
} catch (SecurityException e) {

View File

@ -2,6 +2,7 @@ package com.reactnativesafx.utils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.UriPermission;
import android.net.Uri;
@ -18,6 +19,7 @@ import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@ -378,6 +380,9 @@ public class DocumentHelper {
public DocumentFile goToDocument(
String unknownUriString, boolean createIfDirectoryNotExist, boolean includeLastSegment)
throws SecurityException, IOException {
if (unknownUriString.startsWith(ContentResolver.SCHEME_FILE)) {
return DocumentFile.fromFile(new File(Uri.parse(unknownUriString).getPath()));
}
String uriString = UriHelper.normalize(unknownUriString);
String baseUri = "";
String appendUri;
@ -454,7 +459,7 @@ public class DocumentHelper {
public void transferFile(
String srcUri, String destUri, boolean replaceIfDestExists, boolean copy, Promise promise) {
try {
DocumentFile srcDoc = this.goToDocument(srcUri, false, true);
DocumentFile srcDoc = this.goToDocument(UriHelper.getUnifiedUri(srcUri), false, true);
if (srcDoc.isDirectory()) {
throw new IllegalArgumentException("Cannot move directories");
@ -462,7 +467,7 @@ public class DocumentHelper {
DocumentFile destDoc;
try {
destDoc = this.goToDocument(destUri, false, true);
destDoc = this.goToDocument(UriHelper.getUnifiedUri(destUri), false, true);
if (!replaceIfDestExists) {
throw new IOException("a document with the same name already exists in destination");
}

View File

@ -1,5 +1,6 @@
package com.reactnativesafx.utils;
import android.content.ContentResolver;
import android.net.Uri;
import android.os.Build.VERSION_CODES;
import androidx.annotation.RequiresApi;
@ -33,4 +34,19 @@ public class UriHelper {
return Uri.decode(normalize(uriString));
}
public static String getUnifiedUri(String uriString) throws Exception {
Uri uri = Uri.parse(uriString);
if (uri.getScheme() == null) {
uri = Uri.parse(ContentResolver.SCHEME_FILE+"://"+uriString);
} else if (!(uri.getScheme().equals(ContentResolver.SCHEME_FILE) || uri.getScheme().equals(ContentResolver.SCHEME_CONTENT))) {
throw new Exception("Scheme not supported");
}
if (uri.getScheme() == null) {
throw new Exception("Invalid Uri: Cannot determine scheme");
}
return uri.toString();
}
}