Skip to content

Commit

Permalink
Add route to access sound files
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed May 16, 2023
1 parent 52fc12c commit 26d7b4d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 15 deletions.
4 changes: 1 addition & 3 deletions .idea/artifacts/common_js_1_0_SNAPSHOT.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions .idea/artifacts/common_jvm_1_0_SNAPSHOT.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/FileServer.kt
Original file line number Diff line number Diff line change
@@ -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<Sounds.Sound.Audio> { (id) ->
val sound = SoundBoardDatabase.sounds.findOneById(ObjectId(id))
?: soundNotFound()

val path = Config.SOUNDS_FOLDER / sound.fileName

call.respondFile(path.toFile())
}
}
15 changes: 15 additions & 0 deletions bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/Ktor.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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")
6 changes: 3 additions & 3 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions common/src/commonMain/kotlin/dev/schlaubi/tonbrett/common/Route.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

0 comments on commit 26d7b4d

Please sign in to comment.