musikr: introduce cover cleanup
Helps reduce overall memory use.
This commit is contained in:
@@ -32,6 +32,8 @@ interface CoverFiles {
|
||||
|
||||
suspend fun write(name: String, block: suspend (OutputStream) -> Unit): CoverFile?
|
||||
|
||||
suspend fun deleteWhere(block: (String) -> Boolean)
|
||||
|
||||
companion object {
|
||||
suspend fun at(dir: File): CoverFiles {
|
||||
withContext(Dispatchers.IO) { check(dir.exists() && dir.isDirectory) }
|
||||
@@ -83,6 +85,12 @@ private class CoverFilesImpl(private val dir: File) : CoverFiles {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun deleteWhere(block: (String) -> Boolean) {
|
||||
withContext(Dispatchers.IO) {
|
||||
dir.listFiles { file -> block(file.name) }?.forEach { it.deleteRecursively() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class CoverFileImpl(private val file: File) : CoverFile {
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
package org.oxycblt.musikr.cover
|
||||
|
||||
import org.oxycblt.musikr.Library
|
||||
|
||||
interface StoredCovers {
|
||||
suspend fun obtain(id: String): Cover?
|
||||
|
||||
@@ -32,6 +34,8 @@ interface StoredCovers {
|
||||
|
||||
interface MutableStoredCovers : StoredCovers {
|
||||
suspend fun write(data: ByteArray): Cover?
|
||||
|
||||
suspend fun cleanup(assuming: Library)
|
||||
}
|
||||
|
||||
private class FileStoredCovers(
|
||||
@@ -49,6 +53,11 @@ private class FileStoredCovers(
|
||||
?.let { FileCover(id, it) }
|
||||
}
|
||||
|
||||
override suspend fun cleanup(assuming: Library) {
|
||||
val used = assuming.songs.mapNotNullTo(mutableSetOf()) { it.cover?.id?.let(::getFileName) }
|
||||
coverFiles.deleteWhere { it !in used }
|
||||
}
|
||||
|
||||
private fun getFileName(id: String) = "$id.${coverFormat.extension}"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user