From eea81d7106a4790b4f994dcb0190887671cdb7a6 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Fri, 25 Feb 2022 18:57:18 +0100 Subject: [PATCH] use POST api/v2/media to upload media (#2327) --- .../tusky/components/compose/ComposeViewModel.kt | 2 +- .../tusky/components/compose/MediaUploader.kt | 7 +++---- .../com/keylesspalace/tusky/entity/MediaUploadResult.kt | 9 +++++++++ .../java/com/keylesspalace/tusky/network/MastodonApi.kt | 5 +++-- 4 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/entity/MediaUploadResult.kt diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt index 7391ab1c..2eb5b9fc 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt @@ -185,7 +185,7 @@ class ComposeViewModel @Inject constructor( is UploadEvent.ProgressEvent -> item.copy(uploadPercent = event.percentage) is UploadEvent.FinishedEvent -> - item.copy(id = event.attachment.id, uploadPercent = -1) + item.copy(id = event.mediaId, uploadPercent = -1) } synchronized(media) { val mediaValue = media.value!! 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 44d1e8ed..5966bb53 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 @@ -25,7 +25,6 @@ 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.entity.Attachment import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.ProgressRequestBody import com.keylesspalace.tusky.util.MEDIA_SIZE_UNKNOWN @@ -45,7 +44,7 @@ import javax.inject.Inject sealed class UploadEvent { data class ProgressEvent(val percentage: Int) : UploadEvent() - data class FinishedEvent(val attachment: Attachment) : UploadEvent() + data class FinishedEvent(val mediaId: String) : UploadEvent() } fun createNewImageFile(context: Context): File { @@ -183,8 +182,8 @@ class MediaUploader @Inject constructor( val uploadDisposable = mastodonApi.uploadMedia(body, description) .subscribe( - { attachment -> - emitter.onNext(UploadEvent.FinishedEvent(attachment)) + { result -> + emitter.onNext(UploadEvent.FinishedEvent(result.id)) emitter.onComplete() }, { e -> diff --git a/app/src/main/java/com/keylesspalace/tusky/entity/MediaUploadResult.kt b/app/src/main/java/com/keylesspalace/tusky/entity/MediaUploadResult.kt new file mode 100644 index 00000000..15910f62 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/entity/MediaUploadResult.kt @@ -0,0 +1,9 @@ +package com.keylesspalace.tusky.entity + +/** + * The same as Attachment, except the url is null - see https://docs.joinmastodon.org/methods/statuses/media/ + * We are only interested in the id, so other attributes are omitted + */ +data class MediaUploadResult( + val id: String +) 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 a8b1050c..8804124c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt +++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt @@ -28,6 +28,7 @@ import com.keylesspalace.tusky.entity.IdentityProof import com.keylesspalace.tusky.entity.Instance import com.keylesspalace.tusky.entity.Marker import com.keylesspalace.tusky.entity.MastoList +import com.keylesspalace.tusky.entity.MediaUploadResult import com.keylesspalace.tusky.entity.NewStatus import com.keylesspalace.tusky.entity.Notification import com.keylesspalace.tusky.entity.Poll @@ -142,11 +143,11 @@ interface MastodonApi { fun clearNotifications(): Single @Multipart - @POST("api/v1/media") + @POST("api/v2/media") fun uploadMedia( @Part file: MultipartBody.Part, @Part description: MultipartBody.Part? = null - ): Single + ): Single @FormUrlEncoded @PUT("api/v1/media/{mediaId}")