Merge pull request #1955 from vector-im/feature/fix_unique_filename

Fix save media error: Failed to build unique file
This commit is contained in:
Benoit Marty 2020-08-19 14:44:37 +02:00 committed by GitHub
commit 482a8f1fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -18,6 +18,7 @@ Bugfix 🐛:
- Fix relative date time formatting (#822) - Fix relative date time formatting (#822)
- Fix crash reported by RageShake - Fix crash reported by RageShake
- Fix refreshing of sessions list when another session is logged out - Fix refreshing of sessions list when another session is logged out
- Failed to build unique file (#1954)
Translations 🗣: Translations 🗣:
- Add PlayStore description resources in the Triple-T format, to let Weblate handle them - Add PlayStore description resources in the Triple-T format, to let Weblate handle them

View File

@ -308,11 +308,23 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
} }
} }
private fun appendTimeToFilename(name: String): String {
val dateExtension = SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()).format(Date())
if (!name.contains(".")) return name + "_" + dateExtension
val filename = name.substringBeforeLast(".")
val fileExtension = name.substringAfterLast(".")
return """${filename}_$dateExtension.$fileExtension"""
}
fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String?, notificationUtils: NotificationUtils) { fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String?, notificationUtils: NotificationUtils) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val filename = appendTimeToFilename(title)
val values = ContentValues().apply { val values = ContentValues().apply {
put(MediaStore.Images.Media.TITLE, title) put(MediaStore.Images.Media.TITLE, filename)
put(MediaStore.Images.Media.DISPLAY_NAME, title) put(MediaStore.Images.Media.DISPLAY_NAME, filename)
put(MediaStore.Images.Media.MIME_TYPE, mediaMimeType) put(MediaStore.Images.Media.MIME_TYPE, mediaMimeType)
put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis()) put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis()) put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())
@ -338,7 +350,7 @@ fun saveMedia(context: Context, file: File, title: String, mediaMimeType: String
} }
notificationUtils.buildDownloadFileNotification( notificationUtils.buildDownloadFileNotification(
uri, uri,
title, filename,
mediaMimeType ?: "application/octet-stream" mediaMimeType ?: "application/octet-stream"
).let { notification -> ).let { notification ->
notificationUtils.showNotificationMessage("DL", uri.hashCode(), notification) notificationUtils.showNotificationMessage("DL", uri.hashCode(), notification)