attachments: proper metadata plumbing
This commit is contained in:
@@ -71,11 +71,17 @@ data class Message(
|
||||
}
|
||||
}
|
||||
|
||||
data class UploadingAttachmentMetadata(
|
||||
val inputStream: InputStream,
|
||||
val mimeType: String,
|
||||
val filename: String,
|
||||
)
|
||||
|
||||
data class OutgoingMessage(
|
||||
val body: String,
|
||||
val conversation: Conversation,
|
||||
val attachmentUris: Set<Uri>,
|
||||
val attachmentDataSource: (Uri) -> InputStream?
|
||||
val attachmentDataSource: (Uri) -> UploadingAttachmentMetadata?
|
||||
) {
|
||||
val guid: String = UUID.randomUUID().toString()
|
||||
|
||||
|
||||
@@ -1,45 +1,37 @@
|
||||
package net.buzzert.kordophone.backend.server
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.net.toFile
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.consumeEach
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.buzzert.kordophone.backend.db.CachedChatDatabase
|
||||
import net.buzzert.kordophone.backend.events.MessageDeliveredEvent
|
||||
import net.buzzert.kordophone.backend.model.Conversation
|
||||
import net.buzzert.kordophone.backend.model.GUID
|
||||
import net.buzzert.kordophone.backend.model.Message
|
||||
import net.buzzert.kordophone.backend.model.OutgoingMessage
|
||||
import net.buzzert.kordophone.backend.model.UpdateItem
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.RequestBody
|
||||
import okio.BufferedSource
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.lang.Error
|
||||
import java.net.URL
|
||||
import java.util.Date
|
||||
import java.util.Queue
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.ArrayBlockingQueue
|
||||
import java.util.concurrent.CancellationException
|
||||
import kotlin.Boolean
|
||||
import kotlin.Int
|
||||
import kotlin.String
|
||||
import kotlin.let
|
||||
|
||||
const val REPO_LOG: String = "ChatRepository"
|
||||
const val CONVERSATION_MESSAGE_SYNC_COUNT = 10
|
||||
@@ -210,7 +202,10 @@ class ChatRepository(
|
||||
private suspend fun uploadAttachment(filename: String, mediaType: String, source: InputStream): String {
|
||||
val attachmentData = source.readBytes()
|
||||
val requestBody = RequestBody.create(MediaType.get(mediaType), attachmentData)
|
||||
source.close()
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
source.close()
|
||||
}
|
||||
|
||||
val response = apiInterface.uploadAttachment(filename, requestBody)
|
||||
return response.bodyOnSuccessOrThrow().transferGUID
|
||||
@@ -237,7 +232,7 @@ class ChatRepository(
|
||||
.onEach { it.conversation = conversation }
|
||||
}
|
||||
|
||||
private suspend fun handleConversationChangedUpdate(conversation: Conversation) {
|
||||
private fun handleConversationChangedUpdate(conversation: Conversation) {
|
||||
Log.d(REPO_LOG, "Handling conversation changed update")
|
||||
database.writeConversations(listOf(conversation))
|
||||
}
|
||||
@@ -276,13 +271,10 @@ class ChatRepository(
|
||||
val attachmentGUIDs = mutableListOf<String>()
|
||||
try {
|
||||
for (uri: Uri in it.attachmentUris) {
|
||||
val inputStream = it.attachmentDataSource(uri)
|
||||
?: throw java.lang.Exception("No input stream")
|
||||
|
||||
val filename = uri.lastPathSegment ?: "attachment.png"
|
||||
val mediaType = "image/png" // TODO: Actually get this: it needs to be plumbed through ContentResolver
|
||||
val guid = uploadAttachment(filename, mediaType, inputStream)
|
||||
val uploadData = it.attachmentDataSource(uri)
|
||||
?: throw java.lang.Exception("No upload data.")
|
||||
|
||||
val guid = uploadAttachment(uploadData.filename, uploadData.mimeType, uploadData.inputStream)
|
||||
attachmentGUIDs.add(guid)
|
||||
}
|
||||
} catch (e: java.lang.Exception) {
|
||||
|
||||
Reference in New Issue
Block a user