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 {
|
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
|
if (!DocumentsContract.isTreeUri(uri)) return null
|
||||||
val documentPathFactory = DocumentPathFactory.from(context)
|
val documentPathFactory = DocumentPathFactory.from(context)
|
||||||
val path = documentPathFactory.unpackDocumentTreeUri(uri) ?: return null
|
return documentPathFactory.unpackDocumentTreeUri(uri)
|
||||||
return Unopened(uri, path)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
package org.oxycblt.musikr.fs
|
package org.oxycblt.musikr.fs
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.net.Uri
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,6 +76,12 @@ sealed interface Volume {
|
|||||||
/** The UUID of the volume. */
|
/** The UUID of the volume. */
|
||||||
val id: String?
|
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) =
|
fun parseWindows(path: String) =
|
||||||
Components(path.trimSlashes().split('\\').filter { it.isNotEmpty() })
|
Components(path.trimSlashes().split('\\').filter { it.isNotEmpty() })
|
||||||
|
|
||||||
|
fun root() = Components(emptyList())
|
||||||
|
|
||||||
private fun String.trimSlashes() = trimStart(File.separatorChar).trimEnd(File.separatorChar)
|
private fun String.trimSlashes() = trimStart(File.separatorChar).trimEnd(File.separatorChar)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import org.oxycblt.musikr.fs.saf.useQuery
|
|||||||
*/
|
*/
|
||||||
internal interface DocumentPathFactory {
|
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.
|
* @param uri The document URI to unpack.
|
||||||
* @return The [Path] instance, or null if the URI could not be unpacked.
|
* @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?
|
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.
|
* @param uri The document tree URI to unpack.
|
||||||
* @return The [Path] instance, or null if the URI could not be unpacked.
|
* @return The [Path] instance, or null if the URI could not be unpacked.
|
||||||
*/
|
*/
|
||||||
fun unpackDocumentTreeUri(uri: Uri): Path?
|
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 {
|
companion object {
|
||||||
fun from(context: Context): DocumentPathFactory {
|
fun from(context: Context): DocumentPathFactory {
|
||||||
val volumeManager = VolumeManager.from(context)
|
val volumeManager = VolumeManager.from(context)
|
||||||
@@ -122,16 +106,7 @@ private class DocumentPathFactoryImpl(
|
|||||||
return fromDocumentId(treeUri)
|
return fromDocumentId(treeUri)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toDocumentId(path: Path): String =
|
private fun fromDocumentId(path: String): Path? {
|
||||||
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? {
|
|
||||||
// Document tree URIs consist of a prefixed volume name followed by a relative path,
|
// Document tree URIs consist of a prefixed volume name followed by a relative path,
|
||||||
// delimited with a colon.
|
// delimited with a colon.
|
||||||
val split = path.split(File.pathSeparator, limit = 2)
|
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.Playlist
|
||||||
import org.oxycblt.musikr.fs.Components
|
import org.oxycblt.musikr.fs.Components
|
||||||
import org.oxycblt.musikr.fs.Path
|
import org.oxycblt.musikr.fs.Path
|
||||||
|
import org.oxycblt.musikr.fs.Volume
|
||||||
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
||||||
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
import org.oxycblt.musikr.fs.saf.contentResolverSafe
|
||||||
import org.oxycblt.musikr.playlist.m3u.M3U
|
import org.oxycblt.musikr.playlist.m3u.M3U
|
||||||
@@ -93,7 +94,7 @@ private class ExternalPlaylistManagerImpl(
|
|||||||
private val m3u: M3U,
|
private val m3u: M3U,
|
||||||
) : ExternalPlaylistManager {
|
) : ExternalPlaylistManager {
|
||||||
override suspend fun import(uri: Uri): ImportedPlaylist? {
|
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 {
|
return try {
|
||||||
context.contentResolverSafe.openInputStream(uri)?.use {
|
context.contentResolverSafe.openInputStream(uri)?.use {
|
||||||
@@ -118,7 +119,7 @@ private class ExternalPlaylistManagerImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun export(playlist: Playlist, uri: Uri, config: ExportConfig): Boolean {
|
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 =
|
val workingDirectory =
|
||||||
if (config.absolute) {
|
if (config.absolute) {
|
||||||
Path(filePath.volume, Components.parseUnix("/"))
|
Path(filePath.volume, Components.parseUnix("/"))
|
||||||
|
|||||||
Reference in New Issue
Block a user