From d9b86584ef227fae9a82ea5f2252cd1c5fdabd0c Mon Sep 17 00:00:00 2001 From: Maksim Pischulenok Date: Sun, 27 Jul 2025 01:00:20 +0300 Subject: [PATCH] Refactor index.html loading --- .../memevizor/upload/IndexInitializer.kt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/pischule/memevizor/upload/IndexInitializer.kt b/src/main/kotlin/com/pischule/memevizor/upload/IndexInitializer.kt index b588189..db2eedb 100644 --- a/src/main/kotlin/com/pischule/memevizor/upload/IndexInitializer.kt +++ b/src/main/kotlin/com/pischule/memevizor/upload/IndexInitializer.kt @@ -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() } }