Skip to content

Commit

Permalink
Improve load image from net
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Nov 20, 2024
1 parent 13ac684 commit 1b2e22e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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"
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Expand Down Expand Up @@ -48,6 +49,7 @@ suspend fun LinkPreview(
runCatching {
Jsoup
.connect(link)
.userAgent(USER_AGENT)
.execute()
.parse()
.getElementsByTag("meta")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -44,27 +45,23 @@ internal class AndroidHtmlImageParser @Inject constructor(
url: String,
onFailure: (message: String) -> Unit
): List<String> = 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")
Expand All @@ -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()
Expand All @@ -86,4 +92,19 @@ internal class AndroidHtmlImageParser @Inject constructor(
baseImage + parsedImages
}

private suspend fun loadImage(
url: String
): List<String> = 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://"))

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ fun LoadNetImageContent(
allowHardware = false,
model = component.targetUrl,
modifier = Modifier
.container(
resultPadding = 8.dp
)
.then(
if (component.bitmap == null) {
Modifier
Expand All @@ -219,9 +222,6 @@ fun LoadNetImageContent(
} else {
Modifier.aspectRatio(component.bitmap?.safeAspectRatio ?: 2f)
}
)
.container(
resultPadding = 8.dp
),
isLoadingFromDifferentPlace = component.isImageLoading,
contentScale = ContentScale.FillBounds,
Expand Down Expand Up @@ -324,7 +324,8 @@ fun LoadNetImageContent(
layout(result.measuredWidth, result.measuredHeight) {
result.place(0, 0)
}
}
},
showExtension = false
)
}
}
Expand Down

0 comments on commit 1b2e22e

Please sign in to comment.