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 <xosem@disroot.org>
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 <the-ceo-of-antifa@protonmail.com>
Co-authored-by: XoseM <xosem@disroot.org>
Co-authored-by: Konrad Pozniak <k.pozniak@gmx.at>
This commit is contained in:
QuirkyPony 2022-08-05 18:55:13 +02:00 committed by GitHub
parent df9e2652e9
commit 17fb93626c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 13 deletions

View file

@ -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)
}
displayTransientError(errorId)
is VideoOrImageException -> getString(R.string.error_media_upload_image_or_video)
else -> getString(R.string.error_media_upload_opening)
}
displayTransientError(errorString)
}
}
}

View file

@ -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)
}

View file

@ -12,9 +12,8 @@
<string name="error_loading_account_details">Failed loading account details</string>
<string name="error_could_not_load_login_page">Could not load the login page.</string>
<string name="error_compose_character_limit">The post is too long!</string>
<string name="error_image_upload_size">The file must be less than 8MB.</string>
<string name="error_video_upload_size">Video files must be less than 40MB.</string>
<string name="error_audio_upload_size">Audio files must be less than 40MB.</string>
<string name="error_multimedia_size_limit">Video and audio files cannot exceed %s MB in size.</string>
<string name="error_image_edit_failed">The image could not be edited.</string>
<string name="error_media_upload_type">That type of file cannot be uploaded.</string>
<string name="error_media_upload_opening">That file could not be opened.</string>