feat: rebuild AudioManager around downloadable moods
- 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:
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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package win.taptap.musicmanager.media
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
|
||||
/**
|
||||
* Layout of the server-owned media root:
|
||||
*
|
||||
* <media>/<videoId>/<sanitized-title>.mp3
|
||||
*
|
||||
* The first path segment is the YouTube video id, which makes startup scans
|
||||
* deterministic and lets the downloader find its file without waiting for one.
|
||||
*/
|
||||
class MediaStore(val root: Path) {
|
||||
|
||||
init {
|
||||
Files.createDirectories(root)
|
||||
}
|
||||
|
||||
fun file(song: SongRecord): Path? = resolveRelative(song.relativePath)
|
||||
|
||||
fun resolveRelative(relativePath: String): Path? {
|
||||
val candidate = root.resolve(relativePath).normalize()
|
||||
return candidate.takeIf { it.startsWith(root) }
|
||||
}
|
||||
|
||||
/** Target path for a download. The title is sanitised for the filesystem. */
|
||||
fun target(videoId: String, title: String): Path =
|
||||
root.resolve(videoId).resolve("${sanitizeName(title)}.mp3")
|
||||
|
||||
fun relativePath(videoId: String, title: String): String =
|
||||
"$videoId/${sanitizeName(title)}.mp3"
|
||||
|
||||
fun videoIdFrom(relativePath: String): String? =
|
||||
relativePath.replace('\\', '/')
|
||||
.split('/')
|
||||
.firstOrNull()
|
||||
?.takeIf { it.isNotBlank() && it != "." && it != ".." }
|
||||
|
||||
fun deleteSongDirectory(videoId: String) {
|
||||
val dir = root.resolve(videoId).normalize()
|
||||
if (!dir.startsWith(root) || dir == root) return
|
||||
runCatching { dir.toFile().deleteRecursively() }
|
||||
}
|
||||
|
||||
fun songDirectories(): List<Path> {
|
||||
if (!Files.isDirectory(root)) return emptyList()
|
||||
return Files.list(root).use { stream ->
|
||||
stream.filter { Files.isDirectory(it) }.toList()
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val MAX_NAME_BYTES = 120
|
||||
private val INVALID = Regex("[\\\\/:*?\"<>|\\p{Cntrl}]")
|
||||
private val SPACES = Regex("\\s+")
|
||||
|
||||
fun sanitizeName(value: String): String =
|
||||
truncateUtf8(
|
||||
value.replace(INVALID, "_")
|
||||
.replace(SPACES, " ")
|
||||
.trim()
|
||||
.trim('.')
|
||||
.ifEmpty { "track" },
|
||||
MAX_NAME_BYTES,
|
||||
)
|
||||
|
||||
/** Linux NAME_MAX is 255 bytes, not characters; CJK titles are 3x. */
|
||||
fun truncateUtf8(value: String, maxBytes: Int): String {
|
||||
var bytes = 0
|
||||
val result = StringBuilder()
|
||||
for (character in value) {
|
||||
val length = character.toString().toByteArray(Charsets.UTF_8).size
|
||||
if (bytes + length > maxBytes) break
|
||||
result.append(character)
|
||||
bytes += length
|
||||
}
|
||||
return result.toString().ifEmpty { "track" }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user