diff --git a/core/domain/src/main/kotlin/ru/tech/imageresizershrinker/core/domain/Constants.kt b/core/domain/src/main/kotlin/ru/tech/imageresizershrinker/core/domain/Constants.kt index 716c9a6acd..cc498119d1 100644 --- a/core/domain/src/main/kotlin/ru/tech/imageresizershrinker/core/domain/Constants.kt +++ b/core/domain/src/main/kotlin/ru/tech/imageresizershrinker/core/domain/Constants.kt @@ -30,4 +30,7 @@ const val TELEGRAM_CHANNEL_LINK = "https://t.me/t8rin_imagetoolbox_ci" const val BitcoinWallet = "17Pk1RurnkJxLV9V7mc6Y7dLyHFb9rvQDq" const val USDTWallet = "TMPAu7a54NvQNEKnNWh3naXu3oYijqP3U7" const val TONSpaceWallet = "UQDMePBU-FaxwaIME8p7h2spRITeRxvtCPegtKefeV5v-sN1" -const val TONWallet = "UQB44LtN0zArKGcWTbNxU7zAkGYunVs1rkn_VsLq71G4bdmK" \ No newline at end of file +const val TONWallet = "UQB44LtN0zArKGcWTbNxU7zAkGYunVs1rkn_VsLq71G4bdmK" + +const val USER_AGENT = + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36" \ No newline at end of file diff --git a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LinkUtils.kt b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LinkUtils.kt index 065f5b6752..cd72a9b105 100644 --- a/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LinkUtils.kt +++ b/core/ui/src/main/kotlin/ru/tech/imageresizershrinker/core/ui/utils/helper/LinkUtils.kt @@ -20,6 +20,7 @@ package ru.tech.imageresizershrinker.core.ui.utils.helper import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.jsoup.Jsoup +import ru.tech.imageresizershrinker.core.domain.USER_AGENT object LinkUtils { fun parseLinks(text: String): Set { @@ -48,6 +49,7 @@ suspend fun LinkPreview( runCatching { Jsoup .connect(link) + .userAgent(USER_AGENT) .execute() .parse() .getElementsByTag("meta") diff --git a/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/data/AndroidHtmlImageParser.kt b/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/data/AndroidHtmlImageParser.kt index 890725faa0..48ed799d99 100644 --- a/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/data/AndroidHtmlImageParser.kt +++ b/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/data/AndroidHtmlImageParser.kt @@ -23,6 +23,7 @@ import androidx.exifinterface.media.ExifInterface import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.withContext import org.jsoup.Jsoup +import ru.tech.imageresizershrinker.core.domain.USER_AGENT import ru.tech.imageresizershrinker.core.domain.dispatchers.DispatchersHolder import ru.tech.imageresizershrinker.core.domain.image.ImageGetter import ru.tech.imageresizershrinker.core.domain.image.ShareProvider @@ -44,27 +45,23 @@ internal class AndroidHtmlImageParser @Inject constructor( url: String, onFailure: (message: String) -> Unit ): List = withContext(defaultDispatcher) { - val baseImage = imageGetter.getImage(data = url)?.let { - shareProvider.cacheImage( - image = it, - imageInfo = ImageInfo( - width = it.width, - height = it.height, - imageFormat = ImageFormat.Png.Lossless - ) - ) - }.let(::listOfNotNull) + val realUrl = if (url.isMalformed()) { + "https://$url" + } else url + + val baseImage = loadImage(realUrl) - val parsedImages = if (url.isNotEmpty()) { + val parsedImages = if (realUrl.isNotEmpty()) { runCatching { val parsed = Jsoup - .connect(url) + .connect(realUrl) + .userAgent(USER_AGENT) .execute() .parse() val list = parsed.getElementsByTag("img") .mapNotNull { element -> - element.absUrl("src").takeIf { it.isNotEmpty() } + element.absUrl("src").takeIf { it.isNotEmpty() }?.substringBefore("?") } val content = parsed.getElementsByTag("meta") @@ -75,7 +72,16 @@ internal class AndroidHtmlImageParser @Inject constructor( } } - content + list + val favIcon = loadImage( + parsed.head() + .select("link[href~=.*\\.ico]") + .firstOrNull() + ?.attr("href") ?: "" + ).ifEmpty { + loadImage(realUrl.removeSuffix("/") + "/favicon.ico") + } + + content + list + favIcon }.onFailure { if (it is UnknownHostException) onFailure(context.getString(R.string.unknown_host)) }.getOrNull() ?: emptyList() @@ -86,4 +92,19 @@ internal class AndroidHtmlImageParser @Inject constructor( baseImage + parsedImages } + private suspend fun loadImage( + url: String + ): List = imageGetter.getImage(data = url)?.let { + shareProvider.cacheImage( + image = it, + imageInfo = ImageInfo( + width = it.width, + height = it.height, + imageFormat = ImageFormat.Png.Lossless + ) + ) + }.let(::listOfNotNull) + + private fun String.isMalformed(): Boolean = !(startsWith("https://") || startsWith("http://")) + } \ No newline at end of file diff --git a/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/presentation/LoadNetImageContent.kt b/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/presentation/LoadNetImageContent.kt index 85d7714af7..e0f932e992 100644 --- a/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/presentation/LoadNetImageContent.kt +++ b/feature/load-net-image/src/main/java/ru/tech/imageresizershrinker/feature/load_net_image/presentation/LoadNetImageContent.kt @@ -211,6 +211,9 @@ fun LoadNetImageContent( allowHardware = false, model = component.targetUrl, modifier = Modifier + .container( + resultPadding = 8.dp + ) .then( if (component.bitmap == null) { Modifier @@ -219,9 +222,6 @@ fun LoadNetImageContent( } else { Modifier.aspectRatio(component.bitmap?.safeAspectRatio ?: 2f) } - ) - .container( - resultPadding = 8.dp ), isLoadingFromDifferentPlace = component.isImageLoading, contentScale = ContentScale.FillBounds, @@ -324,7 +324,8 @@ fun LoadNetImageContent( layout(result.measuredWidth, result.measuredHeight) { result.place(0, 0) } - } + }, + showExtension = false ) } }