Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import suwayomi.tachidesk.graphql.types.WebUIChannel
import suwayomi.tachidesk.graphql.types.WebUIFlavor
import suwayomi.tachidesk.graphql.types.WebUIInterface
import suwayomi.tachidesk.manga.impl.backup.proto.models.BackupSettingsDownloadConversionType
import suwayomi.tachidesk.manga.impl.extension.repoMatchRegex
import suwayomi.tachidesk.manga.impl.extension.ExtensionsList.repoMatchRegex
import suwayomi.tachidesk.server.settings.BooleanSetting
import suwayomi.tachidesk.server.settings.DisableableDoubleSetting
import suwayomi.tachidesk.server.settings.DisableableIntSetting
Expand All @@ -50,7 +50,6 @@ import suwayomi.tachidesk.server.settings.PathSetting
import suwayomi.tachidesk.server.settings.SettingGroup
import suwayomi.tachidesk.server.settings.SettingsRegistry
import suwayomi.tachidesk.server.settings.StringSetting
import xyz.nulldev.ts.config.GlobalConfigManager
import xyz.nulldev.ts.config.SystemPropertyOverridableConfigModule
import kotlin.collections.associate
import kotlin.time.Duration
Expand All @@ -63,8 +62,6 @@ val mutableConfigValueScope = CoroutineScope(SupervisorJob() + Dispatchers.Defau

const val SERVER_CONFIG_MODULE_NAME = "server"

val serverConfig: ServerConfig by lazy { GlobalConfigManager.module() }

// Settings are ordered by "protoNumber".
class ServerConfig(
getConfig: () -> Config,
Expand Down Expand Up @@ -763,15 +760,12 @@ class ServerConfig(
)

val webUISubpath: MutableStateFlow<String> by StringSetting(
protoNumber = 75,
protoNumber = 76,
group = SettingGroup.WEB_UI,
defaultValue = "",
pattern = "^(/[a-zA-Z0-9._-]+)*$".toRegex(),
description = "Serve WebUI under a subpath (e.g., /manga). Leave empty for root path. Must start with / if specified.",
requiresRestart = true,
description = "Subpath for serving the web UI (e.g., '/tachidesk')",
)


/** ****************************************************************** **/
/** **/
/** Renamed settings **/
Expand Down
42 changes: 37 additions & 5 deletions server/src/main/kotlin/suwayomi/tachidesk/manga/impl/Page.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ import suwayomi.tachidesk.manga.impl.util.storage.ImageUtil
import suwayomi.tachidesk.manga.model.table.ChapterTable
import suwayomi.tachidesk.manga.model.table.MangaTable
import suwayomi.tachidesk.manga.model.table.PageTable
import suwayomi.tachidesk.server.serverConfig
import suwayomi.tachidesk.util.ConversionUtil
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
import java.io.PipedInputStream
import java.io.PipedOutputStream
import javax.imageio.ImageIO

object Page {
Expand Down Expand Up @@ -142,20 +145,49 @@ object Page {
image: Pair<InputStream, String>,
format: String? = null,
): Pair<InputStream, String> {
val imageExtension = MimeUtils.guessExtensionFromMimeType(image.second) ?: image.second.removePrefix("image/")
var currentImage = image
var currentMimeType = image.second

// Step 1: Check if HTTP upscaling is configured
val conversions = serverConfig.downloadConversions.value
val defaultConversion = conversions["default"]
val imageType = image.second
val conversion = conversions[imageType] ?: defaultConversion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better than what you had in #1678, but I think it would make sense to allow both upscaling and converting, i.e. first upscale if desired, then convert (as mentioned on discord)

Copy link
Contributor

@Robonau Robonau Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not really specifically for upscaling, a converter server could also do the conversion to webp or whatever

my use-case for this feature would be just to have access to all the options when converting to webp that the current conversion system hides


// Apply HTTP upscaling if configured (complementary with format conversion)
if (conversion != null && ConversionUtil.isHttpConversion(conversion)) {
try {
val upscaledStream = ConversionUtil.upscaleImageHttp(currentImage.first, currentMimeType, conversion.target)
if (upscaledStream != null) {
// Update current image to upscaled version
currentImage = Pair(upscaledStream, "image/jpeg") // HTTP upscaler returns JPEG
currentMimeType = "image/jpeg"
}
} catch (e: Exception) {
// HTTP upscaling failed, continue with original image
}
}

val targetExtension =
(if (format != imageExtension) format else null)
?: return image
// Step 2: Apply format conversion if requested (works on upscaled or original image)
val imageExtension = MimeUtils.guessExtensionFromMimeType(currentMimeType) ?: currentMimeType.removePrefix("image/")
val targetExtension = (if (format != imageExtension) format else null) ?: return currentImage

return convertToFormat(currentImage.first, currentMimeType, targetExtension)
}

private fun convertToFormat(
inputStream: InputStream,
sourceMimeType: String,
targetExtension: String,
): Pair<InputStream, String> {
val outStream = ByteArrayOutputStream()
val writers = ImageIO.getImageWritersBySuffix(targetExtension)
val writer = writers.next()
ImageIO.createImageOutputStream(outStream).use { o ->
writer.setOutput(o)

val inImage =
ConversionUtil.readImage(image.first, image.second)
ConversionUtil.readImage(inputStream, sourceMimeType)
?: throw NoSuchElementException("No conversion to $targetExtension possible")
writer.write(inImage)
}
Expand Down
96 changes: 96 additions & 0 deletions server/src/main/kotlin/suwayomi/tachidesk/util/ConversionUtil.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package suwayomi.tachidesk.util

import io.github.oshai.kotlinlogging.KotlinLogging
import libcore.net.MimeUtils
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.asRequestBody
import suwayomi.tachidesk.graphql.types.DownloadConversion
import java.awt.image.BufferedImage
import java.io.File
import java.io.InputStream
import java.util.concurrent.TimeUnit
import javax.imageio.ImageIO

object ConversionUtil {
Expand Down Expand Up @@ -49,4 +57,92 @@ object ConversionUtil {
logger.info { "No suitable image converter found for $mimeType" }
return null
}

/**
* Send image to external HTTP service for upscaling
* Returns the upscaled image stream or null if failed
*/
fun upscaleImageHttp(
imageFile: File,
targetUrl: String,
): InputStream? =
try {
logger.debug { "Sending ${imageFile.name} to HTTP upscaler: $targetUrl" }

val contentType = MimeUtils.guessMimeTypeFromExtension(imageFile.extension) ?: "application/octet-stream"

val requestBody =
MultipartBody
.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
"image",
imageFile.name,
imageFile.asRequestBody(contentType.toMediaType()),
).build()

val client =
OkHttpClient
.Builder()
.connectTimeout(5, TimeUnit.SECONDS) // Faster timeout for connection
.readTimeout(60, TimeUnit.SECONDS) // Reasonable timeout for processing
.build()

val request =
Request
.Builder()
.url(targetUrl)
.post(requestBody)
.build()

val response = client.newCall(request).execute()
if (response.isSuccessful) {
logger.debug { "HTTP upscaling successful for ${imageFile.name}" }
response.body?.byteStream()
} else {
logger.warn { "HTTP upscaling failed: ${response.code} - ${response.message}" }
null
}
} catch (e: Exception) {
logger.warn(e) { "HTTP upscaling failed for ${imageFile.name}" }
null
}

/**
* Overload that takes InputStream and mimeType, creates temp file for HTTP upload
*/
fun upscaleImageHttp(
inputStream: InputStream,
mimeType: String,
targetUrl: String,
): InputStream? =
try {
// Create temporary file from input stream
val extension = MimeUtils.guessExtensionFromMimeType(mimeType) ?: "tmp"

val tempFile =
kotlin.io.path
.createTempFile("upscale", ".$extension")
.toFile()
tempFile.outputStream().use { output ->
inputStream.copyTo(output)
}

// Upscale using file method
val result = upscaleImageHttp(tempFile, targetUrl)

// Clean up temp file
tempFile.delete()

result
} catch (e: Exception) {
logger.warn(e) { "Failed to create temp file for HTTP upscaling" }
null
}

/**
* Check if a DownloadConversion target is an HTTP URL
*/
fun isHttpConversion(conversion: DownloadConversion): Boolean =
conversion.target.startsWith("http://") || conversion.target.startsWith("https://")
}