Build direct path to shared storage folder

This commit is contained in:
Nicholas Wallace
2025-04-13 14:45:32 -07:00
parent 80bf3f12b6
commit 76c4ec542b
2 changed files with 35 additions and 29 deletions
@@ -231,6 +231,13 @@ class FolderScanner(var ctx: Context) {
cb(downloadItemScanResult) cb(downloadItemScanResult)
} }
// Find a folder by path in the DocumentFile tree
private fun findFolderByPath(root: DocumentFile, subPath: String): DocumentFile? {
var current = root
subPath.split("/").forEach { name -> current = current.findFile(name) ?: return null }
return current
}
// Scan item after download and create local library item // Scan item after download and create local library item
fun scanDownloadItem(downloadItem: DownloadItem, cb: (DownloadItemScanResult?) -> Unit) { fun scanDownloadItem(downloadItem: DownloadItem, cb: (DownloadItemScanResult?) -> Unit) {
// If downloading to internal storage handle separately // If downloading to internal storage handle separately
@@ -239,28 +246,33 @@ class FolderScanner(var ctx: Context) {
return return
} }
val folderDf = DocumentFileCompat.fromUri(ctx, Uri.parse(downloadItem.localFolder.contentUrl)) Log.d(tag, "starting my custom scanner")
val foldersFound = folderDf?.search(true, DocumentFileType.FOLDER) ?: mutableListOf() // Get the root by stripping off the localfolder content url from the itemfolderPath
val rootPathUri = Uri.parse(downloadItem.localFolder.contentUrl)
var itemFolderId = "" Log.d(tag, "root path URI after concat: ${rootPathUri}")
var itemFolderUrl = "" val root = DocumentFileCompat.fromUri(ctx, rootPathUri)
var itemFolderBasePath = "" Log.d(tag, "Root Doc File ${root?.uri} | ${root?.name} | ${root?.length()}")
var itemFolderAbsolutePath = "" if (root == null) {
foldersFound.forEach { Log.e(tag, "Root Doc File Invalid ${rootPathUri}")
// e.g. absolute path is "storage/emulated/0/Audiobooks/Orson Scott Card/Enders Game"
// and itemSubfolder is "Orson Scott Card/Enders Game"
if (it.getAbsolutePath(ctx).endsWith(downloadItem.itemSubfolder)) {
itemFolderId = it.id
itemFolderUrl = it.uri.toString()
itemFolderBasePath = it.getBasePath(ctx)
itemFolderAbsolutePath = it.getAbsolutePath(ctx)
}
}
if (itemFolderUrl == "") {
Log.d(tag, "scanDownloadItem failed to find media folder")
return cb(null) return cb(null)
} }
Log.d(tag, "item folder path: ${downloadItem.itemSubfolder}")
val baseFolder = findFolderByPath(root, downloadItem.itemSubfolder)
if (baseFolder == null) {
Log.e(tag, "Base folder not found ${downloadItem.itemSubfolder}")
return cb(null)
}
Log.d(tag, "baseFolder: ${baseFolder.uri} | ${baseFolder.name} | ${baseFolder.length()}")
// Build references to files in this library item folder
// e.g. absolute path is "storage/emulated/0/Audiobooks/Orson Scott Card/Enders Game"
// and itemSubfolder is "Orson Scott Card/Enders Game"
val itemFolderId = baseFolder.id
val itemFolderUrl = baseFolder.uri.toString()
val itemFolderBasePath = baseFolder.getBasePath(ctx)
val itemFolderAbsolutePath = baseFolder.getAbsolutePath(ctx)
val df: DocumentFile? = DocumentFileCompat.fromUri(ctx, Uri.parse(itemFolderUrl)) val df: DocumentFile? = DocumentFileCompat.fromUri(ctx, Uri.parse(itemFolderUrl))
if (df == null) { if (df == null) {
@@ -271,18 +283,12 @@ class FolderScanner(var ctx: Context) {
val localLibraryItemId = getLocalLibraryItemId(itemFolderId) val localLibraryItemId = getLocalLibraryItemId(itemFolderId)
Log.d( Log.d(
tag, tag,
"scanDownloadItem starting for ${downloadItem.itemFolderPath} | ${df.uri} | Item Folder Id:$itemFolderId | LLI Id:$localLibraryItemId" "scanDownloadItem starting for ${downloadItem.itemFolderPath} | ${baseFolder.uri} | Item Folder Id:$itemFolderId | LLI Id:$localLibraryItemId"
) )
// Search for files in media item folder // Search for files in media item folder
// m4b files showing as mimeType application/octet-stream on Android 10 and earlier see #154 // m4b files showing as mimeType application/octet-stream on Android 10 and earlier see #154
val filesFound = val filesFound = baseFolder.listFiles()
df.search(
false,
DocumentFileType.FILE,
arrayOf("audio/*", "image/*", "video/mp4", "application/*")
)
Log.d(tag, "scanDownloadItem ${filesFound.size} files found in ${downloadItem.itemFolderPath}")
var localEpisodeId: String? = null var localEpisodeId: String? = null
var localLibraryItem: LocalLibraryItem? var localLibraryItem: LocalLibraryItem?
@@ -195,7 +195,7 @@ class AbsDownloader : Plugin() {
itemFolderPath, itemFolderPath,
localFolder, localFolder,
title, title,
title, itemSubfolder,
libraryItem.media, libraryItem.media,
mutableListOf() mutableListOf()
) )