feat: rebuild AudioManager around downloadable moods
CI / chart (push) Successful in 3s
CI / test (push) Successful in 2m2s
CI / chart-push (push) Successful in 4s
CI / image (push) Successful in 3m20s

- V8 wipe migration and the shared song store keyed by YouTube video id
- media store, indexer, downloader, recommendation top-up and refcounted GC
- mood songs plus the recommendation lifecycle and mixed radio pools
- new search, song and recommendation API; remove library/longform/seed routes
- chart PVC/media env, docs, Nix, README and an end-to-end smoke test
This commit is contained in:
TapTap committed 2026-11-15 21:37:39 +00:00
1 parent 50d8b6c580
commit 824eb36dd2
59 files changed
+3838 -5276

No files matched your search

@@ -1,26 +1,26 @@
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.config.LibraryConfig
import win.taptap.musicmanager.config.MusicFolder
import win.taptap.musicmanager.db.MediaRepository
import win.taptap.musicmanager.db.SongRepository
import win.taptap.musicmanager.media.MediaStore
/**
* Recursive MP3 scanner with delta updates and orphan cleanup.
* 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. The file's `(mtime, size)` pair decides whether
* 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 library: LibraryConfig,
private val coverArtService: CoverArtService,
private val tagExtractor: Mp3TagExtractor = Mp3TagExtractor(),
private val repository: MediaRepository = MediaRepository,
private val mediaStore: MediaStore,
private val indexer: SongIndexer,
private val onScanFinished: (ScanStatus) -> Unit = {},
) {
@@ -49,92 +49,82 @@ class MediaScanner(
var count = 0L
val seenSongIds = mutableSetOf<String>()
var error: String? = null
var folderFailed = false
var rootFailed = false
try {
library.folders.forEach { folder ->
try {
scanFolder(folder, seenSongIds) {
count++
status.set(ScanStatus(scanning = true, count = count))
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 videoId = mediaStore.videoIdFrom(relativePath)
if (videoId == null) {
log.debug("Skipping file outside a video folder: {}", file)
return@forEach
}
try {
val existing = SongRepository.byVideoId(videoId)
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(videoId, 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.byVideoId(videoId)?.let { seenSongIds += it.id }
log.warn("Could not index file: $file", e)
}
}
} catch (e: Exception) {
folderFailed = true
if (error == null) {
error = "Could not scan folder ${folder.id} (${folder.path}): " +
(e.message ?: e::class.simpleName)
}
log.error("Could not scan folder ${folder.id} (${folder.path})", e)
}
}
if (folderFailed) {
// An unmounted or temporarily unreadable folder must not be
// mistaken for deleted files. Keep the last known library.
log.warn("Skipping orphan cleanup because at least one folder failed to scan")
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 {
repository.deleteSongsNotIn(seenSongIds)
SongRepository.all().forEach { song ->
if (song.id !in seenSongIds) {
SongRepository.delete(song.id)
}
}
}
repository.rebuildAggregates()
} catch (t: Throwable) {
error = t.message ?: t::class.simpleName
log.error("Library scan failed after $count files", t)
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("Library scan finished: {} files, error={}", count, error)
log.info("Media scan finished: {} files, error={}", count, error)
runCatching { onScanFinished(finalStatus) }
.onFailure { log.warn("Scan completion hook failed", it) }
return finalStatus
}
private fun scanFolder(folder: MusicFolder, seenSongIds: MutableSet<String>, onFile: () -> Unit) {
Files.walk(folder.path).use { paths ->
paths.filter { Files.isRegularFile(it) }.forEach { file ->
if (!file.fileName.toString().endsWith(".mp3", ignoreCase = true)) {
log.debug("Skipping unsupported file: {}", file)
return@forEach
}
onFile()
val relativePath = folder.path.relativize(file).joinToString("/") { it.toString() }
val existing = repository.findExisting(folder.id, relativePath)
try {
val attributes = Files.readAttributes(file, java.nio.file.attribute.BasicFileAttributes::class.java)
val mtime = attributes.lastModifiedTime().toMillis()
val size = attributes.size()
if (existing != null && existing.fileMtime == mtime && existing.size == size) {
seenSongIds += existing.id
return@forEach
}
val track = tagExtractor.extract(folder.id, folder.path, file)
if (track == null) {
if (existing != null) seenSongIds += existing.id
log.warn("Could not read MP3 metadata: {}", file)
return@forEach
}
val coverBytes = track.coverBytes
?: coverArtService.findSidecarCover(file)?.let(coverArtService::readCover)
repository.upsertTrack(track)
if (coverBytes != null && coverArtService.saveCover(track.albumId, coverBytes)) {
repository.setAlbumCover(track.albumId)
}
seenSongIds += track.songId
} catch (e: Exception) {
if (existing != null) seenSongIds += existing.id
log.warn("Could not index file: $file", e)
}
}
}
}
}