musikr: more gracefully handle third-party paths
This commit is contained in:
@@ -60,11 +60,13 @@ sealed class Location(val uri: Uri, val path: Path) {
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun from(context: Context, uri: Uri): Unopened? {
|
||||
fun from(context: Context, uri: Uri) =
|
||||
Unopened(uri, getTreePath(context, uri) ?: Path(Volume.ThirdParty(uri), Components.root()))
|
||||
|
||||
private fun getTreePath(context: Context, uri: Uri): Path? {
|
||||
if (!DocumentsContract.isTreeUri(uri)) return null
|
||||
val documentPathFactory = DocumentPathFactory.from(context)
|
||||
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
|
||||
return Unopened(uri, path)
|
||||
return documentPathFactory.unpackDocumentTreeUri(uri)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
package org.oxycblt.musikr.fs
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -75,6 +76,12 @@ sealed interface Volume {
|
||||
/** The UUID of the volume. */
|
||||
val id: String?
|
||||
}
|
||||
|
||||
data class ThirdParty(val uri: Uri) : Volume {
|
||||
override val mediaStoreName: String? = null
|
||||
override val components: Components? = null
|
||||
override fun resolveName(context: Context) = uri.toString()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -170,6 +177,8 @@ value class Components private constructor(val components: List<String>) {
|
||||
fun parseWindows(path: String) =
|
||||
Components(path.trimSlashes().split('\\').filter { it.isNotEmpty() })
|
||||
|
||||
fun root() = Components(emptyList())
|
||||
|
||||
private fun String.trimSlashes() = trimStart(File.separatorChar).trimEnd(File.separatorChar)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ import org.oxycblt.musikr.fs.saf.useQuery
|
||||
*/
|
||||
internal interface DocumentPathFactory {
|
||||
/**
|
||||
* Unpacks a document URI into a [Path] instance, using [fromDocumentId].
|
||||
* Unpacks a document URI into a [Path] instance.
|
||||
*
|
||||
* @param uri The document URI to unpack.
|
||||
* @return The [Path] instance, or null if the URI could not be unpacked.
|
||||
@@ -44,29 +44,13 @@ internal interface DocumentPathFactory {
|
||||
fun unpackDocumentUri(uri: Uri): Path?
|
||||
|
||||
/**
|
||||
* Unpacks a document tree URI into a [Path] instance, using [fromDocumentId].
|
||||
* Unpacks a document tree URI into a [Path] instance.
|
||||
*
|
||||
* @param uri The document tree URI to unpack.
|
||||
* @return The [Path] instance, or null if the URI could not be unpacked.
|
||||
*/
|
||||
fun unpackDocumentTreeUri(uri: Uri): Path?
|
||||
|
||||
/**
|
||||
* Serializes a [Path] instance into a document tree URI format path.
|
||||
*
|
||||
* @param path The [Path] instance to serialize.
|
||||
* @return The serialized path.
|
||||
*/
|
||||
fun toDocumentId(path: Path): String
|
||||
|
||||
/**
|
||||
* Deserializes a document tree URI format path into a [Path] instance.
|
||||
*
|
||||
* @param path The path to deserialize.
|
||||
* @return The [Path] instance, or null if the path could not be deserialized.
|
||||
*/
|
||||
fun fromDocumentId(path: String): Path?
|
||||
|
||||
companion object {
|
||||
fun from(context: Context): DocumentPathFactory {
|
||||
val volumeManager = VolumeManager.from(context)
|
||||
@@ -122,16 +106,7 @@ private class DocumentPathFactoryImpl(
|
||||
return fromDocumentId(treeUri)
|
||||
}
|
||||
|
||||
override fun toDocumentId(path: Path): String =
|
||||
when (val volume = path.volume) {
|
||||
// The primary storage has a volume prefix of "primary", regardless
|
||||
// of if it's internal or not.
|
||||
is Volume.Internal -> "$DOCUMENT_URI_PRIMARY_NAME:${path.components}"
|
||||
// Document tree URIs consist of a prefixed volume name followed by a relative path.
|
||||
is Volume.External -> "${volume.id}:${path.components}"
|
||||
}
|
||||
|
||||
override fun fromDocumentId(path: String): Path? {
|
||||
private fun fromDocumentId(path: String): Path? {
|
||||
// Document tree URIs consist of a prefixed volume name followed by a relative path,
|
||||
// delimited with a colon.
|
||||
val split = path.split(File.pathSeparator, limit = 2)
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.net.Uri
|
||||
import org.oxycblt.musikr.Playlist
|
||||
import org.oxycblt.musikr.fs.Components
|
||||
import org.oxycblt.musikr.fs.Path
|
||||
import org.oxycblt.musikr.fs.Volume
|
||||
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
||||
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||
import org.oxycblt.musikr.playlist.m3u.M3U
|
||||
@@ -93,7 +94,7 @@ private class ExternalPlaylistManagerImpl(
|
||||
private val m3u: M3U,
|
||||
) : ExternalPlaylistManager {
|
||||
override suspend fun import(uri: Uri): ImportedPlaylist? {
|
||||
val filePath = documentPathFactory.unpackDocumentUri(uri) ?: return null
|
||||
val filePath = documentPathFactory.unpackDocumentUri(uri) ?: Path(Volume.ThirdParty(uri), Components.root())
|
||||
|
||||
return try {
|
||||
context.contentResolverSafe.openInputStream(uri)?.use {
|
||||
@@ -118,7 +119,7 @@ private class ExternalPlaylistManagerImpl(
|
||||
}
|
||||
|
||||
override suspend fun export(playlist: Playlist, uri: Uri, config: ExportConfig): Boolean {
|
||||
val filePath = documentPathFactory.unpackDocumentUri(uri) ?: return false
|
||||
val filePath = documentPathFactory.unpackDocumentUri(uri) ?: Path(Volume.ThirdParty(uri), Components.root())
|
||||
val workingDirectory =
|
||||
if (config.absolute) {
|
||||
Path(filePath.volume, Components.parseUnix("/"))
|
||||
|
||||
Reference in New Issue
Block a user