From ce5ec15ff13ba0b36b486edd192dbb6c103a2f6c Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sun, 1 May 2022 17:16:22 +0200 Subject: [PATCH] increase timeout for media uploads (#2489) --- .../tusky/components/compose/MediaUploader.kt | 6 +++--- .../keylesspalace/tusky/di/NetworkModule.kt | 15 +++++++++++++++ .../tusky/network/MastodonApi.kt | 7 ------- .../tusky/network/MediaUploadApi.kt | 19 +++++++++++++++++++ 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/network/MediaUploadApi.kt diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt index 85be146c..f1debc98 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt @@ -26,7 +26,7 @@ import androidx.core.net.toUri import com.keylesspalace.tusky.BuildConfig import com.keylesspalace.tusky.R 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.util.MEDIA_SIZE_UNKNOWN import com.keylesspalace.tusky.util.getImageSquarePixels @@ -75,7 +75,7 @@ class CouldNotOpenFileException : Exception() class MediaUploader @Inject constructor( private val context: Context, - private val mastodonApi: MastodonApi + private val mediaUploadApi: MediaUploadApi ) { @OptIn(ExperimentalCoroutinesApi::class) @@ -222,7 +222,7 @@ class MediaUploader @Inject constructor( null } - val result = mastodonApi.uploadMedia(body, description).getOrThrow() + val result = mediaUploadApi.uploadMedia(body, description).getOrThrow() send(UploadEvent.FinishedEvent(result.id)) awaitClose() } diff --git a/app/src/main/java/com/keylesspalace/tusky/di/NetworkModule.kt b/app/src/main/java/com/keylesspalace/tusky/di/NetworkModule.kt index d8b52ca3..90dd3026 100644 --- a/app/src/main/java/com/keylesspalace/tusky/di/NetworkModule.kt +++ b/app/src/main/java/com/keylesspalace/tusky/di/NetworkModule.kt @@ -24,6 +24,7 @@ import com.keylesspalace.tusky.BuildConfig import com.keylesspalace.tusky.db.AccountManager import com.keylesspalace.tusky.network.InstanceSwitchAuthInterceptor import com.keylesspalace.tusky.network.MastodonApi +import com.keylesspalace.tusky.network.MediaUploadApi import com.keylesspalace.tusky.util.getNonNullString import dagger.Module import dagger.Provides @@ -112,4 +113,18 @@ class NetworkModule { @Provides @Singleton 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() + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt index 25fe03d4..7357293b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt +++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt @@ -142,13 +142,6 @@ interface MastodonApi { @POST("api/v1/notifications/clear") fun clearNotifications(): Single - @Multipart - @POST("api/v2/media") - suspend fun uploadMedia( - @Part file: MultipartBody.Part, - @Part description: MultipartBody.Part? = null - ): Result - @FormUrlEncoded @PUT("api/v1/media/{mediaId}") suspend fun updateMedia( diff --git a/app/src/main/java/com/keylesspalace/tusky/network/MediaUploadApi.kt b/app/src/main/java/com/keylesspalace/tusky/network/MediaUploadApi.kt new file mode 100644 index 00000000..c7e9633f --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/network/MediaUploadApi.kt @@ -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 +}