From 17fb93626cb168af759582a615bf6b23a34c5d31 Mon Sep 17 00:00:00 2001 From: QuirkyPony <43992380+QuirkyPony@users.noreply.github.com> Date: Fri, 5 Aug 2022 18:55:13 +0200 Subject: [PATCH] adapt file size error messages to show real instance upload limit (#2630) * remove megabyte counts from file size error messages The file size limits depend on the server; change strings to reflect that. * show real file size limits instead of assuming Mastodon defaults * correct previous commit * correct previous commit (again) * remove megabyte counts from file size error messages The file size limits depend on the server; change strings to reflect that. * Translated using Weblate (Galician) Currently translated at 100.0% (489 of 489 strings) Co-authored-by: XoseM Translate-URL: https://weblate.tusky.app/projects/tusky/tusky/gl/ Translation: Tusky/Tusky * Translated using Weblate (Finnish) Currently translated at 5.5% (1 of 18 strings) Translation: Tusky/Tusky description Translate-URL: https://weblate.tusky.app/projects/tusky/tusky-app/fi/ * correct previous commit correct previous commit (again) fixed type error caused by previous commits * fix lint error... * Update strings.xml * improve code, calculate correct max size and format it Co-authored-by: Laura Co-authored-by: XoseM Co-authored-by: Konrad Pozniak --- .../tusky/components/compose/ComposeActivity.kt | 17 +++++++++++------ .../tusky/components/compose/MediaUploader.kt | 7 +++---- app/src/main/res/values/strings.xml | 5 ++--- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index 0ffe8f60..832eee81 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -103,6 +103,7 @@ import kotlinx.coroutines.launch import kotlinx.parcelize.Parcelize import java.io.File import java.io.IOException +import java.text.DecimalFormat import java.util.Locale import javax.inject.Inject import kotlin.math.max @@ -952,13 +953,17 @@ class ComposeActivity : private fun pickMedia(uri: Uri) { lifecycleScope.launch { viewModel.pickMedia(uri).onFailure { throwable -> - val errorId = when (throwable) { - is VideoSizeException -> R.string.error_video_upload_size - is AudioSizeException -> R.string.error_audio_upload_size - is VideoOrImageException -> R.string.error_media_upload_image_or_video - else -> R.string.error_media_upload_opening + val errorString = when (throwable) { + is FileSizeException -> { + val decimalFormat = DecimalFormat("0.##") + val allowedSizeInMb = throwable.allowedSizeInBytes.toDouble() / (1024 * 1024) + val formattedSize = decimalFormat.format(allowedSizeInMb) + getString(R.string.error_multimedia_size_limit, formattedSize) + } + is VideoOrImageException -> getString(R.string.error_media_upload_image_or_video) + else -> getString(R.string.error_media_upload_opening) } - displayTransientError(errorId) + displayTransientError(errorString) } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt index 221b306e..d7769cbd 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/MediaUploader.kt @@ -71,8 +71,7 @@ fun createNewImageFile(context: Context, suffix: String = ".jpg"): File { data class PreparedMedia(val type: QueuedMedia.Type, val uri: Uri, val size: Long) -class AudioSizeException : Exception() -class VideoSizeException : Exception() +class FileSizeException(val allowedSizeInBytes: Int) : Exception() class MediaTypeException : Exception() class CouldNotOpenFileException : Exception() class UploadServerError(val errorMessage: String) : Exception() @@ -166,7 +165,7 @@ class MediaUploader @Inject constructor( return when (mimeType.substring(0, mimeType.indexOf('/'))) { "video" -> { if (mediaSize > instanceInfo.videoSizeLimit) { - throw VideoSizeException() + throw FileSizeException(instanceInfo.videoSizeLimit) } PreparedMedia(QueuedMedia.Type.VIDEO, uri, mediaSize) } @@ -175,7 +174,7 @@ class MediaUploader @Inject constructor( } "audio" -> { if (mediaSize > instanceInfo.videoSizeLimit) { - throw AudioSizeException() + throw FileSizeException(instanceInfo.videoSizeLimit) } PreparedMedia(QueuedMedia.Type.AUDIO, uri, mediaSize) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1a7ef298..7c163d18 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,9 +12,8 @@ Failed loading account details Could not load the login page. The post is too long! - The file must be less than 8MB. - Video files must be less than 40MB. - Audio files must be less than 40MB. + Video and audio files cannot exceed %s MB in size. + The image could not be edited. That type of file cannot be uploaded. That file could not be opened.