diff --git a/.idea/artifacts/common_js_1_0_SNAPSHOT.xml b/.idea/artifacts/common_js_1_0_SNAPSHOT.xml index f4b6098..e803ecd 100644 --- a/.idea/artifacts/common_js_1_0_SNAPSHOT.xml +++ b/.idea/artifacts/common_js_1_0_SNAPSHOT.xml @@ -1,8 +1,6 @@ $PROJECT_DIR$/common/build/libs - - - + \ No newline at end of file diff --git a/.idea/artifacts/common_jvm_1_0_SNAPSHOT.xml b/.idea/artifacts/common_jvm_1_0_SNAPSHOT.xml index 6589c56..383f2c7 100644 --- a/.idea/artifacts/common_jvm_1_0_SNAPSHOT.xml +++ b/.idea/artifacts/common_jvm_1_0_SNAPSHOT.xml @@ -1,8 +1,6 @@ $PROJECT_DIR$/common/build/libs - - - + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/commands/AddSoundCommand.kt b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/commands/AddSoundCommand.kt index 7ae1ad4..49cecc0 100644 --- a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/commands/AddSoundCommand.kt +++ b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/commands/AddSoundCommand.kt @@ -17,9 +17,7 @@ import io.ktor.client.request.* import io.ktor.http.* import org.litote.kmongo.newId import java.nio.file.StandardOpenOption -import kotlin.io.path.absolutePathString -import kotlin.io.path.div -import kotlin.io.path.writeBytes +import kotlin.io.path.* class AddSoundCommandArgs : Arguments() { val sound by attachment { @@ -58,6 +56,10 @@ suspend fun Extension.addSoundCommand() = ephemeralSlashCommand(::AddSoundComman } val sound = Sound(id, arguments.name, user.id, arguments.description, null) val file = Config.SOUNDS_FOLDER / sound.fileName + val soundsFolder = file.parent + if(!soundsFolder.exists()) { + soundsFolder.createDirectories() + } file.writeBytes(cdnResponse.body(), StandardOpenOption.CREATE) SoundBoardDatabase.sounds.save(sound) diff --git a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/FileServer.kt b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/FileServer.kt index 955d731..607a17a 100644 --- a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/FileServer.kt +++ b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/FileServer.kt @@ -1,7 +1,23 @@ package dev.schlaubi.tonbrett.bot.server +import dev.schlaubi.tonbrett.bot.config.Config +import dev.schlaubi.tonbrett.bot.io.SoundBoardDatabase +import dev.schlaubi.tonbrett.bot.util.soundNotFound +import dev.schlaubi.tonbrett.common.Route.Sounds +import io.ktor.server.application.* +import io.ktor.server.resources.* +import io.ktor.server.response.* import io.ktor.server.routing.* +import org.bson.types.ObjectId +import kotlin.io.path.div fun Route.files() { + get { (id) -> + val sound = SoundBoardDatabase.sounds.findOneById(ObjectId(id)) + ?: soundNotFound() + val path = Config.SOUNDS_FOLDER / sound.fileName + + call.respondFile(path.toFile()) + } } diff --git a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/Ktor.kt b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/Ktor.kt new file mode 100644 index 0000000..802f6dc --- /dev/null +++ b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/Ktor.kt @@ -0,0 +1,15 @@ +package dev.schlaubi.tonbrett.bot.server + +import dev.schlaubi.mikbot.util_plugins.ktor.api.KtorExtensionPoint +import io.ktor.server.application.* +import io.ktor.server.routing.* +import org.pf4j.Extension + +@Extension +class Ktor : KtorExtensionPoint { + override fun Application.apply() { + routing { + files() + } + } +} diff --git a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/util/KtorUtil.kt b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/util/KtorUtil.kt new file mode 100644 index 0000000..5fc88b1 --- /dev/null +++ b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/util/KtorUtil.kt @@ -0,0 +1,6 @@ +package dev.schlaubi.tonbrett.bot.util + +import io.ktor.server.plugins.* + +fun notFound(message: String): Nothing = throw NotFoundException(message) +fun soundNotFound(): Nothing = notFound("Sound not found") diff --git a/common/build.gradle.kts b/common/build.gradle.kts index a305833..dd3ef0f 100644 --- a/common/build.gradle.kts +++ b/common/build.gradle.kts @@ -15,14 +15,14 @@ kotlin { commonMain { dependencies { api(libs.kotlinx.serialization) - api(libs.ktor.resources) + compileOnly(libs.ktor.resources) } } named("jvmMain") { dependencies { - implementation(libs.kmongo.id.serialization) - implementation(libs.kord.common) + compileOnly(libs.kmongo.id.serialization) + compileOnly(libs.kord.common) } } } diff --git a/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Route.kt b/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Route.kt index f3def07..20945de 100644 --- a/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Route.kt +++ b/common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Route.kt @@ -4,6 +4,16 @@ import io.ktor.resources.* @Resource("/soundboard") public class Route { - @Resource("audio/{audioId}") - public data class Audio(val audioId: String, val parent: Route = Route()) + @Resource("sounds") + public data class Sounds(val parent: Route = Route()) { + @Resource("{id}") + public data class Sound(val id: String, val parent: Sounds = Sounds()) { + @Resource("audio") + public class Audio(public val parent: Sound) { + public constructor(id: String) : this(Sound(id)) + + public operator fun component1(): String = parent.id + } + } + } }