Fixed local copy of attached files being leaked if it isn't being sent

This commit is contained in:
Sylvain Berfini 2023-01-17 14:54:40 +01:00
parent b3d0897897
commit 327f296ac0
3 changed files with 27 additions and 9 deletions

View file

@ -695,9 +695,18 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
chatSendingViewModel.textToSend.value = textToShare chatSendingViewModel.textToSend.value = textToShare
} }
if (filesToShare?.isNotEmpty() == true) { if (filesToShare?.isNotEmpty() == true) {
for (path in filesToShare) { lifecycleScope.launch {
Log.i("[Chat Room] Found $path file to share") withContext(Dispatchers.Main) {
chatSendingViewModel.addAttachment(path) chatSendingViewModel.attachingFileInProgress.value = true
for (filePath in filesToShare) {
val path = FileUtils.copyToLocalStorage(filePath)
Log.i("[Chat Room] Found [$filePath] file to share, matching path is [$path]")
if (path != null) {
chatSendingViewModel.addAttachment(path)
}
}
chatSendingViewModel.attachingFileInProgress.value = false
}
} }
} }
@ -710,7 +719,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
chatSendingViewModel.attachingFileInProgress.value = true chatSendingViewModel.attachingFileInProgress.value = true
val path = FileUtils.getFilePath(requireContext(), uri) val path = FileUtils.getFilePath(requireContext(), uri)
Log.i("[Chat Room] Rich content URI: $uri matching path is: $path") Log.i("[Chat Room] Rich content URI [$uri] matching path is [$path]")
if (path != null) { if (path != null) {
chatSendingViewModel.addAttachment(path) chatSendingViewModel.addAttachment(path)
} }
@ -821,6 +830,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
chatSendingViewModel.temporaryFileUploadPath chatSendingViewModel.temporaryFileUploadPath
) )
) { ) {
Log.i("[Chat Room] Found [$fileToUploadPath] file from intent")
chatSendingViewModel.addAttachment(fileToUploadPath) chatSendingViewModel.addAttachment(fileToUploadPath)
} }
chatSendingViewModel.attachingFileInProgress.value = false chatSendingViewModel.attachingFileInProgress.value = false

View file

@ -140,6 +140,10 @@ class ChatMessageSendingViewModel(private val chatRoom: ChatRoom) : ViewModel()
override fun onCleared() { override fun onCleared() {
pendingChatMessageToReplyTo.value?.destroy() pendingChatMessageToReplyTo.value?.destroy()
for (pendingAttachment in attachments.value.orEmpty()) {
removeAttachment(pendingAttachment)
}
if (this::recorder.isInitialized) { if (this::recorder.isInitialized) {
if (recorder.state != RecorderState.Closed) { if (recorder.state != RecorderState.Closed) {
recorder.close() recorder.close()
@ -192,6 +196,10 @@ class ChatMessageSendingViewModel(private val chatRoom: ChatRoom) : ViewModel()
list.remove(attachment) list.remove(attachment)
attachments.value = list attachments.value = list
val pathToDelete = attachment.path
Log.i("[Chat Message Sending] Attachment is being removed, delete local copy [$pathToDelete]")
FileUtils.deleteFile(pathToDelete)
sendMessageEnabled.value = textToSend.value.orEmpty().trim().isNotEmpty() || list.isNotEmpty() || isPendingVoiceRecord.value == true sendMessageEnabled.value = textToSend.value.orEmpty().trim().isNotEmpty() || list.isNotEmpty() || isPendingVoiceRecord.value == true
if (!corePreferences.allowMultipleFilesAndTextInSameMessage) { if (!corePreferences.allowMultipleFilesAndTextInSameMessage) {
attachFileEnabled.value = list.isEmpty() attachFileEnabled.value = list.isEmpty()

View file

@ -184,7 +184,7 @@ class FileUtils {
filePath = dataUri.toString() filePath = dataUri.toString()
Log.i("[File Utils] Using data URI $filePath") Log.i("[File Utils] Using data URI $filePath")
} }
filePath = cleanFilePath(filePath) filePath = copyToLocalStorage(filePath)
if (filePath != null) list.add(filePath) if (filePath != null) list.add(filePath)
} }
return list return list
@ -201,13 +201,13 @@ class FileUtils {
filePath = temporaryImageFilePath.absolutePath filePath = temporaryImageFilePath.absolutePath
Log.i("[File Utils] Data URI is null, using $filePath") Log.i("[File Utils] Data URI is null, using $filePath")
} }
filePath = cleanFilePath(filePath) filePath = copyToLocalStorage(filePath)
if (filePath != null) return arrayListOf(filePath) if (filePath != null) return arrayListOf(filePath)
} }
} else if (temporaryImageFilePath?.exists() == true) { } else if (temporaryImageFilePath?.exists() == true) {
filePath = temporaryImageFilePath.absolutePath filePath = temporaryImageFilePath.absolutePath
Log.i("[File Utils] Data is null, using $filePath") Log.i("[File Utils] Data is null, using $filePath")
filePath = cleanFilePath(filePath) filePath = copyToLocalStorage(filePath)
if (filePath != null) return arrayListOf(filePath) if (filePath != null) return arrayListOf(filePath)
} }
return arrayListOf() return arrayListOf()
@ -237,10 +237,10 @@ class FileUtils {
filePath = temporaryImageFilePath.absolutePath filePath = temporaryImageFilePath.absolutePath
Log.i("[File Utils] Data is null, using $filePath") Log.i("[File Utils] Data is null, using $filePath")
} }
return cleanFilePath(filePath) return copyToLocalStorage(filePath)
} }
private suspend fun cleanFilePath(filePath: String?): String? { suspend fun copyToLocalStorage(filePath: String?): String? {
if (filePath != null) { if (filePath != null) {
val uriToParse = Uri.parse(filePath) val uriToParse = Uri.parse(filePath)
if (filePath.startsWith("content://com.android.contacts/contacts/lookup/")) { if (filePath.startsWith("content://com.android.contacts/contacts/lookup/")) {