Skip to content

Commit 234987f

Browse files
committed
Added avatar cache cleaner
1 parent 7227991 commit 234987f

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.session.libsession.avatars
2+
3+
import android.app.Application
4+
import com.bumptech.glide.Glide
5+
import kotlinx.coroutines.CoroutineScope
6+
import kotlinx.coroutines.Dispatchers
7+
import kotlinx.coroutines.launch
8+
import kotlinx.coroutines.withContext
9+
import org.session.libsession.utilities.recipients.RemoteFile
10+
import org.session.libsignal.utilities.Log
11+
import org.thoughtcrime.securesms.attachments.RemoteFileDownloadWorker
12+
import org.thoughtcrime.securesms.dependencies.ManagerScope
13+
import org.thoughtcrime.securesms.glide.RecipientAvatarDownloadManager
14+
import java.io.File
15+
import javax.inject.Inject
16+
import javax.inject.Singleton
17+
18+
@Singleton
19+
class AvatarCacheCleaner @Inject constructor(
20+
private val application: Application,
21+
private val avatarDownloadManager: RecipientAvatarDownloadManager, // we can reuse some logic here
22+
@param:ManagerScope private val coroutineScope: CoroutineScope
23+
) {
24+
25+
companion object {
26+
const val TAG = "AvatarCacheCleaner"
27+
}
28+
29+
/**
30+
* Deletes avatar files under cache/remote_files that are no longer referenced
31+
* in the current config. Returns number of files deleted.
32+
*/
33+
suspend fun cleanUpAvatars(): Int = withContext(Dispatchers.IO) {
34+
// 1) Build the set of still-wanted RemoteFiles (contacts + groups)
35+
val wantedRemotes: Set<RemoteFile> = avatarDownloadManager.getAllAvatars()
36+
37+
// 2) Map to actual files (same hashing/location as downloader)
38+
val wantedFiles: Set<File> = wantedRemotes
39+
.map { RemoteFileDownloadWorker.computeFileName(application, it) }
40+
.toSet()
41+
42+
// 3) Delete everything not wanted in cache/remote_files
43+
val dir = File(application.cacheDir, "remote_files")
44+
val files = dir.listFiles().orEmpty()
45+
var deleted = 0
46+
for (f in files) {
47+
if (f !in wantedFiles && f.delete()) deleted++
48+
}
49+
50+
// 4) Clear Glide cache. Might need this now but we should remove after we fully migrate to Coil
51+
// Note to keep this off the main thread
52+
Glide.get(application).clearDiskCache()
53+
54+
deleted
55+
}
56+
57+
fun launchAvatarCleanup(cancelInFlight: Boolean = false) {
58+
coroutineScope.launch(Dispatchers.IO) {
59+
if (cancelInFlight) {
60+
RemoteFileDownloadWorker.cancelAll(application)
61+
}
62+
val deleted = cleanUpAvatars()
63+
Log.d(TAG, "Avatar cache removed: $deleted files")
64+
}
65+
}
66+
}

app/src/main/java/org/thoughtcrime/securesms/groups/OpenGroupManager.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package org.thoughtcrime.securesms.groups
22

3+
import kotlinx.coroutines.CoroutineScope
4+
import kotlinx.coroutines.Dispatchers
35
import kotlinx.coroutines.flow.first
46
import kotlinx.coroutines.flow.mapNotNull
7+
import kotlinx.coroutines.launch
58
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
9+
import org.session.libsession.avatars.AvatarCacheCleaner
610
import org.session.libsession.messaging.open_groups.OpenGroup
711
import org.session.libsession.messaging.open_groups.OpenGroupApi
812
import org.session.libsession.messaging.sending_receiving.pollers.OpenGroupPollerManager
913
import org.session.libsession.snode.utilities.await
1014
import org.session.libsession.utilities.ConfigFactoryProtocol
1115
import org.session.libsignal.utilities.Log
1216
import org.thoughtcrime.securesms.database.LokiAPIDatabase
17+
import org.thoughtcrime.securesms.dependencies.ManagerScope
1318
import javax.inject.Inject
1419
import javax.inject.Singleton
1520

@@ -23,6 +28,7 @@ class OpenGroupManager @Inject constructor(
2328
private val configFactory: ConfigFactoryProtocol,
2429
private val pollerManager: OpenGroupPollerManager,
2530
private val lokiAPIDatabase: LokiAPIDatabase,
31+
private val avatarCacheCleaner: AvatarCacheCleaner,
2632
) {
2733
suspend fun add(server: String, room: String, publicKey: String) {
2834
// Fetch the server's capabilities upfront to see if this server is actually running
@@ -59,6 +65,8 @@ class OpenGroupManager @Inject constructor(
5965
configs.userGroups.eraseCommunity(server, room)
6066
configs.convoInfoVolatile.eraseCommunity(server, room)
6167
}
68+
69+
avatarCacheCleaner.launchAvatarCleanup()
6270
}
6371

6472
suspend fun addOpenGroup(urlAsString: String) {

0 commit comments

Comments
 (0)