Skip to content

Commit

Permalink
fix .jpg not being detected (#458)
Browse files Browse the repository at this point in the history
* Fix .jpg not being detected properly (Fix #455)

* Fix compiler errors
  • Loading branch information
DRSchlaubi authored Dec 19, 2021
1 parent bbb1c35 commit 7e5aa06
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions rest/src/main/kotlin/Image.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.kord.rest

import io.ktor.client.HttpClient
import io.ktor.client.request.request
import io.ktor.client.statement.HttpResponse
import io.ktor.http.HttpMethod
import io.ktor.util.toByteArray
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.*
import kotlinx.coroutines.Dispatchers
import java.util.*

Expand All @@ -14,7 +14,7 @@ class Image private constructor(val data: ByteArray, val format: Format) {
val dataUri: String
get() {
val hash = Base64.getEncoder().encodeToString(data)
return "data:image/${format.extension};base64,$hash"
return "data:image/${format.extensions.first()};base64,$hash"
}

companion object {
Expand All @@ -34,23 +34,31 @@ class Image private constructor(val data: ByteArray, val format: Format) {
}
}

sealed class Format(val extension: String) {
object JPEG : Format("jpeg")
sealed class Format(val extensions: List<String>) {
constructor(vararg extensions: String) : this(extensions.toList())

val extension: String get() = extensions.first()

object JPEG : Format("jpeg", "jpg")
object PNG : Format("png")
object WEBP : Format("webp")
object GIF : Format("gif")

companion object {
val values: Set<Format> get() = setOf(
JPEG,
PNG ,
WEBP,
GIF ,
val values: Set<Format>
get() = setOf(
JPEG,
PNG,
WEBP,
GIF,
)

)
fun isSupported(fileName: String): Boolean {
return values.any { fileName.endsWith(it.extension, true) }
return values.any {
it.extensions.any { extension -> fileName.endsWith(extension, true) }
}
}

fun fromContentType(type: String) = when (type) {
"image/jpeg" -> JPEG
"image/png" -> PNG
Expand Down

0 comments on commit 7e5aa06

Please sign in to comment.