Skip to content

Commit

Permalink
Optimize profile generation
Browse files Browse the repository at this point in the history
  • Loading branch information
WinG4merBR committed Jan 9, 2025
1 parent 2c701ed commit f61f7c7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.cakeyfox.foxy.command.vanilla.social

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.cakeyfox.common.Colors
import net.cakeyfox.common.FoxyEmotes
import net.cakeyfox.foxy.command.FoxyInteractionContext
Expand Down Expand Up @@ -38,7 +40,10 @@ class ProfileViewExecutor: FoxyCommandExecutor() {
return
}

val profile = ProfileRender(ProfileConfig(1436, 884), context).create(user, userData)
val profile = withContext(Dispatchers.IO) {
ProfileRender(ProfileConfig(1436, 884), context).create(user, userData)
}

val file = FileUpload.fromData(profile, "profile.png")

context.reply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import net.cakeyfox.foxy.command.FoxyInteractionContext
import net.cakeyfox.foxy.command.structure.FoxyCommandExecutor

class TopCakesExecutor : FoxyCommandExecutor() {
// TODO: Optimize this

override suspend fun execute(context: FoxyInteractionContext) {
context.defer()

Expand Down
48 changes: 32 additions & 16 deletions foxy/src/main/kotlin/net/cakeyfox/foxy/utils/image/ImageUtils.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.cakeyfox.foxy.utils.image

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import kotlinx.coroutines.*
import mu.KotlinLogging
import net.cakeyfox.foxy.utils.profile.ProfileCacheManager
import net.cakeyfox.serializable.data.ImagePosition
Expand All @@ -10,21 +13,21 @@ import java.awt.Font
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.io.InputStream
import java.net.URL
import javax.imageio.ImageIO
import kotlin.reflect.jvm.jvmName

object ImageUtils {
private val logger = KotlinLogging.logger(this::class.jvmName)
private val client = HttpClient(CIO)

fun getFont(fontName: String, fontSize: Int): Font? {
private fun getFont(fontName: String, fontSize: Int): Font? {
val fontStream: InputStream? = this::class.java.classLoader.getResourceAsStream("fonts/$fontName.ttf")

return if (fontStream != null) {
try {
Font.createFont(Font.TRUETYPE_FONT, fontStream).deriveFont(Font.PLAIN, fontSize.toFloat())
} catch (e: Exception) {
logger.error(e) { "Can't load font $fontName " }
logger.error(e) { "Can't load font $fontName" }
null
}
} else {
Expand All @@ -36,32 +39,45 @@ object ImageUtils {
suspend fun loadProfileAssetFromURL(url: String): BufferedImage {
return withContext(Dispatchers.IO) {
try {
ProfileCacheManager.imageCache.get(url) {
val cachedImage = ProfileCacheManager.imageCache.getIfPresent(url)

if (cachedImage != null) {
return@withContext cachedImage
}

val resultImage = try {
downloadImage(url)
} catch (e: Exception) {
logger.error(e) { "Error downloading image from $url" }
BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)
}

ProfileCacheManager.imageCache.put(url, resultImage)

resultImage
} catch (e: Exception) {
logger.error(e) { "Error loading image from $url" }
BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)
}
}
}

private fun downloadImage(url: String): BufferedImage {
try {
val connection = URL(url).openConnection().apply {
setRequestProperty("User-Agent", "Mozilla/5.0")
connectTimeout = 5000
readTimeout = 5000
}
private suspend fun downloadImage(url: String): BufferedImage {
return try {
val response = client.get(url)
val inputStream: InputStream = response.body()

return ImageIO.read(connection.inputStream)
withContext(Dispatchers.IO) {
ImageIO.read(inputStream) ?: BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)
}
} catch (e: Exception) {
logger.error(e) { "Error downloading image from $url" }
return BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)
BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)
} finally {
client.close()
}
}


fun Graphics2D.drawTextWithFont(width: Int, height: Int, textConfig: TextConfig.() -> Unit) {
val config = TextConfig().apply(textConfig)
this.font = getFont(config.fontFamily, config.fontSize) ?: Font("SansSerif", Font.PLAIN, config.fontSize)
Expand Down

0 comments on commit f61f7c7

Please sign in to comment.