package win.taptap.musicmanager.scanner import java.nio.file.Files import java.nio.file.Path import java.nio.file.attribute.BasicFileAttributes import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicReference import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import org.slf4j.LoggerFactory import win.taptap.musicmanager.db.SongRepository import win.taptap.musicmanager.media.MediaStore /** * Rebuilds the song index from the media root. The first path segment is the * YouTube video id, so a file's identity never depends on its tags. * * Only one scan can run at a time. A file's `(mtime, size)` pair decides whether * tags need to be re-read; unchanged files are skipped. */ class MediaScanner( private val mediaStore: MediaStore, private val indexer: SongIndexer, private val onScanFinished: (ScanStatus) -> Unit = {}, ) { private val log = LoggerFactory.getLogger(MediaScanner::class.java) private val running = AtomicBoolean(false) private val status = AtomicReference(ScanStatus(scanning = false, count = 0)) fun status(): ScanStatus = status.get() /** Starts a scan in [scope]. Returns false when a scan is already running. */ fun start(scope: CoroutineScope): Boolean { if (!running.compareAndSet(false, true)) return false status.set(ScanStatus(scanning = true, count = 0)) scope.launch { runScan() } return true } /** Blocking variant for tests and the startup scan. */ fun scanBlocking(): ScanStatus { check(running.compareAndSet(false, true)) { "A scan is already running" } status.set(ScanStatus(scanning = true, count = 0)) return runScan() } private fun runScan(): ScanStatus { var count = 0L val seenSongIds = mutableSetOf() var error: String? = null var rootFailed = false try { val root = mediaStore.root if (!Files.isDirectory(root) || !Files.isReadable(root)) { rootFailed = true error = "Media root is not readable: $root" log.error("Skipping scan: media root is not readable: {}", root) } else { Files.walk(root).use { paths -> paths.filter { Files.isRegularFile(it) }.forEach { file -> if (!file.fileName.toString().endsWith(".mp3", ignoreCase = true)) { return@forEach } val relativePath = root.relativize(file).joinToString("/") { it.toString() } val videoId = mediaStore.videoIdFrom(relativePath) if (videoId == null) { log.debug("Skipping file outside a video folder: {}", file) return@forEach } try { val existing = SongRepository.byVideoId(videoId) val attributes = Files.readAttributes( file, BasicFileAttributes::class.java, ) val mtime = attributes.lastModifiedTime().toMillis() val size = attributes.size() if (existing != null && existing.fileMtime == mtime && existing.size == size) { seenSongIds += existing.id count++ status.set(ScanStatus(scanning = true, count = count)) return@forEach } val song = indexer.index(videoId, file) if (song == null) { if (existing != null) seenSongIds += existing.id log.warn("Could not read MP3 metadata: {}", file) return@forEach } seenSongIds += song.id count++ status.set(ScanStatus(scanning = true, count = count)) } catch (e: Exception) { SongRepository.byVideoId(videoId)?.let { seenSongIds += it.id } log.warn("Could not index file: $file", e) } } } } if (rootFailed) { // An unmounted or temporarily unreadable root must not be // mistaken for deleted files. Keep the last known index. log.warn("Skipping orphan cleanup because the media root failed to scan") } else { SongRepository.all().forEach { song -> if (song.id !in seenSongIds) { SongRepository.delete(song.id) } } } } catch (t: Throwable) { error = t.message ?: t::class.simpleName log.error("Media scan failed after $count files", t) } finally { running.set(false) } val finalStatus = ScanStatus(scanning = false, count = count, error = error) status.set(finalStatus) log.info("Media scan finished: {} files, error={}", count, error) runCatching { onScanFinished(finalStatus) } .onFailure { log.warn("Scan completion hook failed", it) } return finalStatus } }