Skip to content

Commit

Permalink
Use ktor Base64 functions (#600)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukellmann authored Apr 28, 2022
1 parent 8e1a41c commit 4f7857e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
14 changes: 5 additions & 9 deletions core/src/main/kotlin/builder/kord/KordBuilderUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.websocket.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.util.*
import kotlinx.serialization.json.Json
import java.util.*

internal fun HttpClientConfig<*>.defaultConfig() {
expectSuccess = false
Expand Down Expand Up @@ -39,12 +39,8 @@ internal fun HttpClient?.configure(): HttpClient {
}


internal fun getBotIdFromToken(token: String): Snowflake {
try {
val bytes = Base64.getDecoder().decode(token.split(""".""").first())
return Snowflake(String(bytes))
} catch (exception: IllegalArgumentException) {
throw IllegalArgumentException("Malformed bot token: '$token'. Make sure that your token is correct.")
}
internal fun getBotIdFromToken(token: String) = try {
Snowflake(token.substringBefore('.').decodeBase64String())
} catch (exception: IllegalArgumentException) {
throw IllegalArgumentException("Malformed bot token: '$token'. Make sure that your token is correct.")
}

8 changes: 2 additions & 6 deletions rest/src/main/kotlin/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@ package dev.kord.rest
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.util.*
import kotlinx.coroutines.Dispatchers
import java.util.*

public class Image private constructor(public val data: ByteArray, public val format: Format) {

public val dataUri: String
get() {
val hash = Base64.getEncoder().encodeToString(data)
return "data:image/${format.extensions.first()};base64,$hash"
}
public val dataUri: String get() = "data:image/${format.extensions.first()};base64,${data.encodeBase64()}"

public companion object {
public fun raw(data: ByteArray, format: Format): Image {
Expand Down

0 comments on commit 4f7857e

Please sign in to comment.