From d447370a41cd4e7fd42f4625fd9464ce2126398a Mon Sep 17 00:00:00 2001 From: dece Date: Wed, 16 Feb 2022 11:48:09 +0100 Subject: [PATCH] MimeType: use it to process responses accordingly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This prevents Comet from showing JPGs as Gemtext, whew… Any unhandled MIME type now presents an alert, and we should slowly support more and more types. --- .../java/dev/lowrespalmtree/comet/MimeType.kt | 2 +- .../dev/lowrespalmtree/comet/PageViewModel.kt | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt b/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt index 207528d..2bdffb4 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/MimeType.kt @@ -18,7 +18,7 @@ class MimeType( if (";" in string) { val elements = string.split(";") typeString = elements[0] - params = mutableMapOf() + params = mutableMapOf() elements.subList(1, elements.size) .map { it.trim().lowercase() } .map { p -> if (p.count { it == '=' } != 1) return@from null else p } diff --git a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt index 4884d90..cda8c30 100644 --- a/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt +++ b/app/src/main/java/dev/lowrespalmtree/comet/PageViewModel.kt @@ -8,7 +8,6 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import androidx.preference.PreferenceManager -import dev.lowrespalmtree.comet.utils.joinUrls import dev.lowrespalmtree.comet.utils.resolveLinkUri import kotlinx.coroutines.* import kotlinx.coroutines.channels.onSuccess @@ -147,6 +146,20 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState @ExperimentalCoroutinesApi private suspend fun handleSuccessResponse(response: Response, uri: Uri) { + val mimeType = MimeType.from(response.meta) ?: MimeType.DEFAULT // Spec. section 3.3 last § + when (mimeType.main) { + "text" -> { + if (mimeType.sub == "gemini") + handleSuccessGemtextResponse(response, uri) + else + handleSuccessGenericTextResponse(response, uri) + } + else -> signalError("No idea how to process a \"${mimeType.short}\" file.") + } + } + + @ExperimentalCoroutinesApi + private suspend fun handleSuccessGemtextResponse(response: Response, uri: Uri) { state.postValue(State.RECEIVING) val uriString = uri.toString() @@ -199,6 +212,10 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState state.postValue(State.IDLE) } + @ExperimentalCoroutinesApi + private suspend fun handleSuccessGenericTextResponse(response: Response, uri: Uri) = + handleSuccessGemtextResponse(response, uri) // TODO render plain text as... something else? + private fun handleRedirectResponse(response: Response, redirects: Int) { event.postValue(RedirectEvent(response.meta, redirects)) }