use POST api/v2/media to upload media (#2327)

This commit is contained in:
Konrad Pozniak 2022-02-25 18:57:18 +01:00 committed by GitHub
parent 8dbf255617
commit eea81d7106
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 7 deletions

View file

@ -185,7 +185,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.attachment.id, uploadPercent = -1) item.copy(id = event.mediaId, uploadPercent = -1)
} }
synchronized(media) { synchronized(media) {
val mediaValue = media.value!! val mediaValue = media.value!!

View file

@ -25,7 +25,6 @@ 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.entity.Attachment
import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.network.MastodonApi
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
@ -45,7 +44,7 @@ import javax.inject.Inject
sealed class UploadEvent { sealed class UploadEvent {
data class ProgressEvent(val percentage: Int) : 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 { fun createNewImageFile(context: Context): File {
@ -183,8 +182,8 @@ class MediaUploader @Inject constructor(
val uploadDisposable = mastodonApi.uploadMedia(body, description) val uploadDisposable = mastodonApi.uploadMedia(body, description)
.subscribe( .subscribe(
{ attachment -> { result ->
emitter.onNext(UploadEvent.FinishedEvent(attachment)) emitter.onNext(UploadEvent.FinishedEvent(result.id))
emitter.onComplete() emitter.onComplete()
}, },
{ e -> { e ->

View file

@ -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
)

View file

@ -28,6 +28,7 @@ import com.keylesspalace.tusky.entity.IdentityProof
import com.keylesspalace.tusky.entity.Instance import com.keylesspalace.tusky.entity.Instance
import com.keylesspalace.tusky.entity.Marker import com.keylesspalace.tusky.entity.Marker
import com.keylesspalace.tusky.entity.MastoList import com.keylesspalace.tusky.entity.MastoList
import com.keylesspalace.tusky.entity.MediaUploadResult
import com.keylesspalace.tusky.entity.NewStatus import com.keylesspalace.tusky.entity.NewStatus
import com.keylesspalace.tusky.entity.Notification import com.keylesspalace.tusky.entity.Notification
import com.keylesspalace.tusky.entity.Poll import com.keylesspalace.tusky.entity.Poll
@ -142,11 +143,11 @@ interface MastodonApi {
fun clearNotifications(): Single<ResponseBody> fun clearNotifications(): Single<ResponseBody>
@Multipart @Multipart
@POST("api/v1/media") @POST("api/v2/media")
fun uploadMedia( fun uploadMedia(
@Part file: MultipartBody.Part, @Part file: MultipartBody.Part,
@Part description: MultipartBody.Part? = null @Part description: MultipartBody.Part? = null
): Single<Attachment> ): Single<MediaUploadResult>
@FormUrlEncoded @FormUrlEncoded
@PUT("api/v1/media/{mediaId}") @PUT("api/v1/media/{mediaId}")