fix uploading media
This commit is contained in:
parent
a77ed67fd9
commit
a96abd1343
|
@ -13,6 +13,7 @@ import androidx.core.app.NotificationCompat
|
|||
import androidx.core.app.ServiceCompat
|
||||
import at.connyduck.pixelcat.R
|
||||
import at.connyduck.pixelcat.components.util.getColorForAttr
|
||||
import at.connyduck.pixelcat.components.util.getMimeType
|
||||
import at.connyduck.pixelcat.db.AccountManager
|
||||
import at.connyduck.pixelcat.model.NewStatus
|
||||
import at.connyduck.pixelcat.network.FediverseApi
|
||||
|
@ -113,25 +114,29 @@ class SendStatusService : DaggerService(), CoroutineScope {
|
|||
|
||||
launch {
|
||||
|
||||
val mediaIds = statusToSend.mediaUris.map {
|
||||
val mediaIds = statusToSend.mediaUris.map { mediaPath ->
|
||||
|
||||
var type: String? = null
|
||||
val extension = MimeTypeMap.getFileExtensionFromUrl(it)
|
||||
if (extension != null) {
|
||||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
||||
val mimeType = getMimeType(mediaPath)
|
||||
|
||||
if (mimeType == null) {
|
||||
// unrecoverable error
|
||||
onError(UnrecoverableError(), id)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val file = File(it)
|
||||
val filePart = file.asRequestBody(type!!.toMediaType())
|
||||
val fileExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType)
|
||||
|
||||
val body = MultipartBody.Part.create(filePart)
|
||||
val filePart = File(mediaPath).asRequestBody(mimeType.toMediaType())
|
||||
|
||||
val body = MultipartBody.Part.createFormData("file", "test.$fileExtension", filePart)
|
||||
|
||||
api.uploadMedia(body).fold(
|
||||
{ attachment ->
|
||||
attachment.id
|
||||
},
|
||||
{
|
||||
""
|
||||
{ error ->
|
||||
onError(error, id)
|
||||
return@launch
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -152,10 +157,23 @@ class SendStatusService : DaggerService(), CoroutineScope {
|
|||
).fold<Any?>(
|
||||
{
|
||||
statusesToSend.remove(id)
|
||||
stopSelfWhenDone()
|
||||
},
|
||||
{
|
||||
when (it) {
|
||||
is NetworkResponseError.ApiError -> {
|
||||
onError(it, id)
|
||||
}
|
||||
)
|
||||
}.apply {
|
||||
sendJobs[id] = this
|
||||
}
|
||||
}
|
||||
|
||||
private fun onError(error: Throwable, id: Int) {
|
||||
|
||||
val statusToSend = statusesToSend[id] ?: return
|
||||
|
||||
when (error) {
|
||||
is NetworkResponseError.ApiError, is UnrecoverableError -> {
|
||||
// the server refused to accept the status, save toot & show error message
|
||||
// TODO saveToDrafts
|
||||
|
||||
|
@ -185,11 +203,6 @@ class SendStatusService : DaggerService(), CoroutineScope {
|
|||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}.apply {
|
||||
sendJobs[id] = this
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopSelfWhenDone() {
|
||||
|
||||
|
@ -248,7 +261,6 @@ class SendStatusService : DaggerService(), CoroutineScope {
|
|||
private var sendingNotificationId = -1 // use negative ids to not clash with other notis
|
||||
private var errorNotificationId = Int.MIN_VALUE // use even more negative ids to not clash with other notis
|
||||
|
||||
@JvmStatic
|
||||
fun sendStatusIntent(
|
||||
context: Context,
|
||||
statusToSend: StatusToSend
|
||||
|
@ -277,3 +289,5 @@ data class StatusToSend(
|
|||
val savedTootUid: Int = 0,
|
||||
var retries: Int = 0
|
||||
) : Parcelable
|
||||
|
||||
private class UnrecoverableError : Exception()
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package at.connyduck.pixelcat.components.util
|
||||
|
||||
import android.webkit.MimeTypeMap
|
||||
import java.util.Locale
|
||||
|
||||
/**
|
||||
* tries to guess the mime type of a file path from the file extension.
|
||||
* @return the mime type, or null if it couldn't be determined
|
||||
*/
|
||||
fun getMimeType(filePath: String): String? {
|
||||
val extension = filePath.split('.').lastOrNull()?.toLowerCase(Locale.ROOT) ?: return null
|
||||
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
|
||||
}
|
Loading…
Reference in New Issue