From 81c8546cf527f06065c1b1d36691cbe9012eabdb Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 20 Aug 2024 09:58:38 -0400 Subject: [PATCH 1/3] Create temporary files before uploading --- .../feedbackform/FeedbackFormAttachment.kt | 2 - .../feedbackform/FeedbackFormViewModel.kt | 62 +++++++++++++------ 2 files changed, 44 insertions(+), 20 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormAttachment.kt b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormAttachment.kt index f174e8495d29..e4bb890a7119 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormAttachment.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormAttachment.kt @@ -1,11 +1,9 @@ package org.wordpress.android.ui.main.feedbackform import android.net.Uri -import java.io.File data class FeedbackFormAttachment( val uri: Uri, - val tempFile: File, val mimeType: String, val attachmentType: FeedbackFormAttachmentType, val size: Long, diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt index 64494af2c4e2..2f8225a802d0 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt @@ -32,6 +32,7 @@ import org.wordpress.android.util.extensions.copyToTempFile import org.wordpress.android.util.extensions.fileSize import org.wordpress.android.util.extensions.mimeType import org.wordpress.android.viewmodel.ScopedViewModel +import java.io.File import java.io.IOException import javax.inject.Inject import javax.inject.Named @@ -76,19 +77,23 @@ class FeedbackFormViewModel @Inject constructor( if (_attachments.value.isNotEmpty()) { showProgressDialog(R.string.uploading) launch { - val files = _attachments.value.map { it.tempFile } + val tempFiles = createTempFiles(context) try { - val tokens = zendeskUploadHelper.uploadFileAttachments(files) - withContext(Dispatchers.Main) { - createZendeskFeedbackRequest( - context = context, - attachmentTokens = tokens - ) + try { + val tokens = zendeskUploadHelper.uploadFileAttachments(tempFiles) + withContext(Dispatchers.Main) { + createZendeskFeedbackRequest( + context = context, + attachmentTokens = tokens + ) + } + } catch (e: IOException) { + hideProgressDialog() + onFailure(context, e.message) + return@launch } - } catch (e: IOException) { - hideProgressDialog() - onFailure(context, e.message) - return@launch + } finally { + deleteTempFiles(tempFiles) } } } else { @@ -96,6 +101,34 @@ class FeedbackFormViewModel @Inject constructor( } } + /** + * Creates temporary files for each attachment and returns a list of their paths + */ + private fun createTempFiles(context: Context): List { + val tempFiles = ArrayList() + val uris = _attachments.value.map { it.uri } + for (uri in uris) { + uri.copyToTempFile(context)?.let { + tempFiles.add(it) + } + } + return tempFiles + } + + private fun deleteTempFiles(files: List) { + for (file in files) { + try { + if (file.exists()) { + file.delete() + } + } catch (e: SecurityException) { + appLogWrapper.e(T.SUPPORT, "Failed to delete temp file") + } catch (e: IOException) { + appLogWrapper.e(T.SUPPORT, "Failed to delete temp file") + } + } + } + private fun createZendeskFeedbackRequest( context: Context, attachmentTokens: List = emptyList() @@ -226,12 +259,6 @@ class FeedbackFormViewModel @Inject constructor( return false } - val file = uri.copyToTempFile(context) - if (file == null) { - showToast(R.string.feedback_form_unable_to_create_tempfile) - return false - } - val attachmentType = if (mimeType.startsWith("video")) { FeedbackFormAttachmentType.VIDEO } else { @@ -240,7 +267,6 @@ class FeedbackFormViewModel @Inject constructor( list.add( FeedbackFormAttachment( uri = uri, - tempFile = file, size = fileSize, mimeType = mimeType, attachmentType = attachmentType From 0897e276775f2a3bc7a4280a2917f67675d24bff Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 20 Aug 2024 10:01:02 -0400 Subject: [PATCH 2/3] Fixed broken preview --- .../android/ui/main/feedbackform/FeedbackFormScreen.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormScreen.kt b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormScreen.kt index e3262730cf2d..8891f55d3627 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormScreen.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormScreen.kt @@ -44,11 +44,10 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import kotlinx.coroutines.flow.MutableStateFlow import org.wordpress.android.R +import org.wordpress.android.ui.compose.components.MediaUriPager import org.wordpress.android.ui.compose.components.ProgressDialog import org.wordpress.android.ui.compose.components.ProgressDialogState -import org.wordpress.android.ui.compose.components.MediaUriPager import org.wordpress.android.ui.compose.theme.M3Theme -import java.io.File @Composable fun FeedbackFormScreen( @@ -243,14 +242,12 @@ private fun FeedbackFormScreenPreview() { attachmentType = FeedbackFormAttachmentType.IMAGE, size = 123456789, mimeType = "image/jpeg", - tempFile = File("/tmp/attachment.jpg") ) val attachment2 = FeedbackFormAttachment( uri = Uri.parse("https://via.placeholder.com/150"), attachmentType = FeedbackFormAttachmentType.VIDEO, size = 123456789, mimeType = "video/mp4", - tempFile = File("/tmp/attachment.mp4") ) val attachments = MutableStateFlow(listOf(attachment1, attachment2)) val messageText = MutableStateFlow("I love this app!") From 2be3b96cba0bd4d69289133b6b4bae355e9db562 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Tue, 20 Aug 2024 10:17:52 -0400 Subject: [PATCH 3/3] Log failure to create temp file --- .../android/ui/main/feedbackform/FeedbackFormViewModel.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt index 2f8225a802d0..18a9de6cfab7 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/feedbackform/FeedbackFormViewModel.kt @@ -77,7 +77,7 @@ class FeedbackFormViewModel @Inject constructor( if (_attachments.value.isNotEmpty()) { showProgressDialog(R.string.uploading) launch { - val tempFiles = createTempFiles(context) + val tempFiles = createAttachmentTempFiles(context) try { try { val tokens = zendeskUploadHelper.uploadFileAttachments(tempFiles) @@ -104,13 +104,13 @@ class FeedbackFormViewModel @Inject constructor( /** * Creates temporary files for each attachment and returns a list of their paths */ - private fun createTempFiles(context: Context): List { + private fun createAttachmentTempFiles(context: Context): List { val tempFiles = ArrayList() val uris = _attachments.value.map { it.uri } for (uri in uris) { uri.copyToTempFile(context)?.let { tempFiles.add(it) - } + } ?: appLogWrapper.e(T.SUPPORT, "Failed to copy attachment to temp file: $uri") } return tempFiles }