From 9e5b31812000c44883b78a4975b6d1fa3b02b4b5 Mon Sep 17 00:00:00 2001 From: Issac Date: Sat, 28 Mar 2026 11:01:39 +0530 Subject: [PATCH] Fix: Select highest quality media by comparing resolution instead of assuming array order --- .../core/wrapper/impl/media/MediaInfo.kt | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/core/src/main/kotlin/me/eternal/purrfectsnap/core/wrapper/impl/media/MediaInfo.kt b/core/src/main/kotlin/me/eternal/purrfectsnap/core/wrapper/impl/media/MediaInfo.kt index 5f30021a..59b4d125 100644 --- a/core/src/main/kotlin/me/eternal/purrfectsnap/core/wrapper/impl/media/MediaInfo.kt +++ b/core/src/main/kotlin/me/eternal/purrfectsnap/core/wrapper/impl/media/MediaInfo.kt @@ -19,7 +19,36 @@ class MediaInfo(obj: Any?) : AbstractWrapper(obj) { if (it.isEmpty()) { throw RuntimeException("MediaInfo is empty") } - instance = it[0]!! + + // Select highest quality media by comparing width * height + // Use explicit field name search to avoid relying on field order + instance = it.filterNotNull().maxByOrNull { mediaObj -> + runCatching { + val fields = mediaObj.javaClass.fields + + // Search for width and height fields by name (case-insensitive) + // Common patterns: "width", "mWidth", "height", "mHeight" + val widthField = fields.find { f -> + f.name.equals("width", ignoreCase = true) || + f.name.equals("mWidth", ignoreCase = true) + } + val heightField = fields.find { f -> + f.name.equals("height", ignoreCase = true) || + f.name.equals("mHeight", ignoreCase = true) + } + + // Validate fields exist and are integers before calculating resolution + if (widthField != null && heightField != null && + (widthField.type == Int::class.javaPrimitiveType || widthField.type == Int::class.java) && + (heightField.type == Int::class.javaPrimitiveType || heightField.type == Int::class.java)) { + widthField.isAccessible = true + heightField.isAccessible = true + widthField.getInt(mediaObj) * heightField.getInt(mediaObj) + } else { + 0 + } + }.getOrDefault(0) + } ?: it.filterNotNull().firstOrNull() ?: it.firstOrNull() } } }