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.
main
dece 2 years ago
parent a76e84cf1d
commit d447370a41

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

@ -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))
}

Loading…
Cancel
Save