mirror of
https://github.com/tachiyomiorg/tachiyomi.git
synced 2024-12-22 18:11:50 +01:00
Use custom mime discovery in downloader
This commit is contained in:
parent
56e7a1e2a0
commit
89940677cc
@ -14,10 +14,7 @@ import eu.kanade.tachiyomi.source.SourceManager
|
||||
import eu.kanade.tachiyomi.source.model.Page
|
||||
import eu.kanade.tachiyomi.source.online.HttpSource
|
||||
import eu.kanade.tachiyomi.source.online.fetchAllImageUrlsFromPageList
|
||||
import eu.kanade.tachiyomi.util.DynamicConcurrentMergeOperator
|
||||
import eu.kanade.tachiyomi.util.RetryWithDelay
|
||||
import eu.kanade.tachiyomi.util.plusAssign
|
||||
import eu.kanade.tachiyomi.util.saveTo
|
||||
import eu.kanade.tachiyomi.util.*
|
||||
import okhttp3.Response
|
||||
import rx.Observable
|
||||
import rx.android.schedulers.AndroidSchedulers
|
||||
@ -26,7 +23,6 @@ import rx.subjects.BehaviorSubject
|
||||
import rx.subscriptions.CompositeSubscription
|
||||
import timber.log.Timber
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.net.URLConnection
|
||||
|
||||
/**
|
||||
* 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.
|
||||
?: context.contentResolver.getType(file.uri)
|
||||
// Else read magic numbers.
|
||||
?: file.openInputStream().buffered().use {
|
||||
URLConnection.guessContentTypeFromStream(it)
|
||||
}
|
||||
?: DiskUtil.findImageMime { file.openInputStream() }
|
||||
|
||||
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg"
|
||||
}
|
||||
|
@ -14,38 +14,41 @@ object DiskUtil {
|
||||
|
||||
fun isImage(name: String, openStream: (() -> InputStream)? = null): Boolean {
|
||||
val contentType = URLConnection.guessContentTypeFromName(name)
|
||||
if (contentType != null)
|
||||
return contentType.startsWith("image/")
|
||||
?: openStream?.let { findImageMime(it) }
|
||||
|
||||
if (openStream != null) try {
|
||||
openStream.invoke().buffered().use {
|
||||
var bytes = ByteArray(11)
|
||||
return contentType?.startsWith("image/") ?: false
|
||||
}
|
||||
|
||||
fun findImageMime(openStream: (() -> InputStream)): String? {
|
||||
try {
|
||||
openStream().buffered().use {
|
||||
val bytes = ByteArray(11)
|
||||
it.mark(bytes.size)
|
||||
var length = it.read(bytes, 0, bytes.size)
|
||||
val length = it.read(bytes, 0, bytes.size)
|
||||
it.reset()
|
||||
if (length == -1)
|
||||
return false
|
||||
return null
|
||||
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()
|
||||
&& bytes[3] == 0x47.toByte() && bytes[4] == 0x0D.toByte() && bytes[5] == 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()) {
|
||||
if (bytes[3] == 0xE0.toByte() || bytes[3] == 0xE1.toByte() && bytes[6] == 'E'.toByte()
|
||||
&& bytes[7] == 'x'.toByte() && bytes[8] == 'i'.toByte()
|
||||
&& bytes[9] == 'f'.toByte() && bytes[10] == 0.toByte()) {
|
||||
return true // image/jpeg
|
||||
return "image/jpeg"
|
||||
} 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()) {
|
||||
return true // image/webp
|
||||
return "image/webp"
|
||||
}
|
||||
}
|
||||
} catch(e: Exception) {
|
||||
}
|
||||
return false
|
||||
return null
|
||||
}
|
||||
|
||||
fun hashKeyForDisk(key: String): String {
|
||||
|
Loading…
Reference in New Issue
Block a user