From 0db1a3167ddb2ec6b1c2d74391ef01c28b416778 Mon Sep 17 00:00:00 2001 From: len Date: Tue, 22 Nov 2016 20:49:57 +0100 Subject: [PATCH] Improve extension discovery. Fix #542 --- .../tachiyomi/data/download/Downloader.kt | 18 +++++++++++------- .../tachiyomi/ui/reader/ReaderPresenter.kt | 17 +++++++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt index e1f5decb12..eef46535da 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/Downloader.kt @@ -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" } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt index ccfedecc45..5169592619 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderPresenter.kt @@ -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() { // 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)