MimeType: use it to process responses accordingly

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.
This commit is contained in:
dece 2022-02-16 11:48:09 +01:00
parent a76e84cf1d
commit d447370a41
2 changed files with 19 additions and 2 deletions

View file

@ -18,7 +18,7 @@ class MimeType(
if (";" in string) { if (";" in string) {
val elements = string.split(";") val elements = string.split(";")
typeString = elements[0] typeString = elements[0]
params = mutableMapOf<String, String>() params = mutableMapOf()
elements.subList(1, elements.size) elements.subList(1, elements.size)
.map { it.trim().lowercase() } .map { it.trim().lowercase() }
.map { p -> if (p.count { it == '=' } != 1) return@from null else p } .map { p -> if (p.count { it == '=' } != 1) return@from null else p }

View file

@ -8,7 +8,6 @@ import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import dev.lowrespalmtree.comet.utils.joinUrls
import dev.lowrespalmtree.comet.utils.resolveLinkUri import dev.lowrespalmtree.comet.utils.resolveLinkUri
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.onSuccess import kotlinx.coroutines.channels.onSuccess
@ -147,6 +146,20 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
private suspend fun handleSuccessResponse(response: Response, uri: Uri) { 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) state.postValue(State.RECEIVING)
val uriString = uri.toString() val uriString = uri.toString()
@ -199,6 +212,10 @@ class PageViewModel(@Suppress("unused") private val savedStateHandle: SavedState
state.postValue(State.IDLE) 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) { private fun handleRedirectResponse(response: Response, redirects: Int) {
event.postValue(RedirectEvent(response.meta, redirects)) event.postValue(RedirectEvent(response.meta, redirects))
} }