2021-06-23 06:14:25 +02:00
|
|
|
package jp.juggler.subwaytooter.util
|
|
|
|
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.SystemClock
|
|
|
|
import androidx.annotation.WorkerThread
|
2023-04-28 11:35:13 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2021-06-23 06:14:25 +02:00
|
|
|
import jp.juggler.subwaytooter.R
|
|
|
|
import jp.juggler.subwaytooter.api.TootApiCallback
|
|
|
|
import jp.juggler.subwaytooter.api.TootApiClient
|
|
|
|
import jp.juggler.subwaytooter.api.TootApiResult
|
2023-01-17 13:42:47 +01:00
|
|
|
import jp.juggler.subwaytooter.api.auth.AuthBase
|
2023-05-14 07:29:23 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.EntityId
|
2023-07-19 05:31:16 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.InstanceType
|
2023-05-14 07:29:23 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.ServiceType
|
|
|
|
import jp.juggler.subwaytooter.api.entity.TootAttachment
|
2023-02-07 13:49:45 +01:00
|
|
|
import jp.juggler.subwaytooter.api.entity.TootAttachment.Companion.tootAttachment
|
2023-05-14 07:29:23 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.TootInstance
|
|
|
|
import jp.juggler.subwaytooter.api.entity.parseItem
|
2021-06-23 06:14:25 +02:00
|
|
|
import jp.juggler.subwaytooter.api.runApiTask
|
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.coroutine.AppDispatchers
|
|
|
|
import jp.juggler.util.coroutine.launchIO
|
2023-05-14 07:29:23 +02:00
|
|
|
import jp.juggler.util.data.GetContentResultEntry
|
|
|
|
import jp.juggler.util.data.asciiPattern
|
|
|
|
import jp.juggler.util.data.buildJsonObject
|
|
|
|
import jp.juggler.util.data.encodeHex
|
|
|
|
import jp.juggler.util.data.encodeUTF8
|
|
|
|
import jp.juggler.util.data.getDocumentName
|
|
|
|
import jp.juggler.util.data.groupEx
|
|
|
|
import jp.juggler.util.log.LogCategory
|
|
|
|
import jp.juggler.util.log.showToast
|
|
|
|
import jp.juggler.util.log.withCaption
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.media.ResizeConfig
|
|
|
|
import jp.juggler.util.media.ResizeType
|
|
|
|
import jp.juggler.util.network.toPost
|
|
|
|
import jp.juggler.util.network.toPostRequestBuilder
|
|
|
|
import jp.juggler.util.network.toPut
|
|
|
|
import jp.juggler.util.network.toPutRequestBuilder
|
2023-07-19 05:31:16 +02:00
|
|
|
import kotlinx.coroutines.CancellationException
|
2022-01-05 05:22:44 +01:00
|
|
|
import kotlinx.coroutines.channels.Channel
|
|
|
|
import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
2021-06-23 06:14:25 +02:00
|
|
|
import kotlinx.coroutines.delay
|
2022-01-05 05:22:44 +01:00
|
|
|
import kotlinx.coroutines.isActive
|
|
|
|
import kotlinx.coroutines.withContext
|
2021-06-23 06:14:25 +02:00
|
|
|
import okhttp3.MultipartBody
|
2023-07-19 05:31:16 +02:00
|
|
|
import java.nio.channels.ClosedChannelException
|
2022-01-05 05:22:44 +01:00
|
|
|
import kotlin.coroutines.coroutineContext
|
2021-06-23 06:14:25 +02:00
|
|
|
|
|
|
|
class AttachmentUploader(
|
2023-04-28 11:35:13 +02:00
|
|
|
activity: AppCompatActivity,
|
2021-06-23 06:14:25 +02:00
|
|
|
private val handler: Handler,
|
|
|
|
) {
|
|
|
|
companion object {
|
|
|
|
val log = LogCategory("AttachmentUploader")
|
|
|
|
}
|
|
|
|
|
2023-04-28 11:35:13 +02:00
|
|
|
private val safeContext = activity.applicationContext!!
|
2021-06-23 06:14:25 +02:00
|
|
|
private var lastAttachmentAdd = 0L
|
|
|
|
private var lastAttachmentComplete = 0L
|
2023-07-19 05:31:16 +02:00
|
|
|
private val channel = Channel<AttachmentRequest>(capacity = Channel.UNLIMITED)
|
|
|
|
|
|
|
|
init {
|
|
|
|
launchIO {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
val request = channel.receive()
|
|
|
|
if (request.pa.isCancelled) continue
|
|
|
|
withContext(AppDispatchers.MainImmediate) {
|
|
|
|
val pa = request.pa
|
|
|
|
pa.status = try {
|
|
|
|
withContext(pa.job + AppDispatchers.IO) {
|
|
|
|
request.upload()
|
2022-01-05 10:39:48 +01:00
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
request.pa.progress = ""
|
|
|
|
|
|
|
|
val now = System.currentTimeMillis()
|
|
|
|
if (now - lastAttachmentComplete >= 5000L) {
|
|
|
|
safeContext.showToast(false, R.string.attachment_uploaded)
|
2022-01-05 10:39:48 +01:00
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
lastAttachmentComplete = now
|
|
|
|
|
|
|
|
PostAttachment.Status.Ok
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
if (ex is CancellationException) {
|
|
|
|
// キャンセルはメッセージを出さない
|
|
|
|
} else if (ex.message?.contains("cancel", ignoreCase = true) == true) {
|
|
|
|
// キャンセルはメッセージを出さない
|
|
|
|
} else if (ex is IllegalStateException) {
|
|
|
|
safeContext.showToast(true, "${ex.message}")
|
|
|
|
} else {
|
|
|
|
safeContext.showToast(true, ex.withCaption("upload failed."))
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
PostAttachment.Status.Error
|
|
|
|
}
|
|
|
|
// 投稿中に画面回転があった場合、新しい画面のコールバックを呼び出す必要がある
|
|
|
|
pa.callback?.onPostAttachmentComplete(pa)
|
|
|
|
}
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
when (ex) {
|
|
|
|
is CancellationException -> {
|
|
|
|
log.i("AttachmentUploader: channel cancelled.")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
is ClosedChannelException, is ClosedReceiveChannelException -> {
|
|
|
|
log.i("AttachmentUploader: channel closed.")
|
|
|
|
break
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
|
|
|
|
else -> safeContext.showToast(ex)
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
}
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
|
|
|
fun onActivityDestroy() {
|
2022-01-05 05:22:44 +01:00
|
|
|
try {
|
2023-07-19 05:31:16 +02:00
|
|
|
channel.close()
|
|
|
|
} catch (ignored: Throwable) {
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
fun addRequest(request: AttachmentRequest) {
|
2023-04-28 11:35:13 +02:00
|
|
|
request.pa.progress = safeContext.getString(R.string.attachment_handling_start)
|
2022-01-05 10:39:48 +01:00
|
|
|
|
2021-06-23 06:14:25 +02:00
|
|
|
// マストドンは添付メディアをID順に表示するため
|
|
|
|
// 画像が複数ある場合は一つずつ処理する必要がある
|
2023-07-19 05:31:16 +02:00
|
|
|
// 投稿画面ごとに作成したチャネルにsendして、受け側は順次処理する
|
|
|
|
launchIO {
|
|
|
|
try {
|
|
|
|
val now = System.currentTimeMillis()
|
|
|
|
|
|
|
|
// アップロード開始トースト(連発しない)
|
|
|
|
if (now - lastAttachmentAdd >= 5000L) {
|
|
|
|
safeContext.showToast(false, R.string.attachment_uploading)
|
|
|
|
}
|
|
|
|
|
|
|
|
lastAttachmentAdd = now
|
|
|
|
channel.send(request)
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
log.e(ex, "addRequest failed.")
|
|
|
|
}
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
@WorkerThread
|
2023-07-19 05:31:16 +02:00
|
|
|
private suspend fun AttachmentRequest.upload() {
|
|
|
|
val account = this.account
|
|
|
|
val instance = this.instance()
|
|
|
|
val mediaConfig = this.mediaConfig()
|
|
|
|
|
|
|
|
// ensure mimeType
|
|
|
|
this.mimeType
|
2022-01-05 10:39:48 +01:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
if (instance.instanceType == InstanceType.Pixelfed && isReply) {
|
|
|
|
error(safeContext.getString(R.string.pixelfed_does_not_allow_reply_with_media))
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
val client = TootApiClient(safeContext, callback = object : TootApiCallback {
|
|
|
|
override suspend fun isApiCancelled() = !coroutineContext.isActive
|
|
|
|
})
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
client.account = account
|
|
|
|
client.currentCallCallback = {}
|
2022-01-05 05:22:44 +01:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
// 入力データの変換など
|
|
|
|
val opener = this.createOpener()
|
|
|
|
try {
|
2023-04-28 11:35:13 +02:00
|
|
|
val maxBytes = when (opener.isImage) {
|
2023-07-19 05:31:16 +02:00
|
|
|
true -> maxBytesImage(instance, mediaConfig)
|
|
|
|
else -> maxBytesVideo(instance, mediaConfig)
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
if (opener.contentLength > maxBytes.toLong()) {
|
|
|
|
error(safeContext.getString(R.string.file_size_too_big, maxBytes / 1_000_000))
|
|
|
|
} else if (!opener.mimeType.mimeTypeIsSupported(instance)) {
|
|
|
|
error(safeContext.getString(R.string.mime_type_not_acceptable, opener.mimeType))
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-20 07:03:28 +02:00
|
|
|
val fileName = fixDocumentName(
|
|
|
|
getDocumentName(safeContext.contentResolver, uri),
|
|
|
|
fixExt = opener.fixExt,
|
|
|
|
)
|
2023-04-28 11:35:13 +02:00
|
|
|
pa.progress = safeContext.getString(R.string.attachment_handling_uploading, 0)
|
2022-01-05 23:29:05 +01:00
|
|
|
fun writeProgress(percent: Int) {
|
2023-07-19 05:31:16 +02:00
|
|
|
pa.progress = if (percent < 100) {
|
|
|
|
safeContext.getString(R.string.attachment_handling_uploading, percent)
|
2022-01-05 23:29:05 +01:00
|
|
|
} else {
|
2023-07-19 05:31:16 +02:00
|
|
|
safeContext.getString(R.string.attachment_handling_waiting)
|
2022-01-05 10:39:48 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
if (account.isMisskey) {
|
2022-01-05 05:22:44 +01:00
|
|
|
val multipartBuilder = MultipartBody.Builder()
|
|
|
|
.setType(MultipartBody.FORM)
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-02-06 03:10:24 +01:00
|
|
|
val apiKey = account.tokenJson?.string(AuthBase.KEY_API_KEY_MISSKEY)
|
2022-01-05 05:22:44 +01:00
|
|
|
if (apiKey?.isNotEmpty() == true) {
|
|
|
|
multipartBuilder.addFormDataPart("i", apiKey)
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
multipartBuilder.addFormDataPart(
|
|
|
|
"file",
|
|
|
|
fileName,
|
2022-01-05 23:29:05 +01:00
|
|
|
opener.toRequestBody { writeProgress(it) },
|
2022-01-05 05:22:44 +01:00
|
|
|
)
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
val result = client.request(
|
|
|
|
"/api/drive/files/create",
|
|
|
|
multipartBuilder.build().toPost()
|
|
|
|
)
|
|
|
|
opener.deleteTempFile()
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
result ?: throw CancellationException()
|
|
|
|
|
|
|
|
val jsonObject = result.jsonObject
|
|
|
|
?: error(result.error ?: "missing error detail")
|
|
|
|
pa.attachment =
|
|
|
|
parseItem(jsonObject) { tootAttachment(ServiceType.MISSKEY, it) }
|
|
|
|
?: error("TootAttachment.parse failed")
|
2022-01-05 05:22:44 +01:00
|
|
|
} else {
|
|
|
|
suspend fun postMedia(path: String) = client.request(
|
|
|
|
path,
|
|
|
|
MultipartBody.Builder()
|
|
|
|
.setType(MultipartBody.FORM)
|
|
|
|
.addFormDataPart(
|
|
|
|
"file",
|
|
|
|
fileName,
|
2022-01-05 23:29:05 +01:00
|
|
|
opener.toRequestBody { writeProgress(it) },
|
2022-01-05 05:22:44 +01:00
|
|
|
)
|
|
|
|
.build().toPost()
|
|
|
|
)
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
suspend fun postV1() = postMedia("/api/v1/media")
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
suspend fun postV2(): TootApiResult? {
|
|
|
|
// 3.1.3未満は v1 APIを使う
|
2023-07-19 05:31:16 +02:00
|
|
|
if (!instance.versionGE(TootInstance.VERSION_3_1_3)) {
|
2022-01-05 05:22:44 +01:00
|
|
|
return postV1()
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
// v2 APIを試す
|
|
|
|
val result = postMedia("/api/v2/media")
|
2023-07-19 05:31:16 +02:00
|
|
|
?: throw CancellationException()
|
|
|
|
val code = result.response?.code // complete,or 4xx error
|
2022-01-05 05:22:44 +01:00
|
|
|
when {
|
|
|
|
// 404ならv1 APIにフォールバック
|
|
|
|
code == 404 -> return postV1()
|
|
|
|
// 202 accepted 以外はポーリングしない
|
|
|
|
code != 202 -> return result
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
// ポーリングして処理完了を待つ
|
2023-04-28 11:35:13 +02:00
|
|
|
pa.progress = safeContext.getString(R.string.attachment_handling_waiting_async)
|
2023-07-19 05:31:16 +02:00
|
|
|
|
|
|
|
val id = parseItem(result.jsonObject) {
|
2023-02-07 13:49:45 +01:00
|
|
|
tootAttachment(ServiceType.MASTODON, it)
|
2023-07-19 05:31:16 +02:00
|
|
|
}?.id ?: error("/api/v2/media did not return the media ID.")
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
var lastResponse = SystemClock.elapsedRealtime()
|
2023-07-19 05:31:16 +02:00
|
|
|
while (true) {
|
2022-01-05 05:22:44 +01:00
|
|
|
delay(1000L)
|
2022-01-05 23:29:05 +01:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
val r2 = client.request("/api/v1/media/$id")
|
2023-07-19 05:31:16 +02:00
|
|
|
?: throw CancellationException()
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
val now = SystemClock.elapsedRealtime()
|
|
|
|
when (r2.response?.code) {
|
|
|
|
// complete,or 4xx error
|
|
|
|
200, in 400 until 500 -> return r2
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
// continue to wait
|
|
|
|
206 -> lastResponse = now
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 23:29:05 +01:00
|
|
|
// temporary errors, check timeout without 206 response.
|
2023-07-19 05:31:16 +02:00
|
|
|
else -> if (now - lastResponse >= 120000L) error("timeout.")
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-05 05:22:44 +01:00
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2022-01-05 05:22:44 +01:00
|
|
|
val result = postV2()
|
2023-07-19 05:31:16 +02:00
|
|
|
?: throw CancellationException()
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
val jsonObject = result.jsonObject
|
|
|
|
?: error(result.error ?: "missing error detail")
|
|
|
|
|
|
|
|
pa.attachment = parseItem(jsonObject) { tootAttachment(ServiceType.MASTODON, it) }
|
|
|
|
?: error("TootAttachment.parse failed")
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
} finally {
|
|
|
|
opener.deleteTempFile()
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-20 07:03:28 +02:00
|
|
|
private fun fixDocumentName(s: String, fixExt: String?): String {
|
2023-04-28 11:35:13 +02:00
|
|
|
val sLength = s.length
|
|
|
|
val m = """([^\x20-\x7f])""".asciiPattern().matcher(s)
|
|
|
|
m.reset()
|
|
|
|
val sb = StringBuilder(sLength)
|
|
|
|
var lastEnd = 0
|
|
|
|
while (m.find()) {
|
|
|
|
sb.append(s.substring(lastEnd, m.start()))
|
|
|
|
val escaped = m.groupEx(1)!!.encodeUTF8().encodeHex()
|
|
|
|
sb.append(escaped)
|
|
|
|
lastEnd = m.end()
|
|
|
|
}
|
|
|
|
if (lastEnd < sLength) sb.append(s.substring(lastEnd, sLength))
|
2023-07-20 07:03:28 +02:00
|
|
|
var escaped = sb.toString()
|
|
|
|
if (!fixExt.isNullOrEmpty()) {
|
|
|
|
escaped = """\.[^./\\]*\z""".toRegex().replace(escaped, "") + ".${fixExt}"
|
|
|
|
}
|
|
|
|
return escaped
|
2023-04-28 11:35:13 +02:00
|
|
|
}
|
|
|
|
|
2021-06-23 06:14:25 +02:00
|
|
|
///////////////////////////////////////////////////////////////
|
2023-05-14 07:29:23 +02:00
|
|
|
// 添付データのカスタムサムネイル
|
2021-06-23 06:14:25 +02:00
|
|
|
suspend fun uploadCustomThumbnail(
|
|
|
|
account: SavedAccount,
|
|
|
|
src: GetContentResultEntry,
|
|
|
|
pa: PostAttachment,
|
|
|
|
): TootApiResult? = try {
|
2023-04-28 11:35:13 +02:00
|
|
|
safeContext.runApiTask(account) { client ->
|
|
|
|
val ar = AttachmentRequest(
|
2023-05-14 07:29:23 +02:00
|
|
|
context = safeContext,
|
2023-04-28 11:35:13 +02:00
|
|
|
account = account,
|
|
|
|
pa = pa,
|
|
|
|
uri = src.uri,
|
2023-07-19 05:31:16 +02:00
|
|
|
mimeTypeArg = src.mimeType,
|
2022-03-15 06:21:11 +01:00
|
|
|
imageResizeConfig = ResizeConfig(ResizeType.SquarePixel, 400),
|
2023-07-19 05:31:16 +02:00
|
|
|
maxBytesImage = { _, _ -> 1000000 },
|
|
|
|
maxBytesVideo = { _, _ -> 1000000 },
|
2022-01-05 23:29:05 +01:00
|
|
|
)
|
2023-07-19 05:31:16 +02:00
|
|
|
val instance = ar.instance()
|
|
|
|
val mediaConfig = ar.mediaConfig()
|
|
|
|
val maxBytesImage = ar.maxBytesImage(instance, mediaConfig)
|
|
|
|
|
2023-04-28 11:35:13 +02:00
|
|
|
val opener = ar.createOpener()
|
2023-07-20 04:40:12 +02:00
|
|
|
try {
|
2023-07-19 05:31:16 +02:00
|
|
|
|
|
|
|
if (opener.contentLength > maxBytesImage.toLong()) {
|
|
|
|
return@runApiTask TootApiResult(
|
|
|
|
getString(
|
|
|
|
R.string.file_size_too_big,
|
|
|
|
maxBytesImage / 1000000
|
|
|
|
)
|
2023-05-14 07:29:23 +02:00
|
|
|
)
|
2023-07-19 05:31:16 +02:00
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-20 07:03:28 +02:00
|
|
|
val fileName = fixDocumentName(
|
|
|
|
getDocumentName(safeContext.contentResolver, src.uri),
|
|
|
|
fixExt = opener.fixExt,
|
|
|
|
)
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
if (account.isMisskey) {
|
|
|
|
TootApiResult("custom thumbnail is not supported on misskey account.")
|
|
|
|
} else {
|
|
|
|
val result = client.request(
|
|
|
|
"/api/v1/media/${pa.attachment?.id}",
|
|
|
|
MultipartBody.Builder()
|
|
|
|
.setType(MultipartBody.FORM)
|
|
|
|
.addFormDataPart(
|
|
|
|
"thumbnail",
|
|
|
|
fileName,
|
|
|
|
opener.toRequestBody(),
|
|
|
|
)
|
|
|
|
.build().toPut()
|
|
|
|
)
|
2021-06-23 06:14:25 +02:00
|
|
|
|
2023-07-19 05:31:16 +02:00
|
|
|
val jsonObject = result?.jsonObject
|
|
|
|
if (jsonObject != null) {
|
|
|
|
val a = parseItem(jsonObject) { tootAttachment(ServiceType.MASTODON, it) }
|
|
|
|
if (a == null) {
|
|
|
|
result.error = "TootAttachment.parse failed"
|
|
|
|
} else {
|
|
|
|
pa.attachment = a
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
2023-07-19 05:31:16 +02:00
|
|
|
result
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
2023-07-20 04:40:12 +02:00
|
|
|
} finally {
|
2023-07-19 05:31:16 +02:00
|
|
|
opener.deleteTempFile()
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
TootApiResult(ex.withCaption("uploadCustomThumbnail failed."))
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun setAttachmentDescription(
|
|
|
|
account: SavedAccount,
|
|
|
|
attachmentId: EntityId,
|
|
|
|
description: String,
|
|
|
|
): Pair<TootApiResult?, TootAttachment?> {
|
|
|
|
var resultAttachment: TootAttachment? = null
|
|
|
|
val result = try {
|
2023-04-28 11:35:13 +02:00
|
|
|
safeContext.runApiTask(account) { client ->
|
2022-06-04 20:35:07 +02:00
|
|
|
if (account.isMisskey) {
|
2022-05-30 14:32:12 +02:00
|
|
|
client.request(
|
|
|
|
"/api/drive/files/update",
|
|
|
|
account.putMisskeyApiToken().apply {
|
|
|
|
put("fileId", attachmentId.toString())
|
2022-06-04 20:35:07 +02:00
|
|
|
put("comment", description)
|
2022-05-30 14:32:12 +02:00
|
|
|
}.toPostRequestBuilder()
|
|
|
|
)?.also { result ->
|
2023-02-07 13:49:45 +01:00
|
|
|
resultAttachment = parseItem(result.jsonObject) {
|
|
|
|
tootAttachment(ServiceType.MISSKEY, it)
|
|
|
|
}
|
2022-05-30 14:32:12 +02:00
|
|
|
}
|
2022-06-04 20:35:07 +02:00
|
|
|
} else {
|
2022-05-30 14:32:12 +02:00
|
|
|
client.request(
|
|
|
|
"/api/v1/media/$attachmentId",
|
2023-01-13 13:22:25 +01:00
|
|
|
buildJsonObject {
|
2022-05-30 14:32:12 +02:00
|
|
|
put("description", description)
|
2023-01-13 13:22:25 +01:00
|
|
|
}.toPutRequestBuilder()
|
2022-05-30 14:32:12 +02:00
|
|
|
)?.also { result ->
|
2023-02-07 13:49:45 +01:00
|
|
|
resultAttachment = parseItem(result.jsonObject) {
|
|
|
|
tootAttachment(ServiceType.MASTODON, it)
|
|
|
|
}
|
2021-06-23 06:14:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (ex: Throwable) {
|
2022-12-27 03:54:52 +01:00
|
|
|
log.e(ex, "setAttachmentDescription failed.")
|
2021-06-23 06:14:25 +02:00
|
|
|
TootApiResult(ex.withCaption("setAttachmentDescription failed."))
|
|
|
|
}
|
|
|
|
return Pair(result, resultAttachment)
|
|
|
|
}
|
|
|
|
}
|