Skip to content

Commit 6382c9e

Browse files
authored
implement isSymlink (#5)
1 parent a1239cd commit 6382c9e

File tree

4 files changed

+630
-549
lines changed

4 files changed

+630
-549
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.foxdebug.acode.plugins
2+
3+
import android.content.Context
4+
import android.net.Uri
5+
import android.os.Build
6+
import android.provider.MediaStore
7+
import androidx.core.net.toFile
8+
import androidx.core.net.toUri
9+
import java.io.File
10+
import java.nio.file.Files
11+
12+
class AcodeNativeFs(private val context: Context) {
13+
14+
@Throws(Exception::class)
15+
fun isSymlink(uri: String): Boolean {
16+
val file = getFileFromUri(uri.toUri()) ?: return false
17+
if (file.exists().not()) return false
18+
19+
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
20+
Files.isSymbolicLink(file.toPath())
21+
} else file.canonicalPath != file.absolutePath
22+
}
23+
24+
private fun getFileFromUri(uri: Uri): File? {
25+
return when (uri.scheme) {
26+
"file" -> uri.toFile()
27+
28+
"content" -> {
29+
val projection = arrayOf(MediaStore.MediaColumns.DATA)
30+
context.contentResolver.query(uri, projection, null, null, null)?.use {
31+
val columnIndex = it.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA)
32+
if (it.moveToFirst()) {
33+
val filePath = it.getString(columnIndex)
34+
if (filePath != null) File(filePath) else null
35+
} else null
36+
}
37+
}
38+
39+
else -> null
40+
}
41+
}
42+
}

android/app/src/main/java/com/foxdebug/acode/plugins/NativeLayer.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.foxdebug.acode.plugins
22

33
import android.content.Intent
4-
import android.net.Uri
54
import android.os.Build
65
import androidx.appcompat.app.AlertDialog
6+
import androidx.core.net.toUri
77
import com.foxdebug.acode.Acode
88
import com.foxdebug.acode.runOnUiThread
99
import com.getcapacitor.JSObject
@@ -15,12 +15,13 @@ import kotlinx.coroutines.CoroutineName
1515
import kotlinx.coroutines.MainScope
1616
import kotlinx.coroutines.cancel
1717
import kotlinx.coroutines.plus
18-
import androidx.core.net.toUri
1918

2019
@CapacitorPlugin(name = "NativeLayer")
2120
class NativeLayer : Plugin() {
2221
val scope = MainScope() + CoroutineName("NativeLayer")
2322

23+
private val nativeFs = AcodeNativeFs(context)
24+
2425
override fun handleOnDestroy() {
2526
super.handleOnDestroy()
2627
scope.cancel()
@@ -196,4 +197,21 @@ class NativeLayer : Plugin() {
196197
}
197198
}
198199
}
200+
201+
@PluginMethod
202+
fun isSymlink(call: PluginCall) {
203+
val uri = call.getString("uri") ?: run {
204+
call.reject("Missing 'uri'")
205+
return
206+
}
207+
208+
val isSymlink = try {
209+
nativeFs.isSymlink(uri)
210+
} catch (e: Exception) {
211+
// call.reject("Failed to check if URI is a symlink: ${e.message}")
212+
false
213+
}
214+
215+
call.resolve(JSObject().put("isSymlink", isSymlink))
216+
}
199217
}

0 commit comments

Comments
 (0)