Skip to content

Commit bba4be4

Browse files
committed
fix (feat): Implement animated GIF / WEBP images
1 parent 8e0b417 commit bba4be4

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

app/src/main/kotlin/org/dokiteam/doki/core/util/ext/Coil.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,25 @@ fun SourceFetchResult.copyWithNewSource(): SourceFetchResult = SourceFetchResult
8282
mimeType = mimeType,
8383
dataSource = dataSource,
8484
)
85+
86+
fun String.isAnimatedWebP(): Boolean = runCatching {
87+
val client = okhttp3.OkHttpClient.Builder()
88+
.connectTimeout(2, java.util.concurrent.TimeUnit.SECONDS)
89+
.readTimeout(2, java.util.concurrent.TimeUnit.SECONDS)
90+
.build()
91+
92+
val request = okhttp3.Request.Builder()
93+
.url(this)
94+
.header("Range", "bytes=0-20")
95+
.build()
96+
97+
client.newCall(request).execute().use { response ->
98+
val bytes = response.body.bytes()
99+
100+
bytes.size >= 21 &&
101+
bytes[0] == 0x52.toByte() && bytes[3] == 0x46.toByte() &&
102+
bytes[8] == 0x57.toByte() && bytes[11] == 0x50.toByte() &&
103+
bytes[12] == 0x56.toByte() && bytes[15] == 0x58.toByte() &&
104+
(bytes[20].toInt() and 0x02) != 0
105+
}
106+
}.getOrDefault(false)

app/src/main/kotlin/org/dokiteam/doki/core/util/ext/String.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ fun <T> Collection<T>.joinToStringWithLimit(context: Context, limit: Int, transf
7272

7373
fun String.isHttpUrl() = startsWith("https://", ignoreCase = true) || startsWith("http://", ignoreCase = true)
7474

75-
fun String.isAnimatedImage() = substringBefore("?").run { endsWith(".gif", ignoreCase = true) || endsWith(".webp", ignoreCase = true) }
75+
fun String.isAnimatedImage() = when {
76+
contains(".gif", ignoreCase = true) -> true
77+
contains(".webp", ignoreCase = true) -> isAnimatedWebP()
78+
else -> false
79+
}
7680

7781
fun concatStrings(context: Context, a: String?, b: String?): String? = when {
7882
a.isNullOrEmpty() && b.isNullOrEmpty() -> null

0 commit comments

Comments
 (0)