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,11 +695,20 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
chatSendingViewModel.textToSend.value = textToShare
}
if (filesToShare?.isNotEmpty() == true) {
for (path in filesToShare) {
Log.i("[Chat Room] Found $path file to share")
lifecycleScope.launch {
withContext(Dispatchers.Main) {
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
}
}
}
sharedViewModel.richContentUri.observe(
viewLifecycleOwner
@ -710,7 +719,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
withContext(Dispatchers.Main) {
chatSendingViewModel.attachingFileInProgress.value = true
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) {
chatSendingViewModel.addAttachment(path)
}
@ -821,6 +830,7 @@ class DetailChatRoomFragment : MasterFragment<ChatRoomDetailFragmentBinding, Cha
chatSendingViewModel.temporaryFileUploadPath
)
) {
Log.i("[Chat Room] Found [$fileToUploadPath] file from intent")
chatSendingViewModel.addAttachment(fileToUploadPath)
}
chatSendingViewModel.attachingFileInProgress.value = false

View file

@ -140,6 +140,10 @@ class ChatMessageSendingViewModel(private val chatRoom: ChatRoom) : ViewModel()
override fun onCleared() {
pendingChatMessageToReplyTo.value?.destroy()
for (pendingAttachment in attachments.value.orEmpty()) {
removeAttachment(pendingAttachment)
}
if (this::recorder.isInitialized) {
if (recorder.state != RecorderState.Closed) {
recorder.close()
@ -192,6 +196,10 @@ class ChatMessageSendingViewModel(private val chatRoom: ChatRoom) : ViewModel()
list.remove(attachment)
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
if (!corePreferences.allowMultipleFilesAndTextInSameMessage) {
attachFileEnabled.value = list.isEmpty()

View file

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