feat: add Deezer as the default download source
- V9 adds source/external_id to songs, moods, recommendations and jobs - Deezer public catalog for search, related album covers and metadata - streamrip downloader configured with DEEZER_ARL, 128/320 MP3 - per-mood source selector; switching clears and refills recommendations - keep YouTube Music as a full second catalog and downloader - media layout <source>-<externalId>, legacy YouTube folders still read - Docker/streamrip install, chart ARL secret, docs and dual-source smoke test
This commit is contained in:
1 parent
81a47f4cd2
commit
141698282d
43 files changed
+2072
-605
No files matched your search
@@ -46,8 +46,8 @@ class MediaGcService(
|
||||
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)
|
||||
mediaStore.deleteSongDirectory(song.relativePath)
|
||||
log.info("GC removed unreferenced song {} ({}/{})", song.id, song.source, song.externalId)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -93,11 +93,17 @@ class MediaGcService(
|
||||
*/
|
||||
private fun collectOrphanDirectories() {
|
||||
val cutoff = Instant.now().minus(ORPHAN_GRACE_MINUTES, ChronoUnit.MINUTES)
|
||||
val known = SongRepository.all().map { it.videoId }.toSet()
|
||||
// Use the actual first path segment: new downloads are
|
||||
// `<source>-<id>` while older YouTube files are `<videoId>`.
|
||||
val known = SongRepository.all()
|
||||
.mapNotNull { song ->
|
||||
song.relativePath.replace('\\', '/').split('/').firstOrNull()?.takeIf { it.isNotBlank() }
|
||||
}
|
||||
.toSet()
|
||||
|
||||
mediaStore.songDirectories().forEach { directory ->
|
||||
val videoId = directory.fileName?.toString() ?: return@forEach
|
||||
if (videoId in known) return@forEach
|
||||
val name = directory.fileName?.toString() ?: return@forEach
|
||||
if (name in known) return@forEach
|
||||
val modified = runCatching {
|
||||
Files.getLastModifiedTime(directory)
|
||||
}.getOrNull() ?: return@forEach
|
||||
|
||||
@@ -3,14 +3,15 @@ package win.taptap.musicmanager.media
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
|
||||
/**
|
||||
* Layout of the server-owned media root:
|
||||
*
|
||||
* <media>/<videoId>/<sanitized-title>.mp3
|
||||
* <media>/<source>-<externalId>/<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.
|
||||
* Older YouTube downloads live in `<media>/<videoId>/...`; those directories
|
||||
* are still read and are assumed to belong to YouTube.
|
||||
*/
|
||||
class MediaStore(val root: Path) {
|
||||
|
||||
@@ -25,23 +26,48 @@ class MediaStore(val root: Path) {
|
||||
return candidate.takeIf { it.startsWith(root) }
|
||||
}
|
||||
|
||||
fun directoryName(source: String, externalId: String): String = "$source-$externalId"
|
||||
|
||||
/** 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 target(source: String, externalId: String, title: String): Path =
|
||||
root.resolve(directoryName(source, externalId))
|
||||
.resolve("${sanitizeName(title)}.mp3")
|
||||
|
||||
fun relativePath(videoId: String, title: String): String =
|
||||
"$videoId/${sanitizeName(title)}.mp3"
|
||||
fun relativePath(source: String, externalId: String, title: String): String =
|
||||
"${directoryName(source, externalId)}/${sanitizeName(title)}.mp3"
|
||||
|
||||
fun videoIdFrom(relativePath: String): String? =
|
||||
relativePath.replace('\\', '/')
|
||||
.split('/')
|
||||
.firstOrNull()
|
||||
?.takeIf { it.isNotBlank() && it != "." && it != ".." }
|
||||
/**
|
||||
* Reads a directory name back into a source and external id. A directory
|
||||
* without a known prefix is a legacy YouTube download.
|
||||
*/
|
||||
fun sourceAndIdFrom(directoryName: String): Pair<String, String>? {
|
||||
val name = directoryName.trim()
|
||||
if (name.isEmpty() || name == "." || name == "..") return null
|
||||
val separator = name.indexOf('-')
|
||||
if (separator > 0) {
|
||||
val prefix = name.substring(0, separator)
|
||||
val id = name.substring(separator + 1)
|
||||
when {
|
||||
prefix == MusicSource.DEEZER && id.isNotEmpty() && id.all(Char::isDigit) ->
|
||||
return MusicSource.DEEZER to id
|
||||
prefix == MusicSource.YOUTUBE && id.length == YOUTUBE_ID_LENGTH ->
|
||||
return MusicSource.YOUTUBE to id
|
||||
}
|
||||
}
|
||||
return MusicSource.YOUTUBE to name
|
||||
}
|
||||
|
||||
fun deleteSongDirectory(videoId: String) {
|
||||
val dir = root.resolve(videoId).normalize()
|
||||
if (!dir.startsWith(root) || dir == root) return
|
||||
runCatching { dir.toFile().deleteRecursively() }
|
||||
fun sourceAndIdFromPath(relativePath: String): Pair<String, String>? {
|
||||
val first = relativePath.replace('\\', '/').split('/').firstOrNull() ?: return null
|
||||
return sourceAndIdFrom(first)
|
||||
}
|
||||
|
||||
/** Deletes the directory that actually contains [relativePath]. */
|
||||
fun deleteSongDirectory(relativePath: String) {
|
||||
val file = resolveRelative(relativePath) ?: return
|
||||
val directory = file.parent ?: return
|
||||
if (directory == root || !directory.startsWith(root)) return
|
||||
runCatching { directory.toFile().deleteRecursively() }
|
||||
}
|
||||
|
||||
fun songDirectories(): List<Path> {
|
||||
@@ -53,6 +79,7 @@ class MediaStore(val root: Path) {
|
||||
|
||||
companion object {
|
||||
private const val MAX_NAME_BYTES = 120
|
||||
private const val YOUTUBE_ID_LENGTH = 11
|
||||
private val INVALID = Regex("[\\\\/:*?\"<>|\\p{Cntrl}]")
|
||||
private val SPACES = Regex("\\s+")
|
||||
|
||||
|
||||
Reference in new issue
Block a user