Skip to content

Commit

Permalink
feat: Auto delete temp and obsolete downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Vodes committed Mar 27, 2024
1 parent b029925 commit f22bbf8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ import moe.styx.common.compose.appConfig
import moe.styx.common.compose.http.Endpoints
import moe.styx.common.compose.http.getList
import moe.styx.common.compose.http.getObject
import moe.styx.common.compose.threads.DownloadQueue
import moe.styx.common.compose.utils.ServerStatus
import moe.styx.common.data.*
import moe.styx.common.extension.currentUnixSeconds
import moe.styx.common.extension.eqI
import moe.styx.common.util.SYSTEMFILES
import moe.styx.common.util.launchGlobal
import moe.styx.common.util.launchThreaded
import okio.Path.Companion.toPath

/**
Expand Down Expand Up @@ -70,10 +73,23 @@ object Storage {
)
loadingProgress.emit("Updating image cache...\nThis may take a minute or two.")
ImageCache.checkForNewImages()
deleteFilesForDeletedEntries()
}
isLoaded.emit(true)
}

private fun deleteFilesForDeletedEntries() = launchThreaded {
val entries = stores.entryStore.getOrEmpty()
if (SYSTEMFILES.exists(DownloadQueue.downloadedDir)) {
for (file in SYSTEMFILES.listRecursively(DownloadQueue.downloadedDir)) {
val nameWithoutExt = file.name.substringBeforeLast(".")
val correspondingEntry = entries.find { it.GUID eqI nameWithoutExt }
if (correspondingEntry == null)
SYSTEMFILES.delete(file)
}
}
}

private fun createDirectories() {
SYSTEMFILES.createDirectories("${appConfig().appStoragePath}/store".toPath())
SYSTEMFILES.createDirectories("${appConfig().appStoragePath}/queued".toPath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import moe.styx.common.compose.http.isLoggedIn
import moe.styx.common.compose.http.login
import moe.styx.common.compose.utils.ServerStatus
import moe.styx.common.data.MediaEntry
import moe.styx.common.extension.currentUnixSeconds
import moe.styx.common.extension.eqI
import moe.styx.common.http.DownloadResult
import moe.styx.common.http.downloadFileStream
Expand All @@ -24,6 +25,8 @@ import moe.styx.common.util.launchGlobal
import moe.styx.common.util.launchThreaded
import okio.Path
import okio.Path.Companion.toPath
import kotlin.time.DurationUnit
import kotlin.time.toDuration

object DownloadQueue : LifecycleTrackedJob(false) {
val currentDownload: MutableStateFlow<DownloadProgress?> = MutableStateFlow(null)
Expand All @@ -37,6 +40,17 @@ object DownloadQueue : LifecycleTrackedJob(false) {
}

override fun createJob(): Job = launchGlobal {
if (SYSTEMFILES.exists(tempDir)) {
val now = currentUnixSeconds() * 1000
val oneMinMillis = 1.toDuration(DurationUnit.MINUTES).toLong(DurationUnit.MILLISECONDS)
val files = SYSTEMFILES.listRecursively(tempDir)
for (file in files) {
val metadata = SYSTEMFILES.metadataOrNull(file) ?: continue
if ((metadata.lastModifiedAtMillis ?: 0) < (now - oneMinMillis))
SYSTEMFILES.delete(file)
}
}
delay(5000)
while (true) {
if (ServerStatus.lastKnown == ServerStatus.UNKNOWN || !isLoggedIn()) {
delay(10000)
Expand Down

0 comments on commit f22bbf8

Please sign in to comment.