From 7e5aa0628c7c01df8b751cddc5d822287c7da4ed Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Sun, 19 Dec 2021 18:29:22 +0100 Subject: [PATCH] fix .jpg not being detected (#458) * Fix .jpg not being detected properly (Fix #455) * Fix compiler errors --- rest/src/main/kotlin/Image.kt | 38 +++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/rest/src/main/kotlin/Image.kt b/rest/src/main/kotlin/Image.kt index 6eadec7ba2f6..8d22263cb1f9 100644 --- a/rest/src/main/kotlin/Image.kt +++ b/rest/src/main/kotlin/Image.kt @@ -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.* @@ -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 { @@ -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) { + 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 get() = setOf( - JPEG, - PNG , - WEBP, - GIF , + val values: Set + 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