security: ensure media files are deleted during panic mode (#607)

Closes #591
- Added FileUtils.clearAllMedia() to recursively delete media directories and cache.
- Called clearAllMedia() in ChatViewModel.panicClearAllData().

fix: correct voice notes directory path for cleanup

- Updated FileUtils.clearAllMedia to use 'voicenotes' instead of 'voice_notes' to match VoiceRecorder.kt

fix: update media cleanup to include cache directories

- Updated FileUtils.clearAllMedia to explicitly clean 'files/incoming' and 'images/incoming' from context.cacheDir, reflecting the storage location change from issue #592.
- Maintained legacy cleanup for context.filesDir.

Co-authored-by: a1denvalu3 <>
This commit is contained in:
a1denvalu3
2026-01-15 22:32:30 +07:00
committed by GitHub
co-authored by a1denvalu3 <>
parent 5501141ae0
commit 39e43fa923
2 changed files with 53 additions and 0 deletions
@@ -273,4 +273,54 @@ object FileUtils {
else -> com.bitchat.android.model.BitchatMessageType.File
}
}
/**
* Recursively delete all media files (incoming and outgoing)
* Used for Panic Mode cleanup
*/
fun clearAllMedia(context: Context) {
try {
// Clear files dir subdirectories (legacy storage and outgoing)
val filesDir = context.filesDir
val dirsToClear = listOf(
"files/incoming",
"files/outgoing",
"images/incoming",
"images/outgoing",
"voicenotes"
)
dirsToClear.forEach { subDir ->
val dir = File(filesDir, subDir)
if (dir.exists()) {
dir.deleteRecursively()
Log.d(TAG, "Deleted media directory from filesDir: $subDir")
}
}
// Clear cache dir subdirectories (new incoming storage)
// Note: cacheDir.deleteRecursively() below would handle this, but being explicit ensures these
// specific media folders are targeted even if full cache clear fails or is modified later.
val cacheDir = context.cacheDir
val cacheDirsToClear = listOf(
"files/incoming",
"images/incoming"
)
cacheDirsToClear.forEach { subDir ->
val dir = File(cacheDir, subDir)
if (dir.exists()) {
dir.deleteRecursively()
Log.d(TAG, "Deleted media directory from cacheDir: $subDir")
}
}
// Also clear entire cache dir as a catch-all
context.cacheDir.deleteRecursively()
Log.d(TAG, "Cleared entire cache directory")
} catch (e: Exception) {
Log.e(TAG, "Failed to clear media files", e)
}
}
}
@@ -926,6 +926,9 @@ class ChatViewModel(
// Clear all notifications
notificationManager.clearAllNotifications()
// Clear all media files
com.bitchat.android.features.file.FileUtils.clearAllMedia(getApplication())
// Clear Nostr/geohash state, keys, connections, bookmarks, and reinitialize from scratch
try {