Option to delete old cached covers

This commit is contained in:
Jay 2020-05-09 15:01:04 -04:00
parent 892edd2b92
commit 50405887d2
3 changed files with 57 additions and 0 deletions

View File

@ -1,7 +1,18 @@
package eu.kanade.tachiyomi.data.cache
import android.content.Context
import android.text.format.Formatter
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.util.storage.DiskUtil
import eu.kanade.tachiyomi.util.system.executeOnIO
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
import java.io.IOException
import java.io.InputStream
@ -23,6 +34,34 @@ class CoverCache(private val context: Context) {
private val cacheDir = context.getExternalFilesDir("covers")
?: File(context.filesDir, "covers").also { it.mkdirs() }
fun deleteOldCovers() {
GlobalScope.launch(Dispatchers.Default) {
val db = Injekt.get<DatabaseHelper>()
var deletedSize = 0L
val urls = db.getLibraryMangas().executeOnIO().mapNotNull {
it.thumbnail_url?.let { url ->
return@mapNotNull DiskUtil.hashKeyForDisk(url)
}
null
}
val files = cacheDir.listFiles()?.iterator() ?: return@launch
while (files.hasNext()) {
val file = files.next()
if (file.name !in urls) {
deletedSize += file.length()
file.delete()
}
}
withContext(Dispatchers.Main) {
context.toast(
context.getString(
R.string.deleted_, Formatter.formatFileSize(context, deletedSize)
)
)
}
}
}
/**
* Returns the cover from cache.
*

View File

@ -14,6 +14,7 @@ import androidx.preference.PreferenceScreen
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.cache.ChapterCache
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
@ -43,6 +44,8 @@ class SettingsAdvancedController : SettingsController() {
private val db: DatabaseHelper by injectLazy()
private val coverCache: CoverCache by injectLazy()
@SuppressLint("BatteryLife")
override fun setupPreferenceScreen(screen: PreferenceScreen) = with(screen) {
titleRes = R.string.advanced
@ -97,6 +100,17 @@ class SettingsAdvancedController : SettingsController() {
onClick { cleanupDownloads() }
}
preference {
titleRes = R.string.clean_up_cached_covers
summaryRes = R.string.delete_old_covers_in_library
onClick {
context.toast(R.string.starting_cleanup)
coverCache.deleteOldCovers()
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val pm = context.getSystemService(Context.POWER_SERVICE) as? PowerManager?
if (pm != null) preference {

View File

@ -537,6 +537,9 @@
<item quantity="one">Cleanup done. Removed %d folder</item>
<item quantity="other">Cleanup done. Removed %d folders</item>
</plurals>
<string name="clean_up_cached_covers">Clean up cached covers</string>
<string name="delete_old_covers_in_library">Delete old and unused cached covers of
manga in your library that has been updated</string>
<!-- About section -->
<string name="version">Version</string>
@ -653,6 +656,7 @@
<string name="create">Create</string>
<string name="default_value">Default</string>
<string name="delete">Delete</string>
<string name="deleted_">Deleted: %1$s</string>
<string name="display">Display</string>
<string name="drag_handle">Drag handle</string>
<string name="edit">Edit</string>