Use custom mime discovery in downloader

This commit is contained in:
len 2017-02-02 20:21:38 +01:00
parent 56e7a1e2a0
commit 89940677cc
2 changed files with 18 additions and 21 deletions

View File

@ -14,10 +14,7 @@ import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.source.online.fetchAllImageUrlsFromPageList import eu.kanade.tachiyomi.source.online.fetchAllImageUrlsFromPageList
import eu.kanade.tachiyomi.util.DynamicConcurrentMergeOperator import eu.kanade.tachiyomi.util.*
import eu.kanade.tachiyomi.util.RetryWithDelay
import eu.kanade.tachiyomi.util.plusAssign
import eu.kanade.tachiyomi.util.saveTo
import okhttp3.Response import okhttp3.Response
import rx.Observable import rx.Observable
import rx.android.schedulers.AndroidSchedulers import rx.android.schedulers.AndroidSchedulers
@ -26,7 +23,6 @@ import rx.subjects.BehaviorSubject
import rx.subscriptions.CompositeSubscription import rx.subscriptions.CompositeSubscription
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.net.URLConnection
/** /**
* This class is the one in charge of downloading chapters. * This class is the one in charge of downloading chapters.
@ -407,9 +403,7 @@ class Downloader(private val context: Context, private val provider: DownloadPro
// Else guess from the uri. // Else guess from the uri.
?: context.contentResolver.getType(file.uri) ?: context.contentResolver.getType(file.uri)
// Else read magic numbers. // Else read magic numbers.
?: file.openInputStream().buffered().use { ?: DiskUtil.findImageMime { file.openInputStream() }
URLConnection.guessContentTypeFromStream(it)
}
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg" return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg"
} }

View File

@ -14,38 +14,41 @@ object DiskUtil {
fun isImage(name: String, openStream: (() -> InputStream)? = null): Boolean { fun isImage(name: String, openStream: (() -> InputStream)? = null): Boolean {
val contentType = URLConnection.guessContentTypeFromName(name) val contentType = URLConnection.guessContentTypeFromName(name)
if (contentType != null) ?: openStream?.let { findImageMime(it) }
return contentType.startsWith("image/")
if (openStream != null) try { return contentType?.startsWith("image/") ?: false
openStream.invoke().buffered().use { }
var bytes = ByteArray(11)
fun findImageMime(openStream: (() -> InputStream)): String? {
try {
openStream().buffered().use {
val bytes = ByteArray(11)
it.mark(bytes.size) it.mark(bytes.size)
var length = it.read(bytes, 0, bytes.size) val length = it.read(bytes, 0, bytes.size)
it.reset() it.reset()
if (length == -1) if (length == -1)
return false return null
if (bytes[0] == 'G'.toByte() && bytes[1] == 'I'.toByte() && bytes[2] == 'F'.toByte() && bytes[3] == '8'.toByte()) { if (bytes[0] == 'G'.toByte() && bytes[1] == 'I'.toByte() && bytes[2] == 'F'.toByte() && bytes[3] == '8'.toByte()) {
return true // image/gif return "image/gif"
} else if (bytes[0] == 0x89.toByte() && bytes[1] == 0x50.toByte() && bytes[2] == 0x4E.toByte() } else if (bytes[0] == 0x89.toByte() && bytes[1] == 0x50.toByte() && bytes[2] == 0x4E.toByte()
&& bytes[3] == 0x47.toByte() && bytes[4] == 0x0D.toByte() && bytes[5] == 0x0A.toByte() && bytes[3] == 0x47.toByte() && bytes[4] == 0x0D.toByte() && bytes[5] == 0x0A.toByte()
&& bytes[6] == 0x1A.toByte() && bytes[7] == 0x0A.toByte()) { && bytes[6] == 0x1A.toByte() && bytes[7] == 0x0A.toByte()) {
return true // image/png return "image/png"
} else if (bytes[0] == 0xFF.toByte() && bytes[1] == 0xD8.toByte() && bytes[2] == 0xFF.toByte()) { } else if (bytes[0] == 0xFF.toByte() && bytes[1] == 0xD8.toByte() && bytes[2] == 0xFF.toByte()) {
if (bytes[3] == 0xE0.toByte() || bytes[3] == 0xE1.toByte() && bytes[6] == 'E'.toByte() if (bytes[3] == 0xE0.toByte() || bytes[3] == 0xE1.toByte() && bytes[6] == 'E'.toByte()
&& bytes[7] == 'x'.toByte() && bytes[8] == 'i'.toByte() && bytes[7] == 'x'.toByte() && bytes[8] == 'i'.toByte()
&& bytes[9] == 'f'.toByte() && bytes[10] == 0.toByte()) { && bytes[9] == 'f'.toByte() && bytes[10] == 0.toByte()) {
return true // image/jpeg return "image/jpeg"
} else if (bytes[3] == 0xEE.toByte()) { } else if (bytes[3] == 0xEE.toByte()) {
return true // image/jpg return "image/jpg"
} }
} else if (bytes[0] == 'W'.toByte() && bytes[1] == 'E'.toByte() && bytes[2] == 'B'.toByte() && bytes[3] == 'P'.toByte()) { } else if (bytes[0] == 'W'.toByte() && bytes[1] == 'E'.toByte() && bytes[2] == 'B'.toByte() && bytes[3] == 'P'.toByte()) {
return true // image/webp return "image/webp"
} }
} }
} catch(e: Exception) { } catch(e: Exception) {
} }
return false return null
} }
fun hashKeyForDisk(key: String): String { fun hashKeyForDisk(key: String): String {