Skip to content

Commit

Permalink
feat: basic image manipulation (?resize, ?format) on images
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed May 7, 2022
1 parent b26a74b commit 9049ff1
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 78 deletions.
11 changes: 9 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ plugins {
apply(plugin = "kotlinx-atomicfu")

val JAVA_VERSION = JavaVersion.VERSION_17
val VERSION = Version(1, 1, 1, 0, ReleaseType.None)
val VERSION = Version(1, 2, 0, 0, ReleaseType.None)
val COMMIT_HASH by lazy {
val cmd = "git rev-parse --short HEAD".split("\\s".toRegex())
val proc = ProcessBuilder(cmd)
Expand All @@ -70,7 +70,7 @@ println("|> hazel: $VERSION ($COMMIT_HASH)")
println("|> Java: $JAVA_VERSION (JVM: ${System.getProperty("java.version", "Unknown")} [${System.getProperty("java.vendor", "Unknown")}] | JRE: ${Runtime.version()})")
println("+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+")

group = "dev.floofy"
group = "dev.floofy.hazel"
version = "$VERSION"

repositories {
Expand All @@ -89,6 +89,7 @@ dependencies {
// BOMs
api(platform("org.jetbrains.kotlinx:kotlinx-serialization-bom:1.3.2"))
api(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.1"))
api(platform("org.noelware.ktor:ktor-routing-bom:0.1-beta"))
testImplementation(platform("io.kotest:kotest-bom:5.3.0"))
api(platform("org.noelware.remi:remi-bom:0.1.4-beta.3"))
api(platform("dev.floofy.commons:commons-bom:2.1.0.1"))
Expand All @@ -106,6 +107,8 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.3")

// Noel Utilities
implementation("dev.floofy.commons:extensions-kotlin")
implementation("dev.floofy.commons:extensions-koin")
implementation("dev.floofy.commons:slf4j")

// Apache Utilities
Expand Down Expand Up @@ -149,6 +152,10 @@ dependencies {
implementation("org.noelware.remi:remi-support-fs")
api("org.noelware.remi:remi-core")

// Ktor Routing
implementation("org.noelware.ktor:core")
implementation("org.noelware.ktor:loader-koin")

// TOML
implementation("com.akuleshov7:ktoml-core:0.2.11")
implementation("com.akuleshov7:ktoml-file:0.2.11")
Expand Down
12 changes: 5 additions & 7 deletions src/main/kotlin/dev/floofy/hazel/Hazel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dev.floofy.hazel.plugins.KtorLoggingPlugin
import dev.floofy.hazel.plugins.UserAgentPlugin
import dev.floofy.hazel.routing.AbstractEndpoint
import dev.floofy.hazel.routing.createCdnEndpoints
import dev.floofy.utils.koin.retrieve
import dev.floofy.utils.slf4j.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
Expand All @@ -35,15 +36,14 @@ import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.autohead.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.plugins.defaultheaders.*
import io.ktor.server.plugins.statuspages.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.sentry.Sentry
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.buildJsonArray
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
Expand Down Expand Up @@ -88,14 +88,14 @@ class Hazel {
val ticker = Ticker("update image routing", 1.minutes.inWholeMilliseconds)

ticker.launch {
log.info("Updating routes...")
log.debug("Updating routes...")

val routing = server.application.plugin(Routing)
routing.createCdnEndpoints()
}

val environment = applicationEngineEnvironment {
developmentMode = false
developmentMode = System.getProperty("dev.floofy.hazel.debug", "false") == "true"
log = LoggerFactory.getLogger("dev.floofy.hazel.ktor.KtorApplicationEnvironment")

connector {
Expand All @@ -104,13 +104,11 @@ class Hazel {
}

module {
val json: Json by inject()

install(AutoHeadResponse)
install(KtorLoggingPlugin)
install(UserAgentPlugin)
install(ContentNegotiation) {
this.json(json)
json(GlobalContext.retrieve())
}

install(CORS) {
Expand Down
46 changes: 46 additions & 0 deletions src/main/kotlin/dev/floofy/hazel/core/ImageManipulator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 🪶 hazel: Minimal, simple, and open source content delivery network made in Kotlin
* Copyright 2022 Noel <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.floofy.hazel.core

import java.awt.Image
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import javax.imageio.ImageIO

object ImageManipulator {
fun resize(stream: ByteArrayInputStream, format: String, width: Int, height: Int): ByteArray {
val buffImage = ImageIO.read(stream)
val resulting = buffImage.getScaledInstance(width, height, Image.SCALE_DEFAULT)
val output = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
output.graphics.drawImage(resulting, 0, 0, null)

val baos = ByteArrayOutputStream()
ImageIO.write(output, format, baos)

return baos.toByteArray()
}

fun format(stream: ByteArrayInputStream, to: String): ByteArray {
val bufferedImage = ImageIO.read(stream)
val out = ByteArrayOutputStream()

ImageIO.write(bufferedImage, to, out)
return out.toByteArray()
}
}
13 changes: 12 additions & 1 deletion src/main/kotlin/dev/floofy/hazel/core/StorageWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import java.io.InputStream
class StorageWrapper(config: StorageConfig) {
val trailer: StorageTrailer<*>
private val log by logging<StorageWrapper>()
val listCache = mutableListOf<org.noelware.remi.core.Object>()

init {
log.info("Figuring out what storage trailer to use...")
Expand Down Expand Up @@ -112,6 +113,16 @@ class StorageWrapper(config: StorageConfig) {
contentType: String = "application/octet-stream"
): Boolean = trailer.upload(path, stream, contentType)

suspend fun listAll(): List<org.noelware.remi.core.Object> = trailer.listAll()
suspend fun listAll(force: Boolean = true): List<org.noelware.remi.core.Object> =
if (force || listCache.isEmpty()) {
val c = trailer.listAll()
listCache.clear()
listCache += c

listCache
} else {
listCache
}

fun <I: InputStream> findContentType(stream: I): String = trailer.figureContentType(stream)
}
Loading

0 comments on commit 9049ff1

Please sign in to comment.