fix: Fixed some issues when reading/saving images

This commit is contained in:
FooIbar 2024-07-10 07:43:34 +07:00 committed by Ahmad Ansori Palembani
parent cb6677cc91
commit 73b395000f
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
3 changed files with 9 additions and 24 deletions

View file

@ -11,6 +11,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.util.system.ImageUtil
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
@ -180,7 +181,7 @@ class DownloadManager(val context: Context) {
fun buildPageList(source: Source, manga: Manga, chapter: Chapter): List<Page> {
val chapterDir = provider.findChapterDir(chapter, manga, source)
val files = chapterDir?.listFiles().orEmpty()
.filter { "image" in it.type.orEmpty() }
.filter { it.isFile && ImageUtil.isImage(it.name.orEmpty()) { it.openInputStream() } }
if (files.isEmpty()) {
throw Exception(context.getString(MR.strings.no_pages_found))

View file

@ -536,14 +536,9 @@ class Downloader(
* @param file the file where the image is already downloaded.
*/
private fun getImageExtension(response: Response, file: UniFile): String {
// Read content type if available.
val mime = response.body.contentType()?.let { ct -> "${ct.type}/${ct.subtype}" }
// Else guess from the uri.
?: context.contentResolver.getType(file.uri)
// Else read magic numbers.
?: ImageUtil.findImageType { file.openInputStream() }?.mime
val mime = response.body.contentType()?.let { ct -> if (ct.type == "image") "image/${ct.subtype}" else null }
return ImageUtil.getExtensionFromMimeType(mime)
return ImageUtil.getExtensionFromMimeType(mime) { file.openInputStream() }
}
private fun splitTallImageIfNeeded(page: Page, tmpDir: UniFile): Boolean {

View file

@ -45,12 +45,8 @@ import kotlin.math.roundToInt
object ImageUtil {
fun isImage(name: String, openStream: (() -> InputStream)? = null): Boolean {
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? {
@ -88,10 +84,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 {
@ -780,12 +775,6 @@ object ImageUtil {
return options
}
// Android doesn't include some mappings
private val SUPPLEMENTARY_MIMETYPE_MAPPING = mapOf(
// https://issuetracker.google.com/issues/182703810
"image/jxl" to "jxl",
)
fun isMaxTextureSizeExceeded(data: Any): Boolean {
val width: Int
val height: Int