mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-08-28 14:17:19 +02:00
Add: function to create LocalFile from download item part
This commit is contained in:
@@ -61,6 +61,7 @@ data class ServerConnectionConfig(
|
|||||||
data class LocalFile(
|
data class LocalFile(
|
||||||
var id: String,
|
var id: String,
|
||||||
var filename: String?,
|
var filename: String?,
|
||||||
|
var extension: String?,
|
||||||
var contentUrl: String,
|
var contentUrl: String,
|
||||||
var basePath: String,
|
var basePath: String,
|
||||||
var absolutePath: String,
|
var absolutePath: String,
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import androidx.documentfile.provider.DocumentFile
|
|||||||
import com.anggrayudi.storage.file.*
|
import com.anggrayudi.storage.file.*
|
||||||
import com.audiobookshelf.app.data.*
|
import com.audiobookshelf.app.data.*
|
||||||
import com.audiobookshelf.app.models.DownloadItem
|
import com.audiobookshelf.app.models.DownloadItem
|
||||||
|
import com.audiobookshelf.app.models.DownloadItemPart
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class FolderScanner(var ctx: Context) {
|
class FolderScanner(var ctx: Context) {
|
||||||
@@ -21,6 +22,46 @@ class FolderScanner(var ctx: Context) {
|
|||||||
return "local_" + DeviceManager.getBase64Id(mediaItemId)
|
return "local_" + DeviceManager.getBase64Id(mediaItemId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Create LocalFile from downloadItemPart */
|
||||||
|
private fun createLocalFile(downloadItemPart: DownloadItemPart): LocalFile? {
|
||||||
|
if (downloadItemPart.isInternalStorage) {
|
||||||
|
val file = File(downloadItemPart.finalDestinationPath)
|
||||||
|
val localFileId = DeviceManager.getBase64Id(file.name)
|
||||||
|
val localFile =
|
||||||
|
LocalFile(
|
||||||
|
localFileId,
|
||||||
|
file.name,
|
||||||
|
file.extension,
|
||||||
|
Uri.fromFile(file).toString(),
|
||||||
|
file.getBasePath(ctx),
|
||||||
|
file.absolutePath,
|
||||||
|
file.mimeType,
|
||||||
|
file.length()
|
||||||
|
)
|
||||||
|
return localFile
|
||||||
|
} else {
|
||||||
|
// Convert the file path to a Uri using SAF
|
||||||
|
val docFile = DocumentFileCompat.fromFullPath(ctx, downloadItemPart.finalDestinationPath)
|
||||||
|
if (docFile == null) {
|
||||||
|
Log.e(tag, "Doc File Invalid ${downloadItemPart.finalDestinationPath}")
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val localFileId = DeviceManager.getBase64Id(docFile.id)
|
||||||
|
val localFile =
|
||||||
|
LocalFile(
|
||||||
|
localFileId,
|
||||||
|
docFile.name,
|
||||||
|
docFile.extension,
|
||||||
|
docFile.uri.toString(),
|
||||||
|
docFile.getBasePath(ctx),
|
||||||
|
docFile.getAbsolutePath(ctx),
|
||||||
|
docFile.mimeType,
|
||||||
|
docFile.length()
|
||||||
|
)
|
||||||
|
return localFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun scanInternalDownloadItem(
|
private fun scanInternalDownloadItem(
|
||||||
downloadItem: DownloadItem,
|
downloadItem: DownloadItem,
|
||||||
cb: (DownloadItemScanResult?) -> Unit
|
cb: (DownloadItemScanResult?) -> Unit
|
||||||
@@ -71,16 +112,15 @@ class FolderScanner(var ctx: Context) {
|
|||||||
val file = File(downloadItemPart.finalDestinationPath)
|
val file = File(downloadItemPart.finalDestinationPath)
|
||||||
|
|
||||||
val localFileId = DeviceManager.getBase64Id(file.name)
|
val localFileId = DeviceManager.getBase64Id(file.name)
|
||||||
val localFile =
|
val localFile = createLocalFile(downloadItemPart)
|
||||||
LocalFile(
|
if (localFile == null) {
|
||||||
localFileId,
|
Log.e(
|
||||||
file.name,
|
tag,
|
||||||
Uri.fromFile(file).toString(),
|
"scanInternalDownloadItem: Null localFile for path ${downloadItemPart.finalDestinationPath}"
|
||||||
file.getBasePath(ctx),
|
)
|
||||||
file.absolutePath,
|
return cb(null)
|
||||||
file.mimeType,
|
}
|
||||||
file.length()
|
|
||||||
)
|
|
||||||
Log.d(tag, "Scan internal storage item created file ${file.name}")
|
Log.d(tag, "Scan internal storage item created file ${file.name}")
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
@@ -101,11 +141,11 @@ class FolderScanner(var ctx: Context) {
|
|||||||
|
|
||||||
val trackFileMetadata =
|
val trackFileMetadata =
|
||||||
FileMetadata(
|
FileMetadata(
|
||||||
file.name,
|
localFile.filename ?: "",
|
||||||
file.extension,
|
localFile.extension ?: "",
|
||||||
file.absolutePath,
|
localFile.absolutePath,
|
||||||
file.getBasePath(ctx),
|
localFile.basePath,
|
||||||
file.length()
|
localFile.size
|
||||||
)
|
)
|
||||||
// Create new audio track
|
// Create new audio track
|
||||||
val track =
|
val track =
|
||||||
@@ -140,7 +180,10 @@ class FolderScanner(var ctx: Context) {
|
|||||||
}
|
}
|
||||||
} else if (downloadItemPart.ebookFile != null) {
|
} else if (downloadItemPart.ebookFile != null) {
|
||||||
foundEBookFile = true
|
foundEBookFile = true
|
||||||
Log.d(tag, "scanInternalDownloadItem: Ebook file found with mimetype=${file.mimeType}")
|
Log.d(
|
||||||
|
tag,
|
||||||
|
"scanInternalDownloadItem: Ebook file found with mimetype=${localFile.mimeType}"
|
||||||
|
)
|
||||||
localLibraryItem.localFiles.add(localFile)
|
localLibraryItem.localFiles.add(localFile)
|
||||||
|
|
||||||
val ebookFile =
|
val ebookFile =
|
||||||
@@ -328,27 +371,24 @@ class FolderScanner(var ctx: Context) {
|
|||||||
val itemPart =
|
val itemPart =
|
||||||
downloadItem.downloadItemParts.find { itemPart -> itemPart.filename == docFile.name }
|
downloadItem.downloadItemParts.find { itemPart -> itemPart.filename == docFile.name }
|
||||||
|
|
||||||
// Build local file object
|
var localFile: LocalFile? = null
|
||||||
val localFileId = DeviceManager.getBase64Id(docFile.id)
|
|
||||||
val localFile =
|
if (itemPart != null) {
|
||||||
LocalFile(
|
|
||||||
localFileId,
|
// Build local file object
|
||||||
docFile.name,
|
localFile = createLocalFile(itemPart)
|
||||||
docFile.uri.toString(),
|
}
|
||||||
docFile.getBasePath(ctx),
|
|
||||||
docFile.getAbsolutePath(ctx),
|
|
||||||
docFile.mimeType,
|
|
||||||
docFile.length()
|
|
||||||
)
|
|
||||||
|
|
||||||
if (itemPart == null) {
|
if (itemPart == null) {
|
||||||
if (downloadItem.mediaType == "book"
|
if (downloadItem.mediaType == "book"
|
||||||
) { // for books every download item should be a file found
|
) { // for books every download item should be a file found
|
||||||
Log.e(
|
Log.e(
|
||||||
tag,
|
tag,
|
||||||
"scanDownloadItem: Item part not found for doc file ${localFile.filename} | ${localFile.absolutePath} | ${localFile.contentUrl}"
|
"scanDownloadItem: Item part not found for doc file ${localFile?.filename} | ${localFile?.absolutePath} | ${localFile?.contentUrl}"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
} else if (localFile == null) {
|
||||||
|
Log.e(tag, "scanDownloadItem: Local file is null for item part: ${itemPart.filename}")
|
||||||
} else if (itemPart.audioTrack != null) { // Is audio track
|
} else if (itemPart.audioTrack != null) { // Is audio track
|
||||||
val audioTrackFromServer = itemPart.audioTrack
|
val audioTrackFromServer = itemPart.audioTrack
|
||||||
Log.d(
|
Log.d(
|
||||||
@@ -362,7 +402,7 @@ class FolderScanner(var ctx: Context) {
|
|||||||
val trackFileMetadata =
|
val trackFileMetadata =
|
||||||
FileMetadata(
|
FileMetadata(
|
||||||
localFile.filename ?: "",
|
localFile.filename ?: "",
|
||||||
docFile.extension ?: "",
|
localFile.extension ?: "",
|
||||||
localFile.absolutePath,
|
localFile.absolutePath,
|
||||||
localFile.basePath,
|
localFile.basePath,
|
||||||
localFile.size
|
localFile.size
|
||||||
@@ -377,7 +417,7 @@ class FolderScanner(var ctx: Context) {
|
|||||||
localFile.mimeType ?: "",
|
localFile.mimeType ?: "",
|
||||||
trackFileMetadata,
|
trackFileMetadata,
|
||||||
true,
|
true,
|
||||||
localFileId,
|
localFile.id,
|
||||||
audioTrackFromServer.index
|
audioTrackFromServer.index
|
||||||
)
|
)
|
||||||
audioTracks.add(track)
|
audioTracks.add(track)
|
||||||
@@ -408,7 +448,7 @@ class FolderScanner(var ctx: Context) {
|
|||||||
itemPart.ebookFile.metadata,
|
itemPart.ebookFile.metadata,
|
||||||
itemPart.ebookFile.ebookFormat,
|
itemPart.ebookFile.ebookFormat,
|
||||||
true,
|
true,
|
||||||
localFileId,
|
localFile.id,
|
||||||
localFile.contentUrl
|
localFile.contentUrl
|
||||||
)
|
)
|
||||||
(localLibraryItem.media as Book).ebookFile = ebookFile
|
(localLibraryItem.media as Book).ebookFile = ebookFile
|
||||||
|
|||||||
Reference in New Issue
Block a user