increase timeout for media uploads (#2489)

This commit is contained in:
Konrad Pozniak 2022-05-01 17:16:22 +02:00 committed by GitHub
parent 5cb6a19375
commit ce5ec15ff1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 10 deletions

View file

@ -26,7 +26,7 @@ import androidx.core.net.toUri
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
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MediaUploadApi
import com.keylesspalace.tusky.network.ProgressRequestBody import com.keylesspalace.tusky.network.ProgressRequestBody
import com.keylesspalace.tusky.util.MEDIA_SIZE_UNKNOWN import com.keylesspalace.tusky.util.MEDIA_SIZE_UNKNOWN
import com.keylesspalace.tusky.util.getImageSquarePixels import com.keylesspalace.tusky.util.getImageSquarePixels
@ -75,7 +75,7 @@ class CouldNotOpenFileException : Exception()
class MediaUploader @Inject constructor( class MediaUploader @Inject constructor(
private val context: Context, private val context: Context,
private val mastodonApi: MastodonApi private val mediaUploadApi: MediaUploadApi
) { ) {
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
@ -222,7 +222,7 @@ class MediaUploader @Inject constructor(
null null
} }
val result = mastodonApi.uploadMedia(body, description).getOrThrow() val result = mediaUploadApi.uploadMedia(body, description).getOrThrow()
send(UploadEvent.FinishedEvent(result.id)) send(UploadEvent.FinishedEvent(result.id))
awaitClose() awaitClose()
} }

View file

@ -24,6 +24,7 @@ import com.keylesspalace.tusky.BuildConfig
import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.network.MediaUploadApi
import com.keylesspalace.tusky.util.getNonNullString import com.keylesspalace.tusky.util.getNonNullString
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
@ -112,4 +113,18 @@ class NetworkModule {
@Provides @Provides
@Singleton @Singleton
fun providesApi(retrofit: Retrofit): MastodonApi = retrofit.create() fun providesApi(retrofit: Retrofit): MastodonApi = retrofit.create()
@Provides
@Singleton
fun providesMediaUploadApi(retrofit: Retrofit, okHttpClient: OkHttpClient): MediaUploadApi {
val longTimeOutOkHttpClient = okHttpClient.newBuilder()
.readTimeout(100, TimeUnit.SECONDS)
.writeTimeout(100, TimeUnit.SECONDS)
.build()
return retrofit.newBuilder()
.client(longTimeOutOkHttpClient)
.build()
.create()
}
} }

View file

@ -142,13 +142,6 @@ interface MastodonApi {
@POST("api/v1/notifications/clear") @POST("api/v1/notifications/clear")
fun clearNotifications(): Single<ResponseBody> fun clearNotifications(): Single<ResponseBody>
@Multipart
@POST("api/v2/media")
suspend fun uploadMedia(
@Part file: MultipartBody.Part,
@Part description: MultipartBody.Part? = null
): Result<MediaUploadResult>
@FormUrlEncoded @FormUrlEncoded
@PUT("api/v1/media/{mediaId}") @PUT("api/v1/media/{mediaId}")
suspend fun updateMedia( suspend fun updateMedia(

View file

@ -0,0 +1,19 @@
package com.keylesspalace.tusky.network
import com.keylesspalace.tusky.entity.MediaUploadResult
import okhttp3.MultipartBody
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
/** endpoints defined in this interface will be called with a higher timeout than usual
* which is necessary for media uploads to succeed on some servers
*/
interface MediaUploadApi {
@Multipart
@POST("api/v2/media")
suspend fun uploadMedia(
@Part file: MultipartBody.Part,
@Part description: MultipartBody.Part? = null
): Result<MediaUploadResult>
}