mirror of
https://github.com/pischule/memevizor.git
synced 2025-12-19 06:56:42 +00:00
Refactor index.html loading
This commit is contained in:
@@ -2,6 +2,7 @@ package com.pischule.memevizor.upload
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent
|
||||
import org.springframework.context.ApplicationContext
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.context.event.EventListener
|
||||
import org.springframework.scheduling.annotation.Async
|
||||
@@ -9,22 +10,19 @@ import org.springframework.scheduling.annotation.Async
|
||||
private val logger = KotlinLogging.logger {}
|
||||
|
||||
@Configuration
|
||||
class IndexInitializer(val fileUploaderService: FileUploaderService) {
|
||||
class IndexInitializer(val fileUploaderService: FileUploaderService, val ctx: ApplicationContext) {
|
||||
|
||||
@Async
|
||||
@EventListener
|
||||
fun applicationStartedHandler(event: ApplicationStartedEvent) {
|
||||
@EventListener(ApplicationStartedEvent::class)
|
||||
fun applicationStartedHandler() {
|
||||
try {
|
||||
val fileBytes = readResourceAsByteArray("static/index.html")
|
||||
val fileBytes = getIndexHtmlBytes()
|
||||
fileUploaderService.uploadFile(fileBytes, "index.html", "text/html")
|
||||
} catch (e: Exception) {
|
||||
logger.warn(e) { "Failed to upload " }
|
||||
}
|
||||
}
|
||||
|
||||
private fun readResourceAsByteArray(resourcePath: String): ByteArray {
|
||||
val inputStream =
|
||||
ClassLoader.getSystemResourceAsStream(resourcePath) ?: error("$resourcePath not found")
|
||||
return inputStream.use { it.readAllBytes() }
|
||||
}
|
||||
private fun getIndexHtmlBytes(): ByteArray =
|
||||
ctx.getResource("classpath:static/index.html").inputStream.use { it.readAllBytes() }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user