From 3086b5e01da9d8fbd8809e4e05a71057f326ec8e Mon Sep 17 00:00:00 2001
From: Taras Smakula <tarassmakula@gmail.com>
Date: Fri, 1 Dec 2023 16:34:23 +0200
Subject: [PATCH] Fix video orientation in preview

---
 .../circles/core/extensions/UriExtensions.kt  |  3 +--
 .../org/futo/circles/core/utils/ImageUtils.kt | 19 ++++++++++++++++++-
 .../org/futo/circles/core/utils/MediaUtils.kt | 19 +------------------
 .../org/futo/circles/core/utils/VideoUtils.kt |  6 +++++-
 4 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/core/src/main/java/org/futo/circles/core/extensions/UriExtensions.kt b/core/src/main/java/org/futo/circles/core/extensions/UriExtensions.kt
index a3c1da01d..acef6e4e7 100644
--- a/core/src/main/java/org/futo/circles/core/extensions/UriExtensions.kt
+++ b/core/src/main/java/org/futo/circles/core/extensions/UriExtensions.kt
@@ -9,7 +9,6 @@ import androidx.core.database.getStringOrNull
 import org.futo.circles.core.feature.blurhash.ThumbHash
 import org.futo.circles.core.model.MediaAttachmentInfo
 import org.futo.circles.core.utils.ImageUtils
-import org.futo.circles.core.utils.MediaUtils.getOrientation
 import org.futo.circles.core.utils.VideoUtils
 import org.matrix.android.sdk.api.session.content.ContentAttachmentData
 
@@ -36,7 +35,7 @@ fun Uri.getFilename(context: Context): String? {
 suspend fun Uri.toImageContentAttachmentData(context: Context): ContentAttachmentData? {
     val attachmentInfo = getMediaAttachmentInfo(context) ?: return null
     val resolution = ImageUtils.getImageResolution(context, this)
-    val orientation = getOrientation(context, this)
+    val orientation = ImageUtils.getImageOrientation(context, this)
     return ContentAttachmentData(
         mimeType = attachmentInfo.mimeType,
         type = ContentAttachmentData.Type.IMAGE,
diff --git a/core/src/main/java/org/futo/circles/core/utils/ImageUtils.kt b/core/src/main/java/org/futo/circles/core/utils/ImageUtils.kt
index e33ad6cab..9747deaa5 100644
--- a/core/src/main/java/org/futo/circles/core/utils/ImageUtils.kt
+++ b/core/src/main/java/org/futo/circles/core/utils/ImageUtils.kt
@@ -17,7 +17,24 @@ object ImageUtils {
             null,
             options
         )
-        return getSizeBasedOnOrientation(context, uri, options.outWidth, options.outHeight)
+        return getSizeBasedOnOrientation(
+            getImageOrientation(context, uri),
+            options.outWidth,
+            options.outHeight
+        )
+    }
+
+    fun getImageOrientation(context: Context, uri: Uri): Int {
+        var orientation = 0
+        context.contentResolver.openInputStream(uri)?.use { inputStream ->
+            try {
+                ExifInterface(inputStream).let {
+                    orientation = it.rotationDegrees
+                }
+            } catch (ignore: Exception) {
+            }
+        }
+        return orientation
     }
 
 }
\ No newline at end of file
diff --git a/core/src/main/java/org/futo/circles/core/utils/MediaUtils.kt b/core/src/main/java/org/futo/circles/core/utils/MediaUtils.kt
index 535e29870..c6af09e14 100644
--- a/core/src/main/java/org/futo/circles/core/utils/MediaUtils.kt
+++ b/core/src/main/java/org/futo/circles/core/utils/MediaUtils.kt
@@ -1,27 +1,10 @@
 package org.futo.circles.core.utils
 
-import android.content.Context
-import android.net.Uri
 import android.util.Size
-import androidx.exifinterface.media.ExifInterface
 
 object MediaUtils {
 
-    fun getOrientation(context: Context, uri: Uri): Int {
-        var orientation = 0
-        context.contentResolver.openInputStream(uri)?.use { inputStream ->
-            try {
-                ExifInterface(inputStream).let {
-                    orientation = it.rotationDegrees
-                }
-            } catch (ignore: Exception) {
-            }
-        }
-        return orientation
-    }
-
-    fun getSizeBasedOnOrientation(context: Context, uri: Uri, width: Int, height: Int): Size {
-        val orientation = getOrientation(context, uri)
+    fun getSizeBasedOnOrientation(orientation: Int, width: Int, height: Int): Size {
         return if (orientation == 90 || orientation == 270) Size(height, width)
         else Size(width, height)
     }
diff --git a/core/src/main/java/org/futo/circles/core/utils/VideoUtils.kt b/core/src/main/java/org/futo/circles/core/utils/VideoUtils.kt
index 809e3dad4..ec4f465c5 100644
--- a/core/src/main/java/org/futo/circles/core/utils/VideoUtils.kt
+++ b/core/src/main/java/org/futo/circles/core/utils/VideoUtils.kt
@@ -5,6 +5,7 @@ import android.media.MediaMetadataRetriever
 import android.net.Uri
 import android.util.Size
 import org.futo.circles.core.utils.MediaUtils.getSizeBasedOnOrientation
+import org.matrix.android.sdk.api.extensions.tryOrNull
 import java.util.concurrent.TimeUnit
 
 
@@ -25,9 +26,12 @@ object VideoUtils {
         retriever.setDataSource(context, uri)
         val height = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
         val width = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
+        val orientation = tryOrNull {
+            retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)?.toInt()
+        } ?: 0
         retriever.release()
         getSizeBasedOnOrientation(
-            context, uri,
+            orientation,
             width?.toInt() ?: 0,
             height?.toInt() ?: 0
         )
-- 
GitLab