mirror of
https://git.mihon.tech/mihonapp/mihon
synced 2024-11-27 09:44:55 +03:00
Improve extension discovery. Fix #542
This commit is contained in:
parent
830f792824
commit
0db1a3167d
2 changed files with 24 additions and 11 deletions
|
@ -25,6 +25,7 @@ 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.
|
||||
|
@ -366,19 +367,22 @@ class Downloader(private val context: Context, private val provider: DownloadPro
|
|||
|
||||
/**
|
||||
* Returns the extension of the downloaded image from the network response, or if it's null,
|
||||
* analyze the file. If both fail, assume it's a jpg.
|
||||
* analyze the file. If everything fails, assume it's a jpg.
|
||||
*
|
||||
* @param response the network response of the image.
|
||||
* @param file the file where the image is already downloaded.
|
||||
*/
|
||||
private fun getImageExtension(response: Response, file: UniFile): String {
|
||||
val contentType = response.body().contentType()
|
||||
val mimeStr = if (contentType != null) {
|
||||
"${contentType.type()}/${contentType.subtype()}"
|
||||
} else {
|
||||
context.contentResolver.getType(file.uri)
|
||||
// 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.
|
||||
?: file.openInputStream().buffered().use {
|
||||
URLConnection.guessContentTypeFromStream(it)
|
||||
}
|
||||
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeStr) ?: "jpg"
|
||||
|
||||
return MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg"
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.reader
|
|||
|
||||
import android.os.Bundle
|
||||
import android.os.Environment
|
||||
import android.webkit.MimeTypeMap
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.data.cache.ChapterCache
|
||||
import eu.kanade.tachiyomi.data.cache.CoverCache
|
||||
|
@ -29,6 +30,7 @@ import rx.schedulers.Schedulers
|
|||
import timber.log.Timber
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
import java.io.File
|
||||
import java.net.URLConnection
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
|
@ -563,14 +565,21 @@ class ReaderPresenter : BasePresenter<ReaderActivity>() {
|
|||
// Copy file in background.
|
||||
Observable
|
||||
.fromCallable {
|
||||
// File where the image will be saved.
|
||||
// Folder where the image will be saved.
|
||||
val destDir = File(pictureDirectory)
|
||||
destDir.mkdirs()
|
||||
|
||||
val destFile = File(destDir, manga.title + " - " + chapter.name +
|
||||
" - " + (page.index + 1))
|
||||
// Find out file extension.
|
||||
val mime = context.contentResolver.getType(page.uri)
|
||||
?: context.contentResolver.openInputStream(page.uri).buffered().use {
|
||||
URLConnection.guessContentTypeFromStream(it)
|
||||
}
|
||||
val ext = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime) ?: "jpg"
|
||||
|
||||
// Destination file.
|
||||
val destFile = File(destDir, manga.title + " - " + chapter.name +
|
||||
" - " + (page.index + 1) + ".$ext")
|
||||
|
||||
// Location of image file.
|
||||
context.contentResolver.openInputStream(page.uri).use { input ->
|
||||
destFile.outputStream().use { output ->
|
||||
input.copyTo(output)
|
||||
|
|
Loading…
Reference in a new issue