feat: download accepted recommendations and track longform progress
CI / chart (push) Successful in 4s
CI / chart-push (push) Successful in 4s
CI / test (push) Successful in 2m5s
CI / image (push) Successful in 3m14s

Accepted recommendations without a library match now enter a download queue.
A worker downloads the MP3 with yt-dlp into "<music>/AudioManager Imports",
runs a scan, matches the new song, links it to the recommendation and adds it
as a seed. Jobs retry three times and expose queued/downloading/done/failed
state (plus the error) on the recommendation DTO, so clients can show progress
and retry. Downloads can be disabled and the target folder chosen.

Long-form gets resume points: GET/PUT /longform/progress, scoped to longform
folders and to the owner, with automatic completion detection. Longform stays
excluded from moods and radio.

The chart mounts the music hostPath writable for imports and passes the new
DOWNLOAD_* settings; version 0.2.3.
This commit is contained in:
TapTap committed 2026-11-15 13:26:22 +00:00
1 parent 51d1b20bf6
commit a5af1ee83f
18 files changed
+996 -9

No files matched your search

@@ -5,6 +5,7 @@ import io.ktor.client.request.delete
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.post
import io.ktor.client.request.put
import io.ktor.client.request.setBody
import io.ktor.client.statement.bodyAsBytes
import io.ktor.client.statement.bodyAsText
@@ -50,6 +51,7 @@ import win.taptap.musicmanager.db.ArtistsTable
import win.taptap.musicmanager.db.ConfigRepository
import win.taptap.musicmanager.db.DatabaseFactory
import win.taptap.musicmanager.db.DatabaseHandle
import win.taptap.musicmanager.db.DownloadRepository
import win.taptap.musicmanager.db.SongArtistsTable
import win.taptap.musicmanager.db.SongGenresTable
import win.taptap.musicmanager.db.SongsTable
@@ -304,12 +306,20 @@ class ApiRoutesTest {
assertEquals("accepted", acceptedBody["recommendation"]!!.jsonObject.string("state"))
assertEquals(2, acceptedBody["detail"]!!.jsonObject["mood"]!!.jsonObject.int("seedCount"))
// Unknown tracks are accepted for the future downloader.
// Unknown tracks are accepted and queued for automatic download.
val acceptedNew = client.post("/api/v1/moods/$moodId/recommendations/v-new/accept") {
bearer(token)
}
val acceptedNewBody = parse(acceptedNew.bodyAsText())
assertFalse(acceptedNewBody["seedAdded"]!!.jsonPrimitive.content.toBoolean())
assertEquals(
"queued",
DownloadRepository.forRecommendation("alice", moodId, "v-new")?.state,
)
assertEquals(
"queued",
acceptedNewBody["recommendation"]!!.jsonObject["download"]!!.jsonObject.string("state"),
)
assertEquals(
"accepted",
acceptedNewBody["recommendation"]!!.jsonObject.string("state"),
@@ -344,6 +354,47 @@ class ApiRoutesTest {
}
}
@Test
fun `longform progress is saved resumed and owner scoped`() {
seededContext().use { seed ->
testApplication {
setupApp(seed)
val alice = client.token("alice", "alice-pw")
val saved = client.put("/api/v1/longform/progress/${seed.longformSong}") {
bearer(alice)
contentType(ContentType.Application.Json)
setBody("""{"positionSeconds":42,"durationSeconds":120}""")
}
assertEquals(HttpStatusCode.NoContent, saved.status)
val list = parseBody(
client.get("/api/v1/longform/progress") { bearer(alice) }.bodyAsText(),
)
val item = list["items"]!!.jsonArray.single().jsonObject
assertEquals(seed.longformSong, item["song"]!!.jsonObject.string("id"))
assertEquals(42, item.int("positionSeconds"))
assertFalse(item["completed"]!!.jsonPrimitive.content.toBoolean())
// Music tracks are not longform.
val music = client.put("/api/v1/longform/progress/${seed.aliceRock}") {
bearer(alice)
contentType(ContentType.Application.Json)
setBody("""{"positionSeconds":10}""")
}
assertEquals(HttpStatusCode.NotFound, music.status)
// Progress is private to the owner.
val bob = client.token("bob", "bob-pw")
val bobList = parseBody(
client.get("/api/v1/longform/progress") { bearer(bob) }.bodyAsText(),
)
assertTrue(bobList["items"]!!.jsonArray.isEmpty())
}
}
}
@Test
fun `a session is scoped to its owner`() {
seededContext().use { seed ->
@@ -0,0 +1,225 @@
package win.taptap.musicmanager.download
import java.nio.file.Files
import java.nio.file.Path
import java.time.Instant
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.insertIgnore
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.jupiter.api.io.TempDir
import win.taptap.musicmanager.config.AppConfig
import win.taptap.musicmanager.config.FolderType
import win.taptap.musicmanager.config.LibraryConfig
import win.taptap.musicmanager.config.MusicFolder
import win.taptap.musicmanager.config.UserAccount
import win.taptap.musicmanager.config.UsersConfig
import win.taptap.musicmanager.db.AlbumArtistsTable
import win.taptap.musicmanager.db.AlbumsTable
import win.taptap.musicmanager.db.ArtistsTable
import win.taptap.musicmanager.db.ConfigRepository
import win.taptap.musicmanager.db.DatabaseFactory
import win.taptap.musicmanager.db.DatabaseHandle
import win.taptap.musicmanager.db.DownloadRepository
import win.taptap.musicmanager.db.MoodRepository
import win.taptap.musicmanager.db.RecommendationUpsert
import win.taptap.musicmanager.db.SongArtistsTable
import win.taptap.musicmanager.db.SongGenresTable
import win.taptap.musicmanager.db.SongsTable
import win.taptap.musicmanager.util.IdGenerator
class DownloadServiceTest {
@TempDir
lateinit var tempDir: Path
@Test
fun `downloaded tracks are promoted to recommendation matches and seeds`() {
withDatabase { handle, library, users ->
val mood = MoodRepository.create(
owner = "alice",
name = "Drei ???",
description = null,
query = null,
genres = emptyList(),
targetSize = 30,
refreshHours = 24,
ytEnabled = true,
seedSongIds = emptyList(),
)
MoodRepository.storeRecommendations(
mood.id,
listOf(
RecommendationUpsert(
videoId = "v-1",
title = "Neue Folge",
artist = "Die drei ???",
album = null,
duration = 300,
thumbnail = null,
matchedSongId = null,
),
),
"2026-01-01T00:00:00Z",
)
DownloadRepository.enqueue("alice", mood.id, "v-1", "Neue Folge", "Die drei ???")
var scanned = false
val service = DownloadService(
users = users,
library = library,
downloader = AudioDownloader { videoId, _, _, dir ->
val file = dir.resolve("$videoId.mp3")
Files.write(file, "fake-mp3".toByteArray())
file
},
tempDir = tempDir.resolve("downloads"),
startScan = {
if (!scanned) {
scanned = true
insertSong(handle, "Die drei ???", "Neue Folge")
}
true
},
isScanning = { false },
)
assertTrue(service.processOne())
val job = assertNotNull(DownloadRepository.forRecommendation("alice", mood.id, "v-1"))
assertEquals("done", job.state)
val songId = assertNotNull(job.songId)
val recommendation = assertNotNull(MoodRepository.recommendation(mood.id, "v-1"))
assertEquals("accepted", recommendation.state)
assertEquals(songId, recommendation.matchedSongId)
assertTrue(MoodRepository.seedSongIds(mood.id).contains(songId))
val imports = library.folders.first().path.resolve("AudioManager Imports")
val files = Files.list(imports).use { stream -> stream.toList() }
assertEquals(1, files.size)
assertEquals("Die drei ___ - Neue Folge.mp3", files.first().fileName.toString())
}
}
@Test
fun `failed downloads retry and then fail`() {
withDatabase { _, library, users ->
val mood = MoodRepository.create(
owner = "alice",
name = "Broken",
description = null,
query = null,
genres = emptyList(),
targetSize = 30,
refreshHours = 24,
ytEnabled = true,
seedSongIds = emptyList(),
)
DownloadRepository.enqueue("alice", mood.id, "v-2", "Missing", "Nobody")
val service = DownloadService(
users = users,
library = library,
downloader = AudioDownloader { _, _, _, _ -> error("network down") },
tempDir = tempDir.resolve("downloads"),
startScan = { true },
isScanning = { false },
)
assertTrue(service.processOne())
assertEquals("queued", DownloadRepository.forRecommendation("alice", mood.id, "v-2")?.state)
assertTrue(service.processOne())
assertEquals("queued", DownloadRepository.forRecommendation("alice", mood.id, "v-2")?.state)
assertTrue(service.processOne())
val job = assertNotNull(DownloadRepository.forRecommendation("alice", mood.id, "v-2"))
assertEquals("failed", job.state)
assertEquals(3, job.attempts)
assertTrue(job.error?.contains("network down") == true)
}
}
private fun withDatabase(block: (DatabaseHandle, LibraryConfig, UsersConfig) -> Unit) {
val musicDir = Files.createDirectories(tempDir.resolve("music"))
val library = LibraryConfig(
listOf(MusicFolder(1, "Music", FolderType.MUSIC, musicDir)),
)
val users = UsersConfig(
listOf(UserAccount("alice", "pw", admin = true, folders = setOf(1))),
)
val handle = DatabaseFactory.connect(
AppConfig(
port = 8080,
dataDir = tempDir.resolve("data"),
libraryConfigPath = tempDir.resolve("library.yaml"),
usersFile = tempDir.resolve("users.yaml"),
ffmpegPath = "ffmpeg",
downloadDir = tempDir.resolve("downloads"),
),
)
try {
ConfigRepository.sync(library, users)
block(handle, library, users)
} finally {
handle.close()
}
}
private fun insertSong(handle: DatabaseHandle, artistName: String, title: String) {
val artistId = IdGenerator.artistId(artistName)
val albumId = IdGenerator.albumId(artistName, "Downloads")
val relativePath = "AudioManager Imports/$artistName - $title.mp3"
val songId = IdGenerator.songId(1, relativePath)
val now = Instant.now().toString()
transaction(handle.database) {
ArtistsTable.insertIgnore {
it[id] = artistId
it[name] = artistName
it[sortName] = artistName
it[searchText] = IdGenerator.normalize(artistName)
}
AlbumsTable.insertIgnore {
it[id] = albumId
it[name] = "Downloads"
it[createdAt] = now
it[searchText] = IdGenerator.normalize("$title $artistName")
}
AlbumArtistsTable.insertIgnore {
it[this.albumId] = albumId
it[this.artistId] = artistId
it[position] = 0
}
SongsTable.insert {
it[id] = songId
it[this.albumId] = albumId
it[folderId] = 1
it[this.title] = title
it[displayArtist] = artistName
it[albumName] = "Downloads"
it[duration] = 300
it[bitRate] = 128
it[size] = 10
it[suffix] = "mp3"
it[contentType] = "audio/mpeg"
it[this.relativePath] = relativePath
it[fileMtime] = 0
it[createdAt] = now
it[updatedAt] = now
it[searchText] = IdGenerator.normalize("$title $artistName Downloads $artistName")
}
SongArtistsTable.insert {
it[this.songId] = songId
it[this.artistId] = artistId
it[position] = 0
}
SongGenresTable.insert {
it[this.songId] = songId
it[genre] = "Hörspiel"
}
}
}
}