Fix some issues when reading/saving images (#993)

* Fix unsupported mime type error when saving images

Avoid using platform mime type map to get extensions as it may not have
all mime types we support.

* Fix jxl images downloading/reading
This commit is contained in:
FooIbar
2024-07-08 18:02:50 +08:00
committed by GitHub
parent cbcd8bd668
commit daa47e0493
4 changed files with 20 additions and 32 deletions

View File

@ -13,7 +13,6 @@ import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.os.Build
import android.webkit.MimeTypeMap
import androidx.annotation.ColorInt
import androidx.core.graphics.alpha
import androidx.core.graphics.applyCanvas
@ -29,7 +28,6 @@ import okio.BufferedSource
import tachiyomi.decoder.Format
import tachiyomi.decoder.ImageDecoder
import java.io.InputStream
import java.net.URLConnection
import java.util.Locale
import kotlin.math.abs
import kotlin.math.max
@ -40,12 +38,8 @@ object ImageUtil {
fun isImage(name: String?, openStream: (() -> InputStream)? = null): Boolean {
if (name == null) return false
val contentType = try {
URLConnection.guessContentTypeFromName(name)
} catch (e: Exception) {
null
} ?: openStream?.let { findImageType(it)?.mime }
return contentType?.startsWith("image/") ?: false
val extension = name.substringAfterLast('.')
return ImageType.entries.any { it.extension == extension } || openStream?.let { findImageType(it) } != null
}
fun findImageType(openStream: () -> InputStream): ImageType? {
@ -69,10 +63,9 @@ object ImageUtil {
}
}
fun getExtensionFromMimeType(mime: String?): String {
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime)
?: SUPPLEMENTARY_MIMETYPE_MAPPING[mime]
?: "jpg"
fun getExtensionFromMimeType(mime: String?, openStream: () -> InputStream): String {
val type = mime?.let { ImageType.entries.find { it.mime == mime } } ?: findImageType(openStream)
return type?.extension ?: "jpg"
}
fun isAnimatedAndSupported(source: BufferedSource): Boolean {
@ -558,12 +551,6 @@ object ImageUtil {
}
private val optimalImageHeight = getDisplayMaxHeightInPx * 2
// Android doesn't include some mappings
private val SUPPLEMENTARY_MIMETYPE_MAPPING = mapOf(
// https://issuetracker.google.com/issues/182703810
"image/jxl" to "jxl",
)
}
val getDisplayMaxHeightInPx: Int