From 4e3adcf4516fb9b36733c4063e84ca03d8b3f0fd Mon Sep 17 00:00:00 2001 From: Suhas Dissanayake Date: Fri, 13 Oct 2023 19:14:17 +0530 Subject: [PATCH] fix crash when file names contain invalid characters --- .../memerize/backend/viewmodels/PhotoViewModel.kt | 8 ++++++-- .../memerize/backend/viewmodels/PlayerViewModel.kt | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PhotoViewModel.kt b/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PhotoViewModel.kt index 6dfb162..e11c10a 100644 --- a/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PhotoViewModel.kt +++ b/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PhotoViewModel.kt @@ -69,13 +69,17 @@ class PhotoViewModel : ViewModel() { val outputFile = saveDir.createFile( "image/jpg", - "${meme.title.take(64)}-${UUID.randomUUID().toString().take(8)}" + "${meme.title.take(64)}-${ + UUID.randomUUID().toString().take(8) + }.jpg".replace("[\\\\/:*?\"<>|]".toRegex(), "") ) if (outputFile == null) { withContext(Dispatchers.Main) { downloadState = DownloadState.Error } - Toast.makeText(context, "Failed to create file", Toast.LENGTH_LONG).show() + withContext(Dispatchers.Main) { + Toast.makeText(context, "Failed to create file", Toast.LENGTH_LONG).show() + } return@launch } if (bitmap != null) { diff --git a/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PlayerViewModel.kt b/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PlayerViewModel.kt index e6cd31b..084752f 100644 --- a/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PlayerViewModel.kt +++ b/app/src/main/java/app/suhasdissa/memerize/backend/viewmodels/PlayerViewModel.kt @@ -22,7 +22,9 @@ class PlayerViewModel() : ViewModel() { @RequiresApi(Build.VERSION_CODES.O) fun downloadVideo(context: Context, meme: Meme) { - val fileName = "${meme.title.take(64)}-${UUID.randomUUID().toString().take(8)}" + val fileName = "${meme.title.take(64)}-${ + UUID.randomUUID().toString().take(8) + }".replace("[\\\\/:*?\"<>|]".toRegex(), "") viewModelScope.launch { downloadState = DownloadState.Loading val downloader = RedditVideoDownloader()