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

@@ -0,0 +1,113 @@
package win.taptap.musicmanager.media
import java.nio.file.Files
import java.nio.file.attribute.FileTime
import java.time.Instant
import java.time.temporal.ChronoUnit
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.LoggerFactory
import win.taptap.musicmanager.db.MoodRecommendationsTable
import win.taptap.musicmanager.db.MoodSongsTable
import win.taptap.musicmanager.db.PlaybackSessionsTable
import win.taptap.musicmanager.db.SessionItemsTable
import win.taptap.musicmanager.db.SongRecord
import win.taptap.musicmanager.db.SongRepository
import win.taptap.musicmanager.scanner.CoverArtService
/**
* Reference-counted file cleanup. A song's file is only deleted when no mood
* references it, no recommendation slot still points at it and no open playback
* session has it queued.
*/
class MediaGcService(
private val mediaStore: MediaStore,
private val coverArtService: CoverArtService,
) {
private val log = LoggerFactory.getLogger(MediaGcService::class.java)
/** Deletes every unreferenced song row, file and cover. Returns the count. */
fun collectGarbage(): Int {
var deleted = 0
SongRepository.all().forEach { song ->
if (collect(song)) deleted++
}
collectOrphanDirectories()
return deleted
}
/** Deletes [song] if it has no references. */
fun collect(song: SongRecord): Boolean {
if (isReferenced(song.id)) return false
if (!SongRepository.delete(song.id)) return false
coverArtService.deleteCover(song.id)
mediaStore.deleteSongDirectory(song.videoId)
log.info("GC removed unreferenced song {} ({})", song.id, song.videoId)
return true
}
fun collect(songId: String): Boolean {
val song = SongRepository.byId(songId) ?: return false
return collect(song)
}
fun isReferenced(songId: String): Boolean = transaction {
val inMood = MoodSongsTable.selectAll()
.where { MoodSongsTable.songId eq songId }
.any()
val inRecommendation = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.songId eq songId) and
(MoodRecommendationsTable.state inList listOf("downloading", "ready", "added", "failed"))
}
.any()
val sessionIds = SessionItemsTable.select(SessionItemsTable.sessionId)
.where { SessionItemsTable.songId eq songId }
.map { it[SessionItemsTable.sessionId] }
val inOpenSession = if (sessionIds.isEmpty()) {
false
} else {
PlaybackSessionsTable.selectAll()
.where {
(PlaybackSessionsTable.id inList sessionIds) and
PlaybackSessionsTable.closedAt.isNull()
}
.any()
}
inMood || inRecommendation || inOpenSession
}
/**
* A crash between moving a file and indexing its row leaves a directory that
* no song points at. Only remove directories that have been idle for a while
* so a download that is still in progress is never touched.
*/
private fun collectOrphanDirectories() {
val cutoff = Instant.now().minus(ORPHAN_GRACE_MINUTES, ChronoUnit.MINUTES)
val known = SongRepository.all().map { it.videoId }.toSet()
mediaStore.songDirectories().forEach { directory ->
val videoId = directory.fileName?.toString() ?: return@forEach
if (videoId in known) return@forEach
val modified = runCatching {
Files.getLastModifiedTime(directory)
}.getOrNull() ?: return@forEach
if (modified.toInstant().isAfter(cutoff)) return@forEach
log.info("GC removed orphan media directory {}", directory)
runCatching { directory.toFile().deleteRecursively() }
}
}
private companion object {
const val ORPHAN_GRACE_MINUTES = 30L
}
}