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

fix .jpg not being detected #458

Merged
merged 2 commits into from
Dec 19, 2021
Merged
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
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