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.relativePath) log.info("GC removed unreferenced song {} ({}/{})", song.id, song.source, song.externalId) 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) // Use the actual first path segment: new downloads are // `-` while older YouTube files are ``. val known = SongRepository.all() .mapNotNull { song -> song.relativePath.replace('\\', '/').split('/').firstOrNull()?.takeIf { it.isNotBlank() } } .toSet() mediaStore.songDirectories().forEach { directory -> val name = directory.fileName?.toString() ?: return@forEach if (name 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 } }