Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback form temporary files #21133

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -76,26 +77,58 @@ class FeedbackFormViewModel @Inject constructor(
if (_attachments.value.isNotEmpty()) {
showProgressDialog(R.string.uploading)
launch {
val files = _attachments.value.map { it.tempFile }
val tempFiles = createAttachmentTempFiles(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 {
createZendeskFeedbackRequest(context)
}
}

/**
* Creates temporary files for each attachment and returns a list of their paths
*/
private fun createAttachmentTempFiles(context: Context): List<File> {
val tempFiles = ArrayList<File>()
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
}

private fun deleteTempFiles(files: List<File>) {
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<String> = emptyList()
Expand Down Expand Up @@ -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 {
Expand All @@ -240,7 +267,6 @@ class FeedbackFormViewModel @Inject constructor(
list.add(
FeedbackFormAttachment(
uri = uri,
tempFile = file,
size = fileSize,
mimeType = mimeType,
attachmentType = attachmentType
Expand Down
Loading