android, desktop: catching decoding errors (#3314)

This commit is contained in:
Stanislav Dmitrenko authored and GitHub committed 2023-11-04 17:21:29 +00:00
1 parent 10cbb13c26
commit eee233bd02
8 files changed
+100 -61

No files matched your search

@@ -178,11 +178,13 @@ fun MutableState<ComposeState>.processPickedFile(uri: URI?, text: String?) {
if (fileName != null) {
value = value.copy(message = text ?: value.message, preview = ComposePreview.FilePreview(fileName, uri))
}
} else {
} else if (fileSize != null) {
AlertManager.shared.showAlertMsg(
generalGetString(MR.strings.large_file),
String.format(generalGetString(MR.strings.maximum_supported_file_size), formatBytes(maxFileSize))
)
} else {
showWrongUriAlert()
}
}
}
@@ -196,7 +198,8 @@ suspend fun MutableState<ComposeState>.processPickedMedia(uris: List<URI>, text:
isImage(uri) -> {
// Image
val drawable = getDrawableFromUri(uri)
bitmap = getBitmapFromUri(uri)
// Do not show alert in case it's already shown from the function above
bitmap = getBitmapFromUri(uri, withAlertOnException = AlertManager.shared.alertViews.isEmpty())
if (isAnimImage(uri, drawable)) {
// It's a gif or webp
val fileSize = getFileSize(uri)
@@ -209,13 +212,13 @@ suspend fun MutableState<ComposeState>.processPickedMedia(uris: List<URI>, text:
String.format(generalGetString(MR.strings.maximum_supported_file_size), formatBytes(maxFileSize))
)
}
} else {
} else if (bitmap != null) {
content.add(UploadContent.SimpleImage(uri))
}
}
else -> {
// Video
val res = getBitmapFromVideo(uri)
val res = getBitmapFromVideo(uri, withAlertOnException = true)
bitmap = res.preview
val durationMs = res.duration
content.add(UploadContent.Video(uri, durationMs?.div(1000)?.toInt() ?: 0))
@@ -151,7 +151,7 @@ fun saveAnimImage(uri: URI, encrypted: Boolean): CryptoFile? {
expect suspend fun saveTempImageUncompressed(image: ImageBitmap, asPng: Boolean): File?
fun saveFileFromUri(uri: URI, encrypted: Boolean): CryptoFile? {
fun saveFileFromUri(uri: URI, encrypted: Boolean, withAlertOnException: Boolean = true): CryptoFile? {
return try {
val inputStream = uri.inputStream()
val fileToSave = getFileName(uri)
@@ -170,10 +170,14 @@ fun saveFileFromUri(uri: URI, encrypted: Boolean): CryptoFile? {
}
} else {
Log.e(TAG, "Util.kt saveFileFromUri null inputStream")
if (withAlertOnException) showWrongUriAlert()
null
}
} catch (e: Exception) {
Log.e(TAG, "Util.kt saveFileFromUri error: ${e.stackTraceToString()}")
if (withAlertOnException) showWrongUriAlert()
null
}
}
@@ -267,7 +271,28 @@ fun getMaxFileSize(fileProtocol: FileProtocol): Long {
}
}
expect suspend fun getBitmapFromVideo(uri: URI, timestamp: Long? = null, random: Boolean = true): VideoPlayerInterface.PreviewAndDuration
expect suspend fun getBitmapFromVideo(uri: URI, timestamp: Long? = null, random: Boolean = true, withAlertOnException: Boolean = true): VideoPlayerInterface.PreviewAndDuration
fun showWrongUriAlert() {
AlertManager.shared.showAlertMsg(
title = generalGetString(MR.strings.non_content_uri_alert_title),
text = generalGetString(MR.strings.non_content_uri_alert_text)
)
}
fun showImageDecodingException() {
AlertManager.shared.showAlertMsg(
title = generalGetString(MR.strings.image_decoding_exception_title),
text = generalGetString(MR.strings.image_decoding_exception_desc)
)
}
fun showVideoDecodingException() {
AlertManager.shared.showAlertMsg(
title = generalGetString(MR.strings.image_decoding_exception_title),
text = generalGetString(MR.strings.video_decoding_exception_desc)
)
}
fun Color.darker(factor: Float = 0.1f): Color =
Color(max(red * (1 - factor), 0f), max(green * (1 - factor), 0f), max(blue * (1 - factor), 0f), alpha)
@@ -309,6 +309,7 @@
<string name="videos_limit_desc">Only 10 videos can be sent at the same time</string>
<string name="image_decoding_exception_title">Decoding error</string>
<string name="image_decoding_exception_desc">The image cannot be decoded. Please, try a different image or contact developers.</string>
<string name="video_decoding_exception_desc">The video cannot be decoded. Please, try a different video or contact developers.</string>
<string name="you_are_observer">you are observer</string>
<string name="observer_cant_send_message_title">You can\'t send messages!</string>
<string name="observer_cant_send_message_desc">Please contact group admin.</string>