Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Removed BufferedImage assumption and provided parameters to ByteArray… #564

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 1 addition & 7 deletions api/kweb-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -1733,17 +1733,11 @@ public final class kweb/plugins/fomanticUI/JqueryKt {
}

public final class kweb/plugins/image/DynamicImagePlugin : kweb/plugins/KwebPlugin {
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlin/jvm/functions/Function2;)V
public fun <init> (Ljava/lang/String;Ljava/util/Map;)V
public fun <init> (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;)V
public fun appServerConfigurator (Lio/ktor/server/routing/Routing;)V
}

public final class kweb/plugins/image/DynamicImagePluginKt {
public static final fun toByteArray (Ljava/awt/image/BufferedImage;Ljava/lang/String;)[B
}

public final class kweb/plugins/javascript/JavascriptPlugin : kweb/plugins/KwebPlugin {
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/util/Set;)V
Expand Down
24 changes: 4 additions & 20 deletions src/main/kotlin/kweb/plugins/image/DynamicImagePlugin.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package kweb.plugins.image

import io.ktor.http.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kweb.plugins.KwebPlugin
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
import javax.imageio.ImageIO

/**
* Add multiple dynamically generated images files to your Kweb app routing using ByteArray generator functions.
Expand All @@ -17,32 +15,18 @@ import javax.imageio.ImageIO
*/
class DynamicImagePlugin(
private val resourceFolder: String,
private val fileNames: Map<String,
suspend () -> ByteArray>
private val fileNames: Map<String, suspend (Parameters) -> ByteArray>,
): KwebPlugin() {
constructor(resourceFolder: String, fileName: String, byteProvider: suspend () -> ByteArray):
constructor(resourceFolder: String, fileName: String, byteProvider: suspend (Parameters) -> ByteArray):
this(resourceFolder, mapOf(fileName to byteProvider))

constructor(resourceFolder: String, fileNames: Map<String, suspend () -> BufferedImage>, format: String):
this(resourceFolder, fileNames
.mapValues { (_, value) -> suspend { toByteArray(value(), format) }})

constructor(resourceFolder: String, fileName: String, bufferedImageProvider: suspend () -> BufferedImage, format: String):
this(resourceFolder, mapOf(fileName to bufferedImageProvider), format)

override fun appServerConfigurator(routeHandler: Routing) {
fileNames.forEach { (fileName, byteProvider) ->
routeHandler.get("$resourceFolder/$fileName") { _ ->
context.respondBytes {
byteProvider()
byteProvider(context.parameters)
}
}
}
}
}

fun toByteArray(bufferedImage: BufferedImage, format: String): ByteArray {
val byteArrayOutputStream = ByteArrayOutputStream()
ImageIO.write(bufferedImage, format, byteArrayOutputStream)
return byteArrayOutputStream.toByteArray()
}