Upload index.html on app startup

This commit is contained in:
2025-06-08 23:35:06 +03:00
parent b9adcdada1
commit db9c0b58d1
9 changed files with 91 additions and 26 deletions

View File

@@ -4,7 +4,7 @@ import com.github.kotlintelegrambot.dispatcher.handlers.MessageHandlerEnvironmen
import com.github.kotlintelegrambot.entities.ChatId
import com.github.kotlintelegrambot.entities.reaction.ReactionType
import com.pischule.memestv.bot.BotProps
import com.pischule.memestv.s3.FileUploaderService
import com.pischule.memestv.upload.FileUploaderService
import com.pischule.memestv.util.getMaxResPhotoId
import io.github.oshai.kotlinlogging.KotlinLogging
import io.github.oshai.kotlinlogging.withLoggingContext
@@ -28,8 +28,7 @@ class ThisCommandHandlerService(
val fileBytes = env.bot.downloadFileBytes(maxResPhotoId) ?: return
logger.info { "Downloaded a file from Telegram" }
fileUploaderService.uploadFile(fileBytes)
logger.info { "Uploaded a file to S3" }
fileUploaderService.uploadFile(fileBytes, "_.jpeg")
reactToMessage(env, "👍")
}

View File

@@ -1,20 +0,0 @@
package com.pischule.memestv.s3
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.smithy.kotlin.runtime.content.ByteStream
import org.springframework.stereotype.Service
@Service
class FileUploaderService(private val s3Client: S3Client, private val s3Props: S3Props) {
suspend fun uploadFile(fileBytes: ByteArray) {
s3Client.putObject(
PutObjectRequest {
body = ByteStream.fromBytes(fileBytes)
bucket = s3Props.bucket
key = "_.jpeg"
}
)
}
}

View File

@@ -0,0 +1,16 @@
package com.pischule.memestv.upload
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.context.annotation.Profile
import org.springframework.stereotype.Service
private val logger = KotlinLogging.logger {}
@Profile("local")
@Service
class DummyFileUploadService() : FileUploaderService {
override suspend fun uploadFile(fileBytes: ByteArray, filename: String) {
logger.info { "File $filename has been successfully uploaded to nowhere" }
}
}

View File

@@ -0,0 +1,8 @@
package com.pischule.memestv.upload
import org.springframework.stereotype.Service
@Service
interface FileUploaderService {
suspend fun uploadFile(fileBytes: ByteArray, filename: String)
}

View File

@@ -0,0 +1,31 @@
package com.pischule.memestv.upload
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.runBlocking
import org.springframework.boot.context.event.ApplicationStartedEvent
import org.springframework.context.annotation.Configuration
import org.springframework.context.event.EventListener
import org.springframework.scheduling.annotation.Async
private val logger = KotlinLogging.logger {}
@Configuration
class IndexInitializer(val fileUploaderService: FileUploaderService) {
@Async
@EventListener
fun applicationStartedHandler(event: ApplicationStartedEvent) {
try {
val fileBytes = readResourceAsByteArray("static/index.html")
runBlocking { fileUploaderService.uploadFile(fileBytes, "index.html") }
} catch (e: Error) {
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() }
}
}

View File

@@ -1,4 +1,4 @@
package com.pischule.memestv.s3
package com.pischule.memestv.upload
import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider
import aws.sdk.kotlin.services.s3.S3Client
@@ -6,7 +6,9 @@ import aws.smithy.kotlin.runtime.net.url.Url
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
@Profile("!local")
@EnableConfigurationProperties(S3Props::class)
@Configuration
class S3Config {

View File

@@ -0,0 +1,26 @@
package com.pischule.memestv.upload
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.smithy.kotlin.runtime.content.ByteStream
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
import org.springframework.stereotype.Service
private val logger = KotlinLogging.logger {}
@ConditionalOnBean(S3Config::class)
@Service
class S3FileUploaderService(private val s3Client: S3Client, private val s3Props: S3Props) :
FileUploaderService {
override suspend fun uploadFile(fileBytes: ByteArray, filename: String) {
s3Client.putObject(
PutObjectRequest {
body = ByteStream.fromBytes(fileBytes)
bucket = s3Props.bucket
key = filename
}
)
logger.info { "File $filename has been uploaded to S3" }
}
}

View File

@@ -1,4 +1,4 @@
package com.pischule.memestv.s3
package com.pischule.memestv.upload
import org.springframework.boot.context.properties.ConfigurationProperties