Skip to content

Commit

Permalink
Step46:画像をアップロードする
Browse files Browse the repository at this point in the history
  • Loading branch information
keiji committed Apr 16, 2020
1 parent 7bd6249 commit 5acf340
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package io.keiji.sample.mastodonclient

import io.keiji.sample.mastodonclient.entity.Account
import io.keiji.sample.mastodonclient.entity.Media
import io.keiji.sample.mastodonclient.entity.ResponseToken
import io.keiji.sample.mastodonclient.entity.Toot
import okhttp3.MultipartBody
import retrofit2.http.DELETE
import retrofit2.http.Field
import retrofit2.http.FormUrlEncoded
import retrofit2.http.GET
import retrofit2.http.Header
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part
import retrofit2.http.Path
import retrofit2.http.Query

Expand Down Expand Up @@ -36,9 +40,17 @@ interface MastodonApi {
@POST("api/v1/statuses")
suspend fun postToot(
@Header("Authorization") accessToken: String,
@Field("status") status: String
@Field("status") status: String,
@Field("media_ids[]") mediaIds: List<String>? = null
): Toot

@Multipart
@POST("api/v1/media")
suspend fun postMedia(
@Header("Authorization") accessToken: String,
@Part file: MultipartBody.Part
): Media

@DELETE("api/v1/statuses/{id}")
suspend fun deleteToot(
@Header("Authorization") accessToken: String,
Expand All @@ -55,4 +67,5 @@ interface MastodonApi {
@Field("code") code: String,
@Field("grant_type") grantType: String
): ResponseToken

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ package io.keiji.sample.mastodonclient.repository
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import io.keiji.sample.mastodonclient.MastodonApi
import io.keiji.sample.mastodonclient.entity.Media
import io.keiji.sample.mastodonclient.entity.Toot
import io.keiji.sample.mastodonclient.entity.UserCredential
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import java.io.File

class TootRepository(
private val userCredential: UserCredential
Expand Down Expand Up @@ -43,11 +48,30 @@ class TootRepository(
}

suspend fun postToot(
status: String
status: String,
mediaIds: List<String>? = null
): Toot = withContext(Dispatchers.IO) {
return@withContext api.postToot(
"Bearer ${userCredential.accessToken}",
status
status,
mediaIds
)
}

suspend fun postMedia(
file: File,
mediaType: String
): Media = withContext(Dispatchers.IO) {

val part = MultipartBody.Part.createFormData(
"file",
file.name,
RequestBody.create(MediaType.parse(mediaType), file)
)

return@withContext api.postMedia(
"Bearer ${userCredential.accessToken}",
part
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ class TootEditViewModel(

val tootRepository = TootRepository(credential)
try {
val uploadedMediaIds = mediaAttachments.value?.map {
tootRepository.postMedia(it.file, it.mediaType)
}?.map { it.id }

tootRepository.postToot(
statusSnapshot
statusSnapshot,
uploadedMediaIds
)
postComplete.postValue(true)
} catch (e: HttpException) {
Expand All @@ -60,6 +65,10 @@ class TootEditViewModel(
errorMessage.postValue("必要な権限がありません")
}
}
} catch (e: IOException) {
errorMessage.postValue(
"サーバーに接続できませんでした。${e.message}"
)
}
}
}
Expand Down

0 comments on commit 5acf340

Please sign in to comment.