don't check processing state when upload returned 200

This commit is contained in:
Conny Duck 2022-11-29 21:13:54 +01:00
parent 478dd8c601
commit 5c994ac91f
5 changed files with 23 additions and 16 deletions

View File

@ -1219,7 +1219,8 @@ class ComposeActivity :
val uploadPercent: Int = 0, val uploadPercent: Int = 0,
val id: String? = null, val id: String? = null,
val description: String? = null, val description: String? = null,
val focus: Attachment.Focus? = null val focus: Attachment.Focus? = null,
val processed: Boolean?
) { ) {
enum class Type { enum class Type {
IMAGE, VIDEO, AUDIO; IMAGE, VIDEO, AUDIO;

View File

@ -134,7 +134,8 @@ class ComposeViewModel @Inject constructor(
type = type, type = type,
mediaSize = mediaSize, mediaSize = mediaSize,
description = description, description = description,
focus = focus focus = focus,
processed = null
) )
stashMediaItem = mediaItem stashMediaItem = mediaItem
@ -159,7 +160,7 @@ class ComposeViewModel @Inject constructor(
is UploadEvent.ProgressEvent -> is UploadEvent.ProgressEvent ->
item.copy(uploadPercent = event.percentage) item.copy(uploadPercent = event.percentage)
is UploadEvent.FinishedEvent -> is UploadEvent.FinishedEvent ->
item.copy(id = event.mediaId, uploadPercent = -1) item.copy(id = event.mediaId, uploadPercent = -1, processed = event.processed)
is UploadEvent.ErrorEvent -> { is UploadEvent.ErrorEvent -> {
media.update { mediaValue -> mediaValue.filter { it.localId != mediaItem.localId } } media.update { mediaValue -> mediaValue.filter { it.localId != mediaItem.localId } }
uploadError.emit(event.error) uploadError.emit(event.error)
@ -190,7 +191,8 @@ class ComposeViewModel @Inject constructor(
uploadPercent = -1, uploadPercent = -1,
id = id, id = id,
description = description, description = description,
focus = focus focus = focus,
processed = true
) )
mediaValue + mediaItem mediaValue + mediaItem
} }
@ -294,7 +296,7 @@ class ComposeViewModel @Inject constructor(
uri = item.uri.toString(), uri = item.uri.toString(),
description = item.description, description = item.description,
focus = item.focus, focus = item.focus,
processed = false processed = item.processed == true
) )
} }
val tootToSend = StatusToSend( val tootToSend = StatusToSend(

View File

@ -23,7 +23,6 @@ import android.util.Log
import android.webkit.MimeTypeMap import android.webkit.MimeTypeMap
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.net.toUri import androidx.core.net.toUri
import at.connyduck.calladapter.networkresult.fold
import com.keylesspalace.tusky.BuildConfig import com.keylesspalace.tusky.BuildConfig
import com.keylesspalace.tusky.R import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.components.compose.ComposeActivity.QueuedMedia import com.keylesspalace.tusky.components.compose.ComposeActivity.QueuedMedia
@ -51,6 +50,7 @@ import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.shareIn
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody import okhttp3.MultipartBody
import retrofit2.HttpException
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
@ -63,7 +63,7 @@ sealed interface FinalUploadEvent
sealed class UploadEvent { sealed class UploadEvent {
data class ProgressEvent(val percentage: Int) : UploadEvent() data class ProgressEvent(val percentage: Int) : UploadEvent()
data class FinishedEvent(val mediaId: String) : UploadEvent(), FinalUploadEvent data class FinishedEvent(val mediaId: String, val processed: Boolean) : UploadEvent(), FinalUploadEvent
data class ErrorEvent(val error: Throwable) : UploadEvent(), FinalUploadEvent data class ErrorEvent(val error: Throwable) : UploadEvent(), FinalUploadEvent
} }
@ -286,16 +286,20 @@ class MediaUploader @Inject constructor(
null null
} }
mediaUploadApi.uploadMedia(body, description, focus).fold({ result -> val uploadResponse = mediaUploadApi.uploadMedia(body, description, focus)
send(UploadEvent.FinishedEvent(result.id)) val responseBody = uploadResponse.body()
}, { throwable -> if (uploadResponse.isSuccessful && responseBody != null) {
val errorMessage = throwable.getServerErrorMessage() send(UploadEvent.FinishedEvent(responseBody.id, uploadResponse.code() == 200))
} else {
val error = HttpException(uploadResponse)
val errorMessage = error.getServerErrorMessage()
if (errorMessage == null) { if (errorMessage == null) {
throw throwable throw error
} else { } else {
throw UploadServerError(errorMessage) throw UploadServerError(errorMessage)
} }
}) }
awaitClose() awaitClose()
} }
} }

View File

@ -1,8 +1,8 @@
package com.keylesspalace.tusky.network package com.keylesspalace.tusky.network
import at.connyduck.calladapter.networkresult.NetworkResult
import com.keylesspalace.tusky.entity.MediaUploadResult import com.keylesspalace.tusky.entity.MediaUploadResult
import okhttp3.MultipartBody import okhttp3.MultipartBody
import retrofit2.Response
import retrofit2.http.Multipart import retrofit2.http.Multipart
import retrofit2.http.POST import retrofit2.http.POST
import retrofit2.http.Part import retrofit2.http.Part
@ -17,5 +17,5 @@ interface MediaUploadApi {
@Part file: MultipartBody.Part, @Part file: MultipartBody.Part,
@Part description: MultipartBody.Part? = null, @Part description: MultipartBody.Part? = null,
@Part focus: MultipartBody.Part? = null @Part focus: MultipartBody.Part? = null
): NetworkResult<MediaUploadResult> ): Response<MediaUploadResult>
} }

View File

@ -140,7 +140,7 @@ class SendStatusService : Service(), Injectable {
val media = statusToSend.media.map { mediaItem -> val media = statusToSend.media.map { mediaItem ->
if (mediaItem.id == null) { if (mediaItem.id == null) {
when (val uploadState = mediaUploader.getMediaUploadState(mediaItem.localId)) { when (val uploadState = mediaUploader.getMediaUploadState(mediaItem.localId)) {
is UploadEvent.FinishedEvent -> mediaItem.copy(id = uploadState.mediaId) is UploadEvent.FinishedEvent -> mediaItem.copy(id = uploadState.mediaId, processed = uploadState.processed)
is UploadEvent.ErrorEvent -> { is UploadEvent.ErrorEvent -> {
Log.w(TAG, "failed uploading media", uploadState.error) Log.w(TAG, "failed uploading media", uploadState.error)
failSending(statusId) failSending(statusId)