From a89fe8571c0b73387404a7bc1d934808291d24df Mon Sep 17 00:00:00 2001 From: dev-claw <53543762+dev-claw@users.noreply.github.com> Date: Mon, 18 Aug 2025 19:34:21 +0100 Subject: [PATCH] fixes #243 (#246) --- .../main/kotlin/me/vripper/host/ImxHost.kt | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/vripper-core/src/main/kotlin/me/vripper/host/ImxHost.kt b/vripper-core/src/main/kotlin/me/vripper/host/ImxHost.kt index 34950d5..48e95c0 100644 --- a/vripper-core/src/main/kotlin/me/vripper/host/ImxHost.kt +++ b/vripper-core/src/main/kotlin/me/vripper/host/ImxHost.kt @@ -23,6 +23,15 @@ internal class ImxHost : Host("imx.to", 8) { private val log by LoggerDelegate() + private val failFastHttpClient = HttpClients.custom().apply { + setConnectionManager(BasicHttpClientConnectionManager().apply { + connectionConfig = ConnectionConfig.custom() + .setConnectTimeout(5000, TimeUnit.MILLISECONDS) + .setSocketTimeout(5000, TimeUnit.MILLISECONDS) + .build() + }) + }.build() + @Throws(HostException::class) override fun resolve( image: ImageEntity, @@ -31,29 +40,21 @@ internal class ImxHost : Host("imx.to", 8) { log.debug("Resolving name and image url for ${image.url}") val imgTitle = String.format("IMG_%04d", image.index + 1) synchronized(image.postId.toString().intern()) { - val resolvedHost = resolvedHosts.getIfPresent(image.postId) - if (resolvedHost != null) { - val imgUrl = image.thumbUrl.replace("imx.to", resolvedHost).replace("u/t/", "i/") - .replace("t/", "i/") + val subDomain = resolvedHosts.getIfPresent(image.postId) + if (subDomain != null) { + val imgUrl = findPattern(image, subDomain) return Pair( imgTitle.ifEmpty { getDefaultImageName(imgUrl) }, imgUrl ) } else { IMX_SUBDOMAINS.forEach { subDomain -> - val imgUrl = image.thumbUrl.replace("imx.to", subDomain).replace("u/t/", "i/").replace("t/", "i/") + val imgUrl = findPattern(image, subDomain) val result = runCatching { RequestLimit.getPermit(1) val httpHead = HttpHead(imgUrl).also { context.requests.add(it) } - HttpClients.custom().apply { - setConnectionManager(BasicHttpClientConnectionManager().apply { - connectionConfig = ConnectionConfig.custom() - .setConnectTimeout(5000, TimeUnit.MILLISECONDS) - .setSocketTimeout(5000, TimeUnit.MILLISECONDS) - .build() - }) - }.build().execute(httpHead) { response -> + failFastHttpClient.execute(httpHead) { response -> if (response.code / 100 != 2) { - throw HostException("Invalid response") + throw HostException("Invalid response code ${response.code}") } } } @@ -68,4 +69,10 @@ internal class ImxHost : Host("imx.to", 8) { } throw HostException("Unable to find full size image for ${image.url}") } + + private fun findPattern(image: ImageEntity, subDomain: String): String = image.thumbUrl + .replace("imx.to", subDomain) + .replace("upload/small/", "i/") + .replace("u/t/", "i/") + .replace("t/", "i/") } \ No newline at end of file