Improve Path/Uri conversion

This commit is contained in:
FooIbar committed 2024-07-09 21:51:41 +08:00
1 parent ae66318843
commit d00d700a4b
6 files changed
+37 -15

No files matched your search

@@ -1,7 +1,7 @@
package com.hippo.ehviewer.cronet
import com.hippo.ehviewer.Settings
import okio.Path.Companion.toOkioPath
import java.io.File
import org.chromium.net.CronetEngine
import splitties.init.appCtx
@@ -10,8 +10,8 @@ val cronetHttpClient: CronetEngine = CronetEngine.Builder(appCtx).apply {
setUserAgent(Settings.userAgent)
// Cache Quic hint only since the real cache mechanism should on Ktor layer
val cache = (appCtx.cacheDir.toOkioPath() / "http_cache").toFile().apply { mkdirs() }
setStoragePath(cache.absolutePath)
val cache = File(appCtx.cacheDir, "http_cache").apply { mkdirs() }
setStoragePath(cache.path)
enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, 4096)
addQuicHint("e-hentai.org", 443, 443)
@@ -96,7 +96,9 @@ fun DownloadScreen(navigator: DestinationsNavigator) = composing(navigator) {
launchIO {
runCatching {
contentResolver.takePersistableUriPermission(treeUri, FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION)
downloadLocationState = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)).toOkioPath()
val path = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri)).toOkioPath()
check(path.isDirectory) { "$path is not a directory" }
downloadLocationState = path
launchNonCancellable {
keepNoMediaFileStatus(downloadLocationState)
}
@@ -74,7 +74,7 @@ object Crash {
}
if (t is OutOfMemoryError) {
val dumpFile = File(dir, "hprof-$nowString.hprof")
Debug.dumpHprofData(dumpFile.absolutePath)
Debug.dumpHprofData(dumpFile.path)
}
}
}
@@ -14,11 +14,20 @@ val Uri.displayPath: String?
return path
}
if (DocumentsContract.isDocumentUri(appCtx, this)) {
val context = appCtx
if (DocumentsContract.isDocumentUri(context, this)) {
val (type, path) = DocumentsContract.getDocumentId(this).split(":", limit = 2)
if (authority == "com.android.externalstorage.documents") {
val (type, id) = DocumentsContract.getDocumentId(this).split(":", limit = 2)
if (type == "primary") {
return Environment.getExternalStorageDirectory().absolutePath + "/" + id
return Environment.getExternalStorageDirectory().path + "/" + path
}
}
context.externalCacheDirs.forEach {
val cachePath = it.path
val index = cachePath.indexOf(type)
if (index != -1) {
return cachePath.substring(0, index + type.length) + "/" + path
}
}
}
@@ -190,7 +190,7 @@ class AndroidFileSystem(context: Context) : FileSystem() {
}
}
private fun Path.isPhysicalFile() = toString().first() == '/'
private fun Path.isPhysicalFile() = toString().startsWith('/')
private fun Uri.isCifsDocument() = authority == "com.wa2c.android.cifsdocumentsprovider.documents"
+17 -6
View File
@@ -1,5 +1,6 @@
package com.hippo.files
import android.content.ContentResolver
import android.net.Uri
import android.os.ParcelFileDescriptor
import androidx.core.net.toUri
@@ -30,10 +31,16 @@ fun Path.openInputStream(): FileInputStream =
fun Path.openOutputStream(): FileOutputStream =
ParcelFileDescriptor.AutoCloseOutputStream(SystemFileSystem.openFileDescriptor(this, "wt"))
fun Path.toUri(): Uri = toString().replaceFirst("content:/", "content://").toUri().let {
val paths = it.pathSegments
if (paths.size > 3 && paths[0] == "tree") {
it.buildUpon().apply {
fun Path.toUri(): Uri {
val str = toString()
if (str.startsWith('/')) {
return toFile().toUri()
}
val uri = str.replaceFirst("content:/", "content://").toUri()
val paths = uri.pathSegments
return if (paths.size > 3 && paths[0] == "tree") {
uri.buildUpon().apply {
path(null)
repeat(3) { i ->
appendPath(paths[i])
@@ -41,8 +48,12 @@ fun Path.toUri(): Uri = toString().replaceFirst("content:/", "content://").toUri
appendPath(paths.subList(3, paths.size).joinToString("/").replaceFirst(":/", ":"))
}.build()
} else {
it
uri
}
}
fun Uri.toOkioPath() = toString().toPath()
fun Uri.toOkioPath() = if (scheme == ContentResolver.SCHEME_FILE) {
path!!
} else {
toString()
}.toPath()