mirror of
https://github.com/pischule/memevizor.git
synced 2025-12-19 06:56:42 +00:00
Upload index.html on app startup
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@@ -40,4 +40,7 @@ out/
|
|||||||
.kotlin
|
.kotlin
|
||||||
|
|
||||||
|
|
||||||
src/main/resources/application-local.properties
|
src/main/resources/application-local.properties
|
||||||
|
|
||||||
|
src/main/resources/static
|
||||||
|
!src/main/resources/static/index.html
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import com.github.kotlintelegrambot.dispatcher.handlers.MessageHandlerEnvironmen
|
|||||||
import com.github.kotlintelegrambot.entities.ChatId
|
import com.github.kotlintelegrambot.entities.ChatId
|
||||||
import com.github.kotlintelegrambot.entities.reaction.ReactionType
|
import com.github.kotlintelegrambot.entities.reaction.ReactionType
|
||||||
import com.pischule.memestv.bot.BotProps
|
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 com.pischule.memestv.util.getMaxResPhotoId
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.github.oshai.kotlinlogging.withLoggingContext
|
import io.github.oshai.kotlinlogging.withLoggingContext
|
||||||
@@ -28,8 +28,7 @@ class ThisCommandHandlerService(
|
|||||||
val fileBytes = env.bot.downloadFileBytes(maxResPhotoId) ?: return
|
val fileBytes = env.bot.downloadFileBytes(maxResPhotoId) ?: return
|
||||||
logger.info { "Downloaded a file from Telegram" }
|
logger.info { "Downloaded a file from Telegram" }
|
||||||
|
|
||||||
fileUploaderService.uploadFile(fileBytes)
|
fileUploaderService.uploadFile(fileBytes, "_.jpeg")
|
||||||
logger.info { "Uploaded a file to S3" }
|
|
||||||
|
|
||||||
reactToMessage(env, "👍")
|
reactToMessage(env, "👍")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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" }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.pischule.memestv.upload
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
|
@Service
|
||||||
|
interface FileUploaderService {
|
||||||
|
suspend fun uploadFile(fileBytes: ByteArray, filename: String)
|
||||||
|
}
|
||||||
@@ -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() }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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.runtime.auth.credentials.StaticCredentialsProvider
|
||||||
import aws.sdk.kotlin.services.s3.S3Client
|
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.boot.context.properties.EnableConfigurationProperties
|
||||||
import org.springframework.context.annotation.Bean
|
import org.springframework.context.annotation.Bean
|
||||||
import org.springframework.context.annotation.Configuration
|
import org.springframework.context.annotation.Configuration
|
||||||
|
import org.springframework.context.annotation.Profile
|
||||||
|
|
||||||
|
@Profile("!local")
|
||||||
@EnableConfigurationProperties(S3Props::class)
|
@EnableConfigurationProperties(S3Props::class)
|
||||||
@Configuration
|
@Configuration
|
||||||
class S3Config {
|
class S3Config {
|
||||||
@@ -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" }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.pischule.memestv.s3
|
package com.pischule.memestv.upload
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||||
|
|
||||||
Reference in New Issue
Block a user