This commit is contained in:
bxdxnn
2026-07-07 16:46:42 +00:00
parent c4c0c482f0
commit 4fd0be1fe5
2 changed files with 10 additions and 11 deletions
@@ -223,10 +223,10 @@ class AndroidMediaPreProcessor(
private suspend fun processSvgImage(uri: Uri, mimeType: String): MediaUploadInfo { private suspend fun processSvgImage(uri: Uri, mimeType: String): MediaUploadInfo {
Timber.d("Processing SVG image ${uri.path.orEmpty().hash()}") Timber.d("Processing SVG image ${uri.path.orEmpty().hash()}")
val file = copyToTmpFile(uri) val file = copyToTmpFile(uri)
val (width, height) = svgDimensionExtractor.extractDimensions(file) val size = svgDimensionExtractor.extractDimensions(file)
val imageInfo = ImageInfo( val imageInfo = ImageInfo(
width = width, width = size.width.toLong(),
height = height, height = size.height.toLong(),
mimetype = mimeType, mimetype = mimeType,
size = file.length(), size = file.length(),
thumbnailInfo = null, thumbnailInfo = null,
@@ -1,6 +1,5 @@
/* /*
* Copyright (c) 2025 Element Creations Ltd. * Copyright (c) 2026 Element Creations Ltd.
* Copyright 2023-2025 New Vector Ltd.
* *
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial. * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details. * Please see LICENSE files in the repository root for full details.
@@ -16,7 +15,7 @@ class SvgDimensionExtractor(
private val defaultWidth: Long = 640L, private val defaultWidth: Long = 640L,
private val defaultHeight: Long = 480L, private val defaultHeight: Long = 480L,
) { ) {
fun extractDimensions(file: File): Pair<Long, Long> { fun extractDimensions(file: File): android.util.Size {
return file.inputStream().use { inputStream -> return file.inputStream().use { inputStream ->
try { try {
val parser = Xml.newPullParser() val parser = Xml.newPullParser()
@@ -32,7 +31,7 @@ class SvgDimensionExtractor(
val parsedHeight = height?.let { parseLength(it) } val parsedHeight = height?.let { parseLength(it) }
if (parsedWidth != null && parsedHeight != null) { if (parsedWidth != null && parsedHeight != null) {
return parsedWidth to parsedHeight return android.util.Size(parsedWidth.toInt(), parsedHeight.toInt())
} }
if (viewBox != null) { if (viewBox != null) {
@@ -40,17 +39,17 @@ class SvgDimensionExtractor(
if (parts.size == 4 && parts[2] != null && parts[3] != null) { if (parts.size == 4 && parts[2] != null && parts[3] != null) {
val vbWidth = parts[2]!!.toLong().coerceAtLeast(1) val vbWidth = parts[2]!!.toLong().coerceAtLeast(1)
val vbHeight = parts[3]!!.toLong().coerceAtLeast(1) val vbHeight = parts[3]!!.toLong().coerceAtLeast(1)
return vbWidth to vbHeight return android.util.Size(vbWidth.toInt(), vbHeight.toInt())
} }
} }
return (parsedWidth ?: defaultWidth) to (parsedHeight ?: defaultHeight) return android.util.Size((parsedWidth ?: defaultWidth).toInt(), (parsedHeight ?: defaultHeight).toInt())
} }
eventType = parser.next() eventType = parser.next()
} }
defaultWidth to defaultHeight android.util.Size(defaultWidth.toInt(), defaultHeight.toInt())
} catch (_: Exception) { } catch (_: Exception) {
defaultWidth to defaultHeight android.util.Size(defaultWidth.toInt(), defaultHeight.toInt())
} }
} }
} }