Skip to content

Commit 4653f84

Browse files
authored
feat: add proper fallback and saf uri handling for ischeck (Acode-Foundation#1814)
1 parent e13df1d commit 4653f84

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

src/lib/editorFile.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -612,39 +612,55 @@ export default class EditorFile {
612612
const protocol = Url.getProtocol(this.#uri);
613613
const text = this.session.getValue();
614614

615+
// Helper for JS-based comparison (used as fallback)
616+
const jsCompare = async (fileUri) => {
617+
const fs = fsOperation(fileUri);
618+
const oldText = await fs.readFile(this.encoding);
619+
return await system.compareTexts(oldText, text);
620+
};
621+
615622
if (/s?ftp:/.test(protocol)) {
616-
// if file is a ftp or sftp file, get file content from cached file.
617-
// remove ':' from protocol because cache file of remote files are
618-
// stored as ftp102525465N i.e. protocol + id
619-
// Cache files are local file:// URIs, so native file reading works
623+
// FTP/SFTP files use cached local file
620624
const cacheFilename = protocol.slice(0, -1) + this.id;
621625
const cacheFileUri = Url.join(CACHE_STORAGE, cacheFilename);
622626

623627
try {
624628
return await system.compareFileText(cacheFileUri, this.encoding, text);
625629
} catch (error) {
626-
console.error("Native compareFileText failed:", error);
627-
return false;
630+
console.error(
631+
"Native compareFileText failed, using JS fallback:",
632+
error,
633+
);
634+
try {
635+
return await jsCompare(cacheFileUri);
636+
} catch (fallbackError) {
637+
console.error(fallbackError);
638+
return false;
639+
}
628640
}
629641
}
630642

631643
if (/^(file|content):/.test(protocol)) {
632-
// file:// and content:// URIs can be handled by native Android code
633-
// Native reads file AND compares in background thread
644+
// file:// and content:// URIs - try native first, fallback to JS
634645
try {
635646
return await system.compareFileText(this.uri, this.encoding, text);
636647
} catch (error) {
637-
console.error("Native compareFileText failed:", error);
638-
return false;
648+
console.error(
649+
"Native compareFileText failed, using JS fallback:",
650+
error,
651+
);
652+
try {
653+
return await jsCompare(this.uri);
654+
} catch (fallbackError) {
655+
console.error(fallbackError);
656+
return false;
657+
}
639658
}
640659
}
641660

661+
// Other protocols - JS reads file, native compares strings
642662
try {
643-
const fs = fsOperation(this.uri);
644-
const oldText = await fs.readFile(this.encoding);
645-
646-
// Offload string comparison to background thread
647-
return await system.compareTexts(oldText, text);
663+
return await jsCompare(this.uri);
648664
} catch (error) {
649665
console.error(error);
650666
return false;

src/plugins/system/android/com/foxdebug/system/System.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import android.content.pm.PackageInfo;
9090
import android.content.pm.PackageManager;
9191
import android.net.Uri;
92+
import android.provider.DocumentsContract;
9293
import android.provider.Settings;
9394

9495
import androidx.core.content.ContextCompat;
@@ -681,11 +682,32 @@ private void compareFileText(
681682
Path path = file.toPath();
682683
fileContent = new String(Files.readAllBytes(path), charset);
683684

684-
} else {
685-
// Handle content:// URIs
685+
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
686+
// Handle content:// URIs (including SAF tree URIs)
686687
InputStream inputStream = null;
687688
try {
688-
inputStream = context.getContentResolver().openInputStream(uri);
689+
String uriString = fileUri;
690+
Uri resolvedUri = uri;
691+
692+
// Check if this is a SAF tree URI with :: separator
693+
if (uriString.contains("::")) {
694+
try {
695+
// Split into tree URI and document ID
696+
String[] parts = uriString.split("::", 2);
697+
String treeUriStr = parts[0];
698+
String docId = parts[1];
699+
700+
// Build document URI directly from tree URI and document ID
701+
Uri treeUri = Uri.parse(treeUriStr);
702+
resolvedUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, docId);
703+
} catch (Exception e) {
704+
callback.error("SAF_FALLBACK: Invalid SAF URI format - " + e.getMessage());
705+
return;
706+
}
707+
}
708+
709+
// Try to open the resolved URI
710+
inputStream = context.getContentResolver().openInputStream(resolvedUri);
689711

690712
if (inputStream == null) {
691713
callback.error("Cannot open file");
@@ -710,6 +732,9 @@ private void compareFileText(
710732
} catch (IOException ignored) {}
711733
}
712734
}
735+
} else {
736+
callback.error("Unsupported URI scheme: " + uri.getScheme());
737+
return;
713738
}
714739

715740
// check length first

0 commit comments

Comments
 (0)