Skip to content

Commit 4be43b9

Browse files
committed
Added method in RemotefileDownloadWorker with the implementation details of files
1 parent 368a169 commit 4be43b9

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

app/src/main/java/org/session/libsession/avatars/AvatarCacheCleaner.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ class AvatarCacheCleaner @Inject constructor(
5151
.toSet()
5252

5353
// 5) Delete everything not wanted in cache/remote_files
54-
val dir = File(application.cacheDir, "remote_files")
55-
val files = dir.listFiles().orEmpty()
54+
val files = RemoteFileDownloadWorker.listDownloadedFiles(application)
5655
var deleted = 0
5756
for (file in files) {
5857
if (file !in wantedFiles && file.delete()) deleted++

app/src/main/java/org/thoughtcrime/securesms/attachments/RemoteFileDownloadWorker.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class RemoteFileDownloadWorker @AssistedInject constructor(
261261
private const val ARG_COMMUNITY_ROOM_ID = "community_room_id"
262262
private const val ARG_COMMUNITY_FILE_ID = "community_file_id"
263263

264+
private const val SUBDIRECTORY = "remote_files"
265+
264266
private fun RemoteFile.sha256Hash(): String {
265267
val hash = MessageDigest.getInstance("SHA-256")
266268
when (this) {
@@ -278,9 +280,12 @@ class RemoteFileDownloadWorker @AssistedInject constructor(
278280
return hash.digest().toHexString()
279281
}
280282

283+
private fun downloadsDirectory(context: Context): File =
284+
File(context.cacheDir, SUBDIRECTORY)
285+
281286
// Deterministically get the file path for the given remote file.
282287
fun computeFileName(context: Context, remote: RemoteFile): File {
283-
return File(context.cacheDir, "remote_files/${remote.sha256Hash()}")
288+
return File(downloadsDirectory(context), remote.sha256Hash())
284289
}
285290

286291
fun cancelAll(context: Context) {
@@ -297,6 +302,12 @@ class RemoteFileDownloadWorker @AssistedInject constructor(
297302
)
298303
}
299304

305+
/** Returns all currently downloaded files (may be empty). */
306+
fun listDownloadedFiles(context: Context): List<File> {
307+
val directory = downloadsDirectory(context)
308+
return directory.listFiles()?.toList().orEmpty()
309+
}
310+
300311
/**
301312
* @param isOldAvatarOf used to indicate that this file is an avatar of a specific address. This
302313
* information is optional and only used for migration purposes (to move the avatar from

0 commit comments

Comments
 (0)