From 7b4aa3c400c59b71a2eb2ebbc784597f88936eb0 Mon Sep 17 00:00:00 2001 From: Paul Woitaschek Date: Mon, 1 Aug 2022 16:44:03 +0200 Subject: [PATCH] Add a fallback for the book document name. --- .../kotlin/voice/app/scanner/MediaScanner.kt | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/scanner/src/main/kotlin/voice/app/scanner/MediaScanner.kt b/scanner/src/main/kotlin/voice/app/scanner/MediaScanner.kt index eab1c5358d..c1dc1bb0eb 100644 --- a/scanner/src/main/kotlin/voice/app/scanner/MediaScanner.kt +++ b/scanner/src/main/kotlin/voice/app/scanner/MediaScanner.kt @@ -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 @@ -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, @@ -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) { // the init block performs integrity validation Book(content, chapters)