From a5af1ee83fbeb4f23a5d8da461e514b6c3ab31e3 Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 15 Nov 2026 13:26:22 +0000 Subject: [PATCH] feat: download accepted recommendations and track longform progress Accepted recommendations without a library match now enter a download queue. A worker downloads the MP3 with yt-dlp into "/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. --- README.md | 24 +- charts/audiomanager/Chart.yaml | 2 +- charts/audiomanager/templates/deployment.yaml | 9 +- charts/audiomanager/values.yaml | 10 +- .../win/taptap/musicmanager/Application.kt | 32 +++ .../win/taptap/musicmanager/api/ApiModels.kt | 28 +++ .../win/taptap/musicmanager/api/ApiRoutes.kt | 4 +- .../taptap/musicmanager/api/LongformApi.kt | 70 ++++++ .../win/taptap/musicmanager/api/MoodApi.kt | 21 ++ .../taptap/musicmanager/config/AppConfig.kt | 10 +- .../musicmanager/db/DownloadRepository.kt | 231 ++++++++++++++++++ .../taptap/musicmanager/db/MoodRepository.kt | 22 ++ .../win/taptap/musicmanager/db/Tables.kt | 28 +++ .../musicmanager/download/DownloadService.kt | 136 +++++++++++ .../musicmanager/download/YtDlpDownloader.kt | 67 +++++ .../migration/V6__downloads_and_longform.sql | 33 +++ .../taptap/musicmanager/api/ApiRoutesTest.kt | 53 +++- .../download/DownloadServiceTest.kt | 225 +++++++++++++++++ 18 files changed, 996 insertions(+), 9 deletions(-) create mode 100644 src/main/kotlin/win/taptap/musicmanager/api/LongformApi.kt create mode 100644 src/main/kotlin/win/taptap/musicmanager/db/DownloadRepository.kt create mode 100644 src/main/kotlin/win/taptap/musicmanager/download/DownloadService.kt create mode 100644 src/main/kotlin/win/taptap/musicmanager/download/YtDlpDownloader.kt create mode 100644 src/main/resources/db/migration/V6__downloads_and_longform.sql create mode 100644 src/test/kotlin/win/taptap/musicmanager/download/DownloadServiceTest.kt diff --git a/README.md b/README.md index 835718c..6afd896 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,10 @@ Passwords are read into memory at startup and never written to SQLite. In produc | `LIBRARY_CONFIG` | `./config/library.yaml` | Library folders | | `USERS_FILE` | `./config/users.yaml` | Users and passwords | | `FFMPEG_PATH` | `ffmpeg` | Transcoder binary | -| `YTDLP_PATH` | `yt-dlp` | `yt-dlp` binary used for YT Music discovery | +| `YTDLP_PATH` | `yt-dlp` | `yt-dlp` binary used for discovery and downloads | +| `DOWNLOADS_ENABLED` | `true` | Download accepted recommendations that are not in the library | +| `DOWNLOAD_DIR` | `$DATA_DIR/downloads` | Temporary files for the download worker | +| `DOWNLOAD_FOLDER_ID` | first visible music folder | Folder that receives downloaded MP3s | ## The mood model @@ -181,6 +184,8 @@ Song objects contain deterministic IDs (`sg-…`), album/artist names, duration, - `POST /moods/{id}/recommendations/{videoId}/accept` — accept; the library match becomes a seed immediately. - `POST /moods/{id}/recommendations/{videoId}/reject` — reject; the candidate is not offered again. +Accepted recommendations without a library match are queued for download (`download.state` = `queued|downloading|done|failed` in the recommendation). The worker writes MP3s into `/AudioManager Imports`, runs a scan, matches the new song, adds it as a seed and updates the recommendation. + ### Radio sessions - `POST /moods/{id}/sessions` — `{ limit?: 15 }` → session plus the first queue. @@ -190,6 +195,14 @@ Song objects contain deterministic IDs (`sg-…`), album/artist names, duration, - `POST /sessions/{id}/add-to-mood` — `{ songId, moodId? }`. Without `moodId`, the session's mood is used. This is the replacement for likes. - `DELETE /sessions/{id}` — close. +### Long-form + +- `GET /library/songs?scope=longform` — the long-form library (shared with every user). +- `GET /longform/progress` — resume points, newest first, with song data. +- `PUT /longform/progress/{songId}` — `{ positionSeconds, durationSeconds?, completed? }`. + +Long-form folders are never valid mood seeds and never enter radio. + ### System - `GET /scan` — `{ scanning, count, error }` @@ -208,12 +221,16 @@ src/main/kotlin/win/taptap/musicmanager/ |-- discovery/ | |-- YtSearch.kt # yt-dlp search and result parsing | `-- DiscoveryService.kt # Queries, matching, scoring, pruning +|-- download/ +| |-- DownloadService.kt # Queue worker, import folder, scan, promotion +| `-- YtDlpDownloader.kt # MP3 download with embedded metadata |-- api/ | |-- ApiRoutes.kt # /api/v1 wiring, auth and system endpoints | |-- ApiModels.kt # Native DTOs | |-- ApiSupport.kt # Token resolution, errors, folder scoping | |-- LibraryApi.kt # Sync, search, stream, covers | |-- MoodApi.kt # Mood CRUD, seeds, preview, recommendations +| |-- LongformApi.kt # Long-form resume points | `-- SessionApi.kt # Radio sessions, events, add-to-mood |-- radio/ | `-- RadioService.kt # Candidate pool, scoring, diversity, batches @@ -230,7 +247,7 @@ src/main/kotlin/win/taptap/musicmanager/ `-- util/ # Deterministic IDs, sort names ``` -YT discovery runs in the background for stale moods (`refreshHours`) and on demand from the mood screen. Queries come from the mood text, seed artists and genres; results are deduplicated, filtered to music-length videos, matched to library songs by normalized title and artist, and scored. Undecided candidates not seen in a refresh are pruned; accepted and rejected decisions are permanent. Accepted tracks that are not in the library wait for the future downloader, which will drop them into an import folder and rescan. +YT discovery runs in the background for stale moods (`refreshHours`) and on demand from the mood screen. Queries come from the mood text, seed artists and genres; results are deduplicated, filtered to music-length videos, matched to library songs by normalized title and artist, and scored. Undecided candidates not seen in a refresh are pruned; accepted and rejected decisions are permanent. Accepted tracks that are not in the library are downloaded by the worker into `/AudioManager Imports`, scanned, matched and added as seeds automatically. The old OpenSubsonic layer (`/rest`, `subsonic/`, playlist materialization) was removed in 0.2.0. The server and the KMP client are the two halves of the same product; there is no compatibility layer to preserve and no server-rendered UI. @@ -294,6 +311,7 @@ deploy into the wrong place. Set `allowDefaultNamespace=true` to opt in to The chart expects: - a `users.yaml` Secret (Vault-managed) referenced by `users.existingSecret`; +- a writable music hostPath when automatic downloads are enabled; - a `library.yaml` ConfigMap; - read-only hostPaths for music and long-form; - a registry pull secret for the private image. @@ -335,5 +353,5 @@ The smoke test generates tagged MP3s, starts the server, and verifies login, syn - [x] Kotlin Multiplatform client foundation (Android, desktop, web). - [x] Offline downloads and offline playback across clients. - [x] YT Music discovery through `yt-dlp`, recommendation list, accept/reject. -- [ ] Optional downloader that writes accepted tracks into an import folder and rescans. +- [x] Downloader that writes accepted tracks into an import folder and rescans. - [ ] Home-screen widgets, Android Auto, and richer radio tuning. diff --git a/charts/audiomanager/Chart.yaml b/charts/audiomanager/Chart.yaml index b85d9b1..23b2b8d 100644 --- a/charts/audiomanager/Chart.yaml +++ b/charts/audiomanager/Chart.yaml @@ -2,5 +2,5 @@ apiVersion: v2 name: audiomanager description: Mood-first self-hosted music server type: application -version: 0.2.2 +version: 0.2.3 appVersion: "0.1.0" diff --git a/charts/audiomanager/templates/deployment.yaml b/charts/audiomanager/templates/deployment.yaml index 82d8597..4f9e8c7 100644 --- a/charts/audiomanager/templates/deployment.yaml +++ b/charts/audiomanager/templates/deployment.yaml @@ -44,6 +44,14 @@ spec: value: ffmpeg - name: YTDLP_PATH value: yt-dlp + - name: DOWNLOADS_ENABLED + value: {{ .Values.downloads.enabled | quote }} + - name: DOWNLOAD_DIR + value: /data/downloads + {{- with .Values.downloads.folderId }} + - name: DOWNLOAD_FOLDER_ID + value: {{ . | quote }} + {{- end }} {{- with .Values.env }} {{- toYaml . | nindent 12 }} {{- end }} @@ -79,7 +87,6 @@ spec: mountPath: /tmp - name: music mountPath: {{ .Values.media.music.mountPath }} - readOnly: true - name: longform mountPath: {{ .Values.media.longform.mountPath }} readOnly: true diff --git a/charts/audiomanager/values.yaml b/charts/audiomanager/values.yaml index 25df217..f88b7a2 100644 --- a/charts/audiomanager/values.yaml +++ b/charts/audiomanager/values.yaml @@ -41,7 +41,15 @@ data: accessModes: - ReadWriteOnce -# hostPath mounts shared by every schedulable node. Read-only in the pod. +# Automatic downloads for accepted recommendations. Files land in +# "/AudioManager Imports" and are picked up by a scan, so the music +# hostPath must be writable by the pod. +downloads: + enabled: true + # Empty uses the first music folder visible to the user. + folderId: null + +# hostPath mounts shared by every schedulable node. media: music: hostPath: /mnt/media/music diff --git a/src/main/kotlin/win/taptap/musicmanager/Application.kt b/src/main/kotlin/win/taptap/musicmanager/Application.kt index 1658a5d..4c29a1c 100644 --- a/src/main/kotlin/win/taptap/musicmanager/Application.kt +++ b/src/main/kotlin/win/taptap/musicmanager/Application.kt @@ -19,6 +19,7 @@ import io.ktor.server.response.respond import io.ktor.server.response.respondText import io.ktor.server.routing.get import io.ktor.server.routing.routing +import java.nio.file.Files import java.time.Instant import java.util.concurrent.TimeUnit import kotlinx.coroutines.CoroutineScope @@ -45,6 +46,8 @@ import win.taptap.musicmanager.db.MediaRepository import win.taptap.musicmanager.db.MoodRepository import win.taptap.musicmanager.discovery.DiscoveryService import win.taptap.musicmanager.discovery.YtDlpSearch +import win.taptap.musicmanager.download.DownloadService +import win.taptap.musicmanager.download.YtDlpDownloader import win.taptap.musicmanager.radio.RadioService import win.taptap.musicmanager.scanner.CoverArtService import win.taptap.musicmanager.scanner.MediaScanner @@ -71,11 +74,39 @@ fun main() { library = library, coverArtService = coverArtService, ) + val downloadService = if (config.downloadsEnabled) { + Files.createDirectories(config.downloadDir) + DownloadService( + users = users, + library = library, + downloader = YtDlpDownloader(config.ytDlpPath), + tempDir = config.downloadDir, + startScan = { scanner.start(applicationScope) }, + isScanning = { scanner.status().scanning }, + folderId = config.downloadFolderId, + ) + } else { + null + } if (MediaRepository.countSongs() == 0L) { log.info("Song table is empty, starting initial library scan") scanner.start(applicationScope) } + // Accepted recommendations that are not in the library are fetched in + // the background and promoted after a scan. + downloadService?.let { service -> + applicationScope.launch(Dispatchers.IO) { + delay(TimeUnit.MINUTES.toMillis(1)) + while (isActive) { + val worked = runCatching { service.processOne() } + .onFailure { log.warn("Download worker error", it) } + .getOrDefault(false) + delay(if (worked) 1_000 else 20_000) + } + } + } + // Moods refresh their YT recommendations in the background; the client // can also force a pass from the mood screen. applicationScope.launch(Dispatchers.IO) { @@ -157,6 +188,7 @@ fun main() { transcoder = transcoder, radioService = radioService, discoveryService = discoveryService, + downloadsEnabled = config.downloadsEnabled, startScan = { scanner.start(applicationScope) }, scanStatus = { scanner.status() }, ) diff --git a/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt b/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt index 21e8a04..a23fda4 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt @@ -228,6 +228,14 @@ data class ScanStatusDto( val error: String? = null, ) +@Serializable +data class DownloadDto( + val state: String, + val attempts: Int, + val error: String? = null, + val updatedAt: String, +) + @Serializable data class RecommendationDto( val videoId: String, @@ -241,6 +249,26 @@ data class RecommendationDto( val timesAdded: Int, val firstSeen: String, val lastSeen: String, + val download: DownloadDto? = null, +) + +@Serializable +data class LongformProgressDto( + val song: SongDto, + val positionSeconds: Int, + val durationSeconds: Int, + val completed: Boolean, + val updatedAt: String, +) + +@Serializable +data class LongformProgressResponse(val items: List = emptyList()) + +@Serializable +data class SaveLongformProgressRequest( + val positionSeconds: Int, + val durationSeconds: Int? = null, + val completed: Boolean = false, ) fun seedDto(seed: MoodSeedRecord, song: SongRecord): SeedDto = diff --git a/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt b/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt index 5ef23a2..29363ad 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt @@ -35,6 +35,7 @@ fun Route.apiRoutes( transcoder: FfmpegTranscoder, radioService: RadioService, discoveryService: DiscoveryService, + downloadsEnabled: Boolean = true, startScan: () -> Boolean, scanStatus: () -> ScanStatus, ) { @@ -127,7 +128,8 @@ fun Route.apiRoutes( } libraryRoutes(users, library, coverArtService, transcoder) - moodRoutes(users, library, radioService, discoveryService) + longformRoutes(users, library) + moodRoutes(users, library, radioService, discoveryService, downloadsEnabled) sessionRoutes(users, library, radioService) } } diff --git a/src/main/kotlin/win/taptap/musicmanager/api/LongformApi.kt b/src/main/kotlin/win/taptap/musicmanager/api/LongformApi.kt new file mode 100644 index 0000000..6f483e4 --- /dev/null +++ b/src/main/kotlin/win/taptap/musicmanager/api/LongformApi.kt @@ -0,0 +1,70 @@ +package win.taptap.musicmanager.api + +import io.ktor.http.HttpStatusCode +import io.ktor.server.response.respond +import io.ktor.server.routing.Route +import io.ktor.server.routing.get +import io.ktor.server.routing.put +import win.taptap.musicmanager.api.LongformProgressDto +import win.taptap.musicmanager.api.LongformProgressResponse +import win.taptap.musicmanager.api.SaveLongformProgressRequest +import win.taptap.musicmanager.api.toDto +import win.taptap.musicmanager.config.LibraryConfig +import win.taptap.musicmanager.config.UsersConfig +import win.taptap.musicmanager.db.LibraryRepository +import win.taptap.musicmanager.db.LongformRepository + +/** + * Resume points for audiobooks and podcasts. The library itself is synced + * through `/library/songs?scope=longform`; this only tracks where the user was. + */ +fun Route.longformRoutes( + users: UsersConfig, + library: LibraryConfig, +) { + get("/longform/progress") { + val user = call.requireApiUser(users) + val folders = user.longformFolderIds(library) + val records = LongformRepository.forUser(user.name) + val songs = LibraryRepository + .songsByIds(records.map { it.songId }, folders, user.name) + .associateBy { it.id } + + call.respond( + LongformProgressResponse( + items = records.mapNotNull { record -> + songs[record.songId]?.let { song -> + LongformProgressDto( + song = song.toDto(), + positionSeconds = record.positionSeconds, + durationSeconds = record.durationSeconds, + completed = record.completed, + updatedAt = record.updatedAt, + ) + } + }, + ), + ) + } + + put("/longform/progress/{songId}") { + val user = call.requireApiUser(users) + val songId = call.parameters["songId"] ?: badRequest("Song id is required") + val song = LibraryRepository.song(songId, user.longformFolderIds(library), user.name) + ?: notFound("Longform track") + val request = call.receiveJson() + + val duration = (request.durationSeconds ?: song.duration).coerceAtLeast(0) + val position = request.positionSeconds.coerceIn(0, maxOf(duration, song.duration)) + val completed = request.completed || (duration > 0 && position >= duration - 5) + + LongformRepository.save( + userName = user.name, + songId = song.id, + positionSeconds = position, + durationSeconds = duration, + completed = completed, + ) + call.respond(HttpStatusCode.NoContent) + } +} diff --git a/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt b/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt index 4f141cc..8fee9ce 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt @@ -15,6 +15,7 @@ import win.taptap.musicmanager.api.AddToMoodResponse import win.taptap.musicmanager.api.CreateMoodRequest import win.taptap.musicmanager.api.MoodDetailDto import win.taptap.musicmanager.api.MoodDto +import win.taptap.musicmanager.api.DownloadDto import win.taptap.musicmanager.api.RecommendationDecisionDto import win.taptap.musicmanager.api.RecommendationDto import win.taptap.musicmanager.api.SearchResponse @@ -24,6 +25,7 @@ import win.taptap.musicmanager.api.toDto import win.taptap.musicmanager.config.LibraryConfig import win.taptap.musicmanager.config.UserAccount import win.taptap.musicmanager.config.UsersConfig +import win.taptap.musicmanager.db.DownloadRepository import win.taptap.musicmanager.db.LibraryRepository import win.taptap.musicmanager.db.MoodRecord import win.taptap.musicmanager.db.MoodRepository @@ -38,6 +40,7 @@ fun Route.moodRoutes( library: LibraryConfig, radioService: RadioService, discoveryService: DiscoveryService, + downloadsEnabled: Boolean, ) { get("/moods") { val user = call.requireApiUser(users) @@ -184,6 +187,15 @@ fun Route.moodRoutes( val alreadyAccepted = recommendation.state == "accepted" MoodRepository.setRecommendationState(mood.id, videoId, "accepted") if (!alreadyAccepted) MoodRepository.incrementRecommendationAdds(mood.id, videoId) + if (matched == null && downloadsEnabled) { + DownloadRepository.enqueue( + userName = user.name, + moodId = mood.id, + videoId = recommendation.videoId, + title = recommendation.title, + artist = recommendation.artist, + ) + } call.respond(decision(mood.id, videoId, user, library, seedAdded)) } @@ -234,6 +246,7 @@ private fun recommendations( val matchedSongs = LibraryRepository .songsByIds(matchedIds, user.visibleFolderIds(library), user.name) .associateBy { it.id } + val downloads = DownloadRepository.forMood(user.name, mood.id) return RecommendationsResponse( recommendations = records.map { record -> @@ -249,6 +262,14 @@ private fun recommendations( timesAdded = record.timesAdded, firstSeen = record.firstSeen, lastSeen = record.lastSeen, + download = downloads[record.videoId]?.let { job -> + DownloadDto( + state = job.state, + attempts = job.attempts, + error = job.error, + updatedAt = job.updatedAt, + ) + }, ) }, ) diff --git a/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt b/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt index 386a863..9abd823 100644 --- a/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt +++ b/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt @@ -15,6 +15,9 @@ data class AppConfig( val usersFile: Path, val ffmpegPath: String, val ytDlpPath: String = "yt-dlp", + val downloadsEnabled: Boolean = true, + val downloadDir: Path = dataDir.resolve("downloads"), + val downloadFolderId: Int? = null, ) { val databasePath: Path get() = dataDir.resolve("musicmanager.db") @@ -28,13 +31,18 @@ data class AppConfig( ?: throw IllegalArgumentException("PORT must be an integer, got: $rawPort") require(port in 1..65535) { "PORT must be between 1 and 65535, got: $port" } + val dataDir = Path.of(value("DATA_DIR", "./data")) + return AppConfig( port = port, - dataDir = Path.of(value("DATA_DIR", "./data")), + dataDir = dataDir, libraryConfigPath = Path.of(value("LIBRARY_CONFIG", "./config/library.yaml")), usersFile = Path.of(value("USERS_FILE", "./config/users.yaml")), ffmpegPath = value("FFMPEG_PATH", "ffmpeg"), ytDlpPath = value("YTDLP_PATH", "yt-dlp"), + downloadsEnabled = value("DOWNLOADS_ENABLED", "true").toBooleanStrictOrNull() ?: true, + downloadDir = Path.of(value("DOWNLOAD_DIR", dataDir.resolve("downloads").toString())), + downloadFolderId = env["DOWNLOAD_FOLDER_ID"]?.takeIf { it.isNotBlank() }?.toIntOrNull(), ) } } diff --git a/src/main/kotlin/win/taptap/musicmanager/db/DownloadRepository.kt b/src/main/kotlin/win/taptap/musicmanager/db/DownloadRepository.kt new file mode 100644 index 0000000..65b4c91 --- /dev/null +++ b/src/main/kotlin/win/taptap/musicmanager/db/DownloadRepository.kt @@ -0,0 +1,231 @@ +package win.taptap.musicmanager.db + +import java.time.Instant +import org.jetbrains.exposed.sql.SortOrder +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.and +import org.jetbrains.exposed.sql.insertIgnore +import org.jetbrains.exposed.sql.selectAll +import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.update + +data class DownloadJobRecord( + val id: String, + val userName: String, + val moodId: String, + val videoId: String, + val title: String, + val artist: String, + val state: String, + val attempts: Int, + val error: String?, + val songId: String?, + val createdAt: String, + val updatedAt: String, +) + +/** + * Jobs created when a user accepts a recommendation that the library does not + * have yet. A background worker downloads, scans and promotes them. + */ +object DownloadRepository { + + fun enqueue( + userName: String, + moodId: String, + videoId: String, + title: String, + artist: String, + ): Boolean = transaction { + val now = Instant.now().toString() + val existing = DownloadJobsTable.selectAll() + .where { + (DownloadJobsTable.userName eq userName) and + (DownloadJobsTable.moodId eq moodId) and + (DownloadJobsTable.videoId eq videoId) + } + .singleOrNull() + + if (existing == null) { + val id = "dl-" + ApiTokenRepository.hash("$userName|$moodId|$videoId").take(24) + DownloadJobsTable.insertIgnore { + it[DownloadJobsTable.id] = id + it[DownloadJobsTable.userName] = userName + it[DownloadJobsTable.moodId] = moodId + it[DownloadJobsTable.videoId] = videoId + it[DownloadJobsTable.title] = title.take(500) + it[DownloadJobsTable.artist] = artist.take(300) + it[state] = "queued" + it[attempts] = 0 + it[createdAt] = now + it[updatedAt] = now + } + } else if (existing[DownloadJobsTable.state] == "failed") { + DownloadJobsTable.update({ DownloadJobsTable.id eq existing[DownloadJobsTable.id] }) { + it[state] = "queued" + it[attempts] = 0 + it[error] = null + it[updatedAt] = now + } + } + true + } + + /** Takes the oldest queued job and marks it as downloading. */ + fun claimNext(): DownloadJobRecord? = transaction { + val row = DownloadJobsTable.selectAll() + .where { DownloadJobsTable.state eq "queued" } + .orderBy(DownloadJobsTable.createdAt to SortOrder.ASC) + .limit(1) + .singleOrNull() + ?: return@transaction null + + val attempts = row[DownloadJobsTable.attempts] + 1 + DownloadJobsTable.update({ DownloadJobsTable.id eq row[DownloadJobsTable.id] }) { + it[state] = "downloading" + it[DownloadJobsTable.attempts] = attempts + it[updatedAt] = Instant.now().toString() + } + row.toRecord().copy(state = "downloading", attempts = attempts) + } + + fun markDone(id: String, songId: String) { + transaction { + DownloadJobsTable.update({ DownloadJobsTable.id eq id }) { + it[state] = "done" + it[this.songId] = songId + it[error] = null + it[updatedAt] = Instant.now().toString() + } + } + } + + fun markFailed(id: String, error: String) { + transaction { + DownloadJobsTable.update({ DownloadJobsTable.id eq id }) { + it[state] = "failed" + it[this.error] = error.take(1000) + it[updatedAt] = Instant.now().toString() + } + } + } + + fun retryLater(id: String, error: String) { + transaction { + DownloadJobsTable.update({ DownloadJobsTable.id eq id }) { + it[state] = "queued" + it[this.error] = error.take(1000) + it[updatedAt] = Instant.now().toString() + } + } + } + + fun forUser(userName: String): List = transaction { + DownloadJobsTable.selectAll() + .where { DownloadJobsTable.userName eq userName } + .orderBy(DownloadJobsTable.createdAt to SortOrder.DESC) + .map { it.toRecord() } + } + + fun forMood(userName: String, moodId: String): Map = transaction { + DownloadJobsTable.selectAll() + .where { + (DownloadJobsTable.userName eq userName) and + (DownloadJobsTable.moodId eq moodId) + } + .associate { it[DownloadJobsTable.videoId] to it.toRecord() } + } + + fun forRecommendation(userName: String, moodId: String, videoId: String): DownloadJobRecord? = + transaction { + DownloadJobsTable.selectAll() + .where { + (DownloadJobsTable.userName eq userName) and + (DownloadJobsTable.moodId eq moodId) and + (DownloadJobsTable.videoId eq videoId) + } + .singleOrNull() + ?.toRecord() + } + + private fun org.jetbrains.exposed.sql.ResultRow.toRecord() = DownloadJobRecord( + id = this[DownloadJobsTable.id], + userName = this[DownloadJobsTable.userName], + moodId = this[DownloadJobsTable.moodId], + videoId = this[DownloadJobsTable.videoId], + title = this[DownloadJobsTable.title], + artist = this[DownloadJobsTable.artist], + state = this[DownloadJobsTable.state], + attempts = this[DownloadJobsTable.attempts], + error = this[DownloadJobsTable.error], + songId = this[DownloadJobsTable.songId], + createdAt = this[DownloadJobsTable.createdAt], + updatedAt = this[DownloadJobsTable.updatedAt], + ) +} + +data class LongformProgressRecord( + val songId: String, + val positionSeconds: Int, + val durationSeconds: Int, + val completed: Boolean, + val updatedAt: String, +) + +/** Resume points for audiobooks and podcasts. Music never uses this table. */ +object LongformRepository { + + fun save( + userName: String, + songId: String, + positionSeconds: Int, + durationSeconds: Int, + completed: Boolean, + updatedAt: String = Instant.now().toString(), + ) { + transaction { + val exists = LongformProgressTable.selectAll() + .where { + (LongformProgressTable.userName eq userName) and + (LongformProgressTable.songId eq songId) + } + .any() + if (exists) { + LongformProgressTable.update({ + (LongformProgressTable.userName eq userName) and + (LongformProgressTable.songId eq songId) + }) { + it[this.positionSeconds] = positionSeconds.coerceAtLeast(0) + it[this.durationSeconds] = durationSeconds.coerceAtLeast(0) + it[this.completed] = completed + it[this.updatedAt] = updatedAt + } + } else { + LongformProgressTable.insertIgnore { + it[this.userName] = userName + it[this.songId] = songId + it[this.positionSeconds] = positionSeconds.coerceAtLeast(0) + it[this.durationSeconds] = durationSeconds.coerceAtLeast(0) + it[this.completed] = completed + it[this.updatedAt] = updatedAt + } + } + } + } + + fun forUser(userName: String, limit: Int = 100): List = transaction { + LongformProgressTable.selectAll() + .where { LongformProgressTable.userName eq userName } + .orderBy(LongformProgressTable.updatedAt to SortOrder.DESC) + .limit(limit.coerceIn(1, 500)) + .map { + LongformProgressRecord( + songId = it[LongformProgressTable.songId], + positionSeconds = it[LongformProgressTable.positionSeconds], + durationSeconds = it[LongformProgressTable.durationSeconds], + completed = it[LongformProgressTable.completed], + updatedAt = it[LongformProgressTable.updatedAt], + ) + } + } +} diff --git a/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt b/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt index e1dedfb..2f8e700 100644 --- a/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt +++ b/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt @@ -345,6 +345,28 @@ object MoodRepository { } } + /** Links a downloaded recommendation to the scanned song and accepts it. */ + fun attachDownloadedSong(moodId: String, videoId: String, songId: String) { + transaction { + val row = MoodRecommendationsTable.selectAll() + .where { + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId eq videoId) + } + .singleOrNull() + ?: return@transaction + MoodRecommendationsTable.update({ + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId eq videoId) + }) { + it[matchedSongId] = songId + it[state] = "accepted" + it[timesAdded] = row[MoodRecommendationsTable.timesAdded] + 1 + it[lastSeen] = Instant.now().toString() + } + } + } + fun markDiscovered(id: String) { transaction { MoodsTable.update({ MoodsTable.id eq id }) { diff --git a/src/main/kotlin/win/taptap/musicmanager/db/Tables.kt b/src/main/kotlin/win/taptap/musicmanager/db/Tables.kt index 071152b..8ddcd50 100644 --- a/src/main/kotlin/win/taptap/musicmanager/db/Tables.kt +++ b/src/main/kotlin/win/taptap/musicmanager/db/Tables.kt @@ -234,6 +234,34 @@ object SongAffinityTable : Table("song_affinity") { override val primaryKey = PrimaryKey(userName, songId) } +object DownloadJobsTable : Table("download_jobs") { + val id = varchar("id", 32) + val userName = reference("user_name", UsersTable.name, onDelete = ReferenceOption.CASCADE) + val moodId = reference("mood_id", MoodsTable.id, onDelete = ReferenceOption.CASCADE) + val videoId = varchar("video_id", 64) + val title = text("title") + val artist = text("artist") + val state = varchar("state", 16).default("queued") + val attempts = integer("attempts").default(0) + val error = text("error").nullable() + val songId = reference("song_id", SongsTable.id, onDelete = ReferenceOption.SET_NULL).nullable() + val createdAt = varchar("created_at", 40) + val updatedAt = varchar("updated_at", 40) + + override val primaryKey = PrimaryKey(id) +} + +object LongformProgressTable : Table("longform_progress") { + val userName = reference("user_name", UsersTable.name, onDelete = ReferenceOption.CASCADE) + val songId = reference("song_id", SongsTable.id, onDelete = ReferenceOption.CASCADE) + val positionSeconds = integer("position_seconds").default(0) + val durationSeconds = integer("duration_seconds").default(0) + val completed = bool("completed").default(false) + val updatedAt = varchar("updated_at", 40) + + override val primaryKey = PrimaryKey(userName, songId) +} + object LibraryTombstonesTable : Table("library_tombstones") { val entity = varchar("entity", 20) val entityId = varchar("entity_id", 32) diff --git a/src/main/kotlin/win/taptap/musicmanager/download/DownloadService.kt b/src/main/kotlin/win/taptap/musicmanager/download/DownloadService.kt new file mode 100644 index 0000000..12c5bff --- /dev/null +++ b/src/main/kotlin/win/taptap/musicmanager/download/DownloadService.kt @@ -0,0 +1,136 @@ +package win.taptap.musicmanager.download + +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardCopyOption +import org.slf4j.LoggerFactory +import win.taptap.musicmanager.config.FolderType +import win.taptap.musicmanager.config.LibraryConfig +import win.taptap.musicmanager.config.UsersConfig +import win.taptap.musicmanager.db.DownloadJobRecord +import win.taptap.musicmanager.db.DownloadRepository +import win.taptap.musicmanager.db.LibraryRepository +import win.taptap.musicmanager.db.MoodRepository +import win.taptap.musicmanager.discovery.artistMatches +import win.taptap.musicmanager.discovery.matchKey + +fun interface AudioDownloader { + /** Downloads [videoId] and returns the resulting MP3 file. */ + fun download(videoId: String, title: String, artist: String, targetDir: Path): Path +} + +/** + * Downloads accepted recommendations that are not in the library: fetch the + * MP3, put it into the music folder, run a scan and promote the new song. + */ +class DownloadService( + private val users: UsersConfig, + private val library: LibraryConfig, + private val downloader: AudioDownloader, + private val tempDir: Path, + private val startScan: () -> Boolean, + private val isScanning: () -> Boolean, + private val folderId: Int? = null, + private val maxAttempts: Int = 3, + private val sleepMillis: (Long) -> Unit = { Thread.sleep(it) }, +) { + private val log = LoggerFactory.getLogger(DownloadService::class.java) + + /** Processes at most one job. Returns false when the queue is empty. */ + fun processOne(): Boolean { + val job = DownloadRepository.claimNext() ?: return false + try { + process(job) + } catch (t: Throwable) { + val message = t.message ?: t::class.simpleName ?: "Download failed" + log.warn("Download {} failed (attempt {}): {}", job.videoId, job.attempts, message) + if (job.attempts >= maxAttempts) { + DownloadRepository.markFailed(job.id, message) + } else { + DownloadRepository.retryLater(job.id, message) + } + } + return true + } + + private fun process(job: DownloadJobRecord) { + val user = users.byName(job.userName) + ?: throw IllegalStateException("Unknown user ${job.userName}") + val folders = library.folders + .asSequence() + .filter { it.type == FolderType.MUSIC && it.id in user.folders } + .associateBy { it.id } + val folder = folderId?.let { folders[it] } + ?: folders.values.firstOrNull() + ?: throw IllegalStateException("No writable music folder for ${job.userName}") + + val importDir = folder.path.resolve(IMPORT_DIR) + Files.createDirectories(importDir) + val working = tempDir.resolve(job.id) + Files.createDirectories(working) + + try { + val downloaded = downloader.download(job.videoId, job.title, job.artist, working) + val extension = downloaded.fileName.toString().substringAfterLast('.', "mp3") + val target = uniqueTarget(importDir, job.artist, job.title, extension) + Files.move(downloaded, target, StandardCopyOption.REPLACE_EXISTING) + log.info("Downloaded {} to {}", job.videoId, target) + + awaitScan() + + val songId = matchSong(job, folders.keys, user.name) + ?: throw IllegalStateException("Downloaded file was not indexed by the scanner") + DownloadRepository.markDone(job.id, songId) + MoodRepository.attachDownloadedSong(job.moodId, job.videoId, songId) + MoodRepository.addSeed(job.moodId, job.userName, songId, "yt") + } finally { + runCatching { working.toFile().deleteRecursively() } + } + } + + private fun awaitScan() { + // If another scan is already running, wait for that one instead. + startScan() + val deadline = System.nanoTime() + SCAN_TIMEOUT_MINUTES * 60_000_000_000L + while (isScanning()) { + if (System.nanoTime() > deadline) { + throw IllegalStateException("Library scan did not finish in time") + } + sleepMillis(250) + } + } + + private fun matchSong(job: DownloadJobRecord, folders: Set, userName: String): String? { + val titleKey = matchKey(job.title) + if (titleKey.isEmpty()) return null + return LibraryRepository.searchSongs(job.title, 25, 0, folders, userName) + .firstOrNull { + artistMatches(job.artist, it.displayArtist) && matchKey(it.title) == titleKey + } + ?.id + } + + private fun uniqueTarget(dir: Path, artist: String, title: String, extension: String): Path { + val base = "${sanitize(artist)} - ${sanitize(title)}" + val suffix = extension.ifBlank { "mp3" }.take(10) + var candidate = dir.resolve("$base.$suffix") + var counter = 2 + while (Files.exists(candidate)) { + candidate = dir.resolve("$base ($counter).$suffix") + counter += 1 + } + return candidate + } + + private fun sanitize(value: String): String = + value.replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_") + .replace(Regex("\\s+"), " ") + .trim() + .take(120) + .ifEmpty { "track" } + + private companion object { + const val IMPORT_DIR = "AudioManager Imports" + const val SCAN_TIMEOUT_MINUTES = 10L + } +} diff --git a/src/main/kotlin/win/taptap/musicmanager/download/YtDlpDownloader.kt b/src/main/kotlin/win/taptap/musicmanager/download/YtDlpDownloader.kt new file mode 100644 index 0000000..dcb501b --- /dev/null +++ b/src/main/kotlin/win/taptap/musicmanager/download/YtDlpDownloader.kt @@ -0,0 +1,67 @@ +package win.taptap.musicmanager.download + +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.TimeUnit +import kotlin.concurrent.thread + +/** + * Downloads a single YouTube video as an MP3 with embedded metadata, so the + * scanner can derive artist and title from the file itself. + */ +class YtDlpDownloader( + private val executable: String, + private val timeoutSeconds: Long = 600, +) : AudioDownloader { + + override fun download(videoId: String, title: String, artist: String, targetDir: Path): Path { + Files.createDirectories(targetDir) + val process = ProcessBuilder( + executable, + "-x", + "--audio-format", + "mp3", + "--audio-quality", + "0", + "--embed-metadata", + "--no-playlist", + "--no-cache-dir", + "--no-progress", + "--print", + "after_move:filepath", + "-o", + targetDir.resolve("%(id)s.%(ext)s").toString(), + "https://www.youtube.com/watch?v=$videoId", + ) + .redirectErrorStream(true) + .start() + + val output = StringBuilder() + val reader = thread(isDaemon = true, name = "yt-dlp-download") { + process.inputStream.bufferedReader().useLines { lines -> + lines.forEach { output.appendLine(it) } + } + } + + if (!process.waitFor(timeoutSeconds, TimeUnit.SECONDS)) { + process.destroyForcibly() + reader.join(2_000) + throw IllegalStateException("yt-dlp download timed out after ${timeoutSeconds}s") + } + reader.join(2_000) + if (process.exitValue() != 0) { + throw IllegalStateException( + "yt-dlp exited with ${process.exitValue()}: ${output.toString().take(400).trim()}", + ) + } + + val printed = output.lines() + .map { it.trim() } + .lastOrNull { it.endsWith(".mp3", ignoreCase = true) } + val file = printed?.let { Path.of(it) } ?: targetDir.resolve("$videoId.mp3") + if (!Files.isRegularFile(file)) { + throw IllegalStateException("yt-dlp did not produce an MP3 file") + } + return file + } +} diff --git a/src/main/resources/db/migration/V6__downloads_and_longform.sql b/src/main/resources/db/migration/V6__downloads_and_longform.sql new file mode 100644 index 0000000..9daaee9 --- /dev/null +++ b/src/main/resources/db/migration/V6__downloads_and_longform.sql @@ -0,0 +1,33 @@ +-- Accepted YT recommendations that are not in the library are downloaded into +-- the first visible music folder and indexed by a scan. +CREATE TABLE download_jobs ( + id TEXT PRIMARY KEY, + user_name TEXT NOT NULL REFERENCES users(name) ON DELETE CASCADE, + mood_id TEXT NOT NULL REFERENCES moods(id) ON DELETE CASCADE, + video_id TEXT NOT NULL, + title TEXT NOT NULL, + artist TEXT NOT NULL, + state TEXT NOT NULL DEFAULT 'queued' + CHECK (state IN ('queued', 'downloading', 'done', 'failed')), + attempts INTEGER NOT NULL DEFAULT 0, + error TEXT, + song_id TEXT REFERENCES songs(id) ON DELETE SET NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + UNIQUE (user_name, mood_id, video_id) +); + +CREATE INDEX idx_download_jobs_state ON download_jobs (state, created_at); + +-- Long-form resume points, separate from mood playback. +CREATE TABLE longform_progress ( + user_name TEXT NOT NULL REFERENCES users(name) ON DELETE CASCADE, + song_id TEXT NOT NULL REFERENCES songs(id) ON DELETE CASCADE, + position_seconds INTEGER NOT NULL DEFAULT 0, + duration_seconds INTEGER NOT NULL DEFAULT 0, + completed BOOLEAN NOT NULL DEFAULT 0, + updated_at TEXT NOT NULL, + PRIMARY KEY (user_name, song_id) +); + +CREATE INDEX idx_longform_progress_updated ON longform_progress (user_name, updated_at); diff --git a/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt b/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt index 72aded9..326ee95 100644 --- a/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt +++ b/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt @@ -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 -> diff --git a/src/test/kotlin/win/taptap/musicmanager/download/DownloadServiceTest.kt b/src/test/kotlin/win/taptap/musicmanager/download/DownloadServiceTest.kt new file mode 100644 index 0000000..7e96bd0 --- /dev/null +++ b/src/test/kotlin/win/taptap/musicmanager/download/DownloadServiceTest.kt @@ -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" + } + } + } +}