1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-20 11:28:40 +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) { public async move(source: string, dest: string) {
if (isScopedUri(source) && isScopedUri(dest)) { if (isScopedUri(source) || isScopedUri(dest)) {
await RNSAF.moveFile(source, dest, { replaceIfDestinationExists: true }); 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); return RNFS.moveFile(source, dest);
} }
@ -191,11 +189,9 @@ export default class FsDriverRN extends FsDriverBase {
public async copy(source: string, dest: string) { public async copy(source: string, dest: string) {
let retry = false; let retry = false;
try { try {
if (isScopedUri(source) && isScopedUri(dest)) { if (isScopedUri(source) || isScopedUri(dest)) {
await RNSAF.copyFile(source, dest, { replaceIfDestinationExists: true }); await RNSAF.copyFile(source, dest, { replaceIfDestinationExists: true });
return; return;
} else if (isScopedUri(source) || isScopedUri(dest)) {
throw new Error('Move between different storage types not supported');
} }
await RNFS.copyFile(source, dest); await RNFS.copyFile(source, dest);
} catch (error) { } catch (error) {
@ -204,7 +200,13 @@ export default class FsDriverRN extends FsDriverBase {
await this.unlink(dest); 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) { public async unlink(path: string) {

View File

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

View File

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

View File

@ -1,5 +1,6 @@
package com.reactnativesafx.utils; package com.reactnativesafx.utils;
import android.content.ContentResolver;
import android.net.Uri; import android.net.Uri;
import android.os.Build.VERSION_CODES; import android.os.Build.VERSION_CODES;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
@ -33,4 +34,19 @@ public class UriHelper {
return Uri.decode(normalize(uriString)); 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();
}
} }