Skip to content

Commit

Permalink
Add a fallback for the book document name.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Aug 1, 2022
1 parent ba31e2b commit 7b4aa3c
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions scanner/src/main/kotlin/voice/app/scanner/MediaScanner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import voice.data.Chapter
import voice.data.repo.BookContentRepo
import voice.data.repo.ChapterRepo
import voice.data.toUri
import voice.logging.core.Logger
import java.time.Instant
import javax.inject.Inject

Expand All @@ -31,22 +32,13 @@ class MediaScanner
val id = BookId(file.uri)
val content = contentRepo.getOrPut(id) {
val analyzed = mediaAnalyzer.analyze(chapterIds.first().toUri())
val name = analyzed?.bookName
?: file.name?.let { name ->
if (file.isFile) {
name.substringBeforeLast(".")
} else {
name
}
}
?: return
val content = BookContent(
id = id,
isActive = true,
addedAt = Instant.now(),
author = analyzed?.author,
lastPlayedAt = Instant.EPOCH,
name = name,
name = analyzed?.bookName ?: file.bookName(),
playbackSpeed = 1F,
skipSilence = false,
chapters = chapterIds,
Expand Down Expand Up @@ -75,6 +67,25 @@ class MediaScanner
}
}

private fun DocumentFile.bookName(): String {
val fileName = name
return if (fileName == null) {
uri.toString()
.removePrefix("/storage/emulated/0/")
.removePrefix("/storage/emulated/")
.removePrefix("/storage/")
.also {
Logger.e("Could not parse fileName from $this. Fallback to $it")
}
} else {
if (isFile) {
fileName.substringBeforeLast(".")
} else {
fileName
}
}
}

private fun validateIntegrity(content: BookContent, chapters: List<Chapter>) {
// the init block performs integrity validation
Book(content, chapters)
Expand Down

0 comments on commit 7b4aa3c

Please sign in to comment.