Files
AudioManager/src/main/kotlin/win/taptap/musicmanager/scanner/MediaScanner.kt
T
TapTap 141698282d
CI / chart (push) Successful in 3s
CI / test (push) Successful in 2m23s
CI / chart-push (push) Successful in 5s
CI / image (push) Successful in 3m50s
feat: add Deezer as the default download source
- V9 adds source/external_id to songs, moods, recommendations and jobs
- Deezer public catalog for search, related album covers and metadata
- streamrip downloader configured with DEEZER_ARL, 128/320 MP3
- per-mood source selector; switching clears and refills recommendations
- keep YouTube Music as a full second catalog and downloader
- media layout <source>-<externalId>, legacy YouTube folders still read
- Docker/streamrip install, chart ARL secret, docs and dual-source smoke test
2026-11-17 07:32:34 +00:00

132 lines
5.6 KiB
Kotlin

package win.taptap.musicmanager.scanner
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.attribute.BasicFileAttributes
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.slf4j.LoggerFactory
import win.taptap.musicmanager.db.SongRepository
import win.taptap.musicmanager.media.MediaStore
/**
* Rebuilds the song index from the media root. The first path segment is the
* YouTube video id, so a file's identity never depends on its tags.
*
* Only one scan can run at a time. A file's `(mtime, size)` pair decides whether
* tags need to be re-read; unchanged files are skipped.
*/
class MediaScanner(
private val mediaStore: MediaStore,
private val indexer: SongIndexer,
private val onScanFinished: (ScanStatus) -> Unit = {},
) {
private val log = LoggerFactory.getLogger(MediaScanner::class.java)
private val running = AtomicBoolean(false)
private val status = AtomicReference(ScanStatus(scanning = false, count = 0))
fun status(): ScanStatus = status.get()
/** Starts a scan in [scope]. Returns false when a scan is already running. */
fun start(scope: CoroutineScope): Boolean {
if (!running.compareAndSet(false, true)) return false
status.set(ScanStatus(scanning = true, count = 0))
scope.launch { runScan() }
return true
}
/** Blocking variant for tests and the startup scan. */
fun scanBlocking(): ScanStatus {
check(running.compareAndSet(false, true)) { "A scan is already running" }
status.set(ScanStatus(scanning = true, count = 0))
return runScan()
}
private fun runScan(): ScanStatus {
var count = 0L
val seenSongIds = mutableSetOf<String>()
var error: String? = null
var rootFailed = false
try {
val root = mediaStore.root
if (!Files.isDirectory(root) || !Files.isReadable(root)) {
rootFailed = true
error = "Media root is not readable: $root"
log.error("Skipping scan: media root is not readable: {}", root)
} else {
Files.walk(root).use { paths ->
paths.filter { Files.isRegularFile(it) }.forEach { file ->
if (!file.fileName.toString().endsWith(".mp3", ignoreCase = true)) {
return@forEach
}
val relativePath = root.relativize(file).joinToString("/") { it.toString() }
val sourceAndId = mediaStore.sourceAndIdFromPath(relativePath)
if (sourceAndId == null) {
log.debug("Skipping file outside a media folder: {}", file)
return@forEach
}
val (source, externalId) = sourceAndId
try {
val existing = SongRepository.byExternalId(source, externalId)
val attributes = Files.readAttributes(
file,
BasicFileAttributes::class.java,
)
val mtime = attributes.lastModifiedTime().toMillis()
val size = attributes.size()
if (existing != null && existing.fileMtime == mtime && existing.size == size) {
seenSongIds += existing.id
count++
status.set(ScanStatus(scanning = true, count = count))
return@forEach
}
val song = indexer.index(source, externalId, file)
if (song == null) {
if (existing != null) seenSongIds += existing.id
log.warn("Could not read MP3 metadata: {}", file)
return@forEach
}
seenSongIds += song.id
count++
status.set(ScanStatus(scanning = true, count = count))
} catch (e: Exception) {
SongRepository.byExternalId(source, externalId)?.let { seenSongIds += it.id }
log.warn("Could not index file: $file", e)
}
}
}
}
if (rootFailed) {
// An unmounted or temporarily unreadable root must not be
// mistaken for deleted files. Keep the last known index.
log.warn("Skipping orphan cleanup because the media root failed to scan")
} else {
SongRepository.all().forEach { song ->
if (song.id !in seenSongIds) {
SongRepository.delete(song.id)
}
}
}
} catch (t: Throwable) {
error = t.message ?: t::class.simpleName
log.error("Media scan failed after $count files", t)
} finally {
running.set(false)
}
val finalStatus = ScanStatus(scanning = false, count = count, error = error)
status.set(finalStatus)
log.info("Media scan finished: {} files, error={}", count, error)
runCatching { onScanFinished(finalStatus) }
.onFailure { log.warn("Scan completion hook failed", it) }
return finalStatus
}
}