Ensure images use .jpg extension instead of .jpeg

This commit is contained in:
FooIbar committed 2024-02-06 16:24:55 +08:00
1 parent 21016dda53
commit b4f4d442eb
2 files changed
+17 -35

No files matched your search

@@ -15,6 +15,7 @@
*/
package com.hippo.ehviewer.spider
import android.webkit.MimeTypeMap
import arrow.fx.coroutines.autoCloseable
import arrow.fx.coroutines.closeable
import arrow.fx.coroutines.parMap
@@ -40,10 +41,9 @@ import eu.kanade.tachiyomi.util.lang.withNonCancellableContext
import io.ktor.client.plugins.onDownload
import io.ktor.client.statement.HttpResponse
import io.ktor.client.statement.bodyAsChannel
import io.ktor.http.contentType
import io.ktor.client.statement.request
import io.ktor.http.isSuccess
import io.ktor.utils.io.jvm.nio.copyTo
import java.util.Locale
import kotlin.io.path.readText
class SpiderDen(val info: GalleryInfo) {
@@ -77,7 +77,7 @@ class SpiderDen(val info: GalleryInfo) {
val key = getImageKey(gid, index)
return runCatching {
sCache.read(key) {
val extension = "." + metadata.toFile().readText()
val extension = metadata.toFile().readText()
val file = dir.createFile(perFilename(index, extension)) ?: return false
data.asUniFile() sendTo file
true
@@ -118,8 +118,7 @@ class SpiderDen(val info: GalleryInfo) {
private fun findDownloadFileForIndex(index: Int, extension: String): UniFile? {
val dir = downloadDir ?: return null
val ext = ".$extension"
return dir.createFile(perFilename(index, ext))
return dir.createFile(perFilename(index, extension))
}
suspend fun makeHttpCallAndSaveImage(
@@ -163,8 +162,8 @@ class SpiderDen(val info: GalleryInfo) {
}
private suspend fun saveFromHttpResponse(index: Int, response: HttpResponse): Boolean {
val contentType = response.contentType()
val extension = contentType?.contentSubtype ?: "jpg"
val url = response.request.url.toString()
val extension = MimeTypeMap.getFileExtensionFromUrl(url).takeUnless { it.isEmpty() } ?: "jpg"
return saveResponseMeta(index, extension) { outFile ->
outFile.openOutputStream().use {
val chan = it.channel.apply { truncate(0) }
@@ -236,7 +235,7 @@ class SpiderDen(val info: GalleryInfo) {
val (fdBatch, names) = (0 until pages).parMap { idx ->
val ext = requireNotNull(getExtension(idx))
val f = autoCloseable { requireNotNull(getImageSource(idx)) }
closeable { f.source.openFileDescriptor("r") }.fd to perFilename(idx, ".$ext")
closeable { f.source.openFileDescriptor("r") }.fd to perFilename(idx, ext)
}.run { plus(comicInfo.fd to COMIC_INFO_FILE) }.unzip()
val arcFd = closeable { file.openFileDescriptor("rw") }
archiveFdBatch(fdBatch.toIntArray(), names.toTypedArray(), arcFd.fd, pages + 1)
@@ -262,15 +261,12 @@ class SpiderDen(val info: GalleryInfo) {
}
}
/**
* @param extension with dot
*/
fun perFilename(index: Int, extension: String?): String {
return String.format(Locale.US, "%08d%s", index + 1, extension)
fun perFilename(index: Int, extension: String = ""): String {
return "%08d.%s".format(index + 1, extension)
}
private fun findImageFile(dir: UniFile, index: Int): UniFile? {
val head = perFilename(index, ".")
val head = perFilename(index)
return dir.findFirst { name -> name.startsWith(head) }
}
@@ -50,27 +50,13 @@ class TreeDocumentFile(
if (child != null) {
if (child.isFile) return child
} else {
val index = displayName.lastIndexOf('.')
if (index > 0) {
val name = displayName.substring(0, index)
val extension = displayName.substring(index + 1)
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
if (!mimeType.isNullOrEmpty()) {
val result = DocumentsContractApi21.createFile(uri, mimeType, name)
if (result != null) {
val f = TreeDocumentFile(this, result, displayName)
popCacheIfPresent(f)
return f
}
}
} else {
// Not dot in displayName or dot is the first char or can't get MimeType
val result = DocumentsContractApi21.createFile(uri, "application/octet-stream", displayName)
if (result != null) {
val f = TreeDocumentFile(this, result, displayName)
popCacheIfPresent(f)
return f
}
val extension = displayName.substringAfterLast('.', "")
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "application/octet-stream"
val result = DocumentsContractApi21.createFile(uri, mimeType, displayName)
if (result != null) {
val f = TreeDocumentFile(this, result, displayName)
popCacheIfPresent(f)
return f
}
}
return null