Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Ktor's Base64 functions #600

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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