Refactor index.html loading

This commit is contained in:
2025-07-27 01:00:20 +03:00
parent cd62c5652d
commit d9b86584ef

View File

@@ -2,6 +2,7 @@ package com.pischule.memevizor.upload
import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.boot.context.event.ApplicationStartedEvent import org.springframework.boot.context.event.ApplicationStartedEvent
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Configuration
import org.springframework.context.event.EventListener import org.springframework.context.event.EventListener
import org.springframework.scheduling.annotation.Async import org.springframework.scheduling.annotation.Async
@@ -9,22 +10,19 @@ import org.springframework.scheduling.annotation.Async
private val logger = KotlinLogging.logger {} private val logger = KotlinLogging.logger {}
@Configuration @Configuration
class IndexInitializer(val fileUploaderService: FileUploaderService) { class IndexInitializer(val fileUploaderService: FileUploaderService, val ctx: ApplicationContext) {
@Async @Async
@EventListener @EventListener(ApplicationStartedEvent::class)
fun applicationStartedHandler(event: ApplicationStartedEvent) { fun applicationStartedHandler() {
try { try {
val fileBytes = readResourceAsByteArray("static/index.html") val fileBytes = getIndexHtmlBytes()
fileUploaderService.uploadFile(fileBytes, "index.html", "text/html") fileUploaderService.uploadFile(fileBytes, "index.html", "text/html")
} catch (e: Exception) { } catch (e: Exception) {
logger.warn(e) { "Failed to upload " } logger.warn(e) { "Failed to upload " }
} }
} }
private fun readResourceAsByteArray(resourcePath: String): ByteArray { private fun getIndexHtmlBytes(): ByteArray =
val inputStream = ctx.getResource("classpath:static/index.html").inputStream.use { it.readAllBytes() }
ClassLoader.getSystemResourceAsStream(resourcePath) ?: error("$resourcePath not found")
return inputStream.use { it.readAllBytes() }
}
} }