From 3cfbbdef1c9ba1ccccc1bb2a91d5505ef91faa17 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 7 Sep 2024 21:39:45 +0200 Subject: [PATCH] fix: Use one source of truth for the filename (instead of recalculating it, this doesn't work, as the date _time_ will differ) Signed-off-by: Myzel394 <50424412+Myzel394@users.noreply.github.com> --- .../alibi/helpers/AudioBatchesFolder.kt | 13 +++++------ .../myzel394/alibi/helpers/BatchesFolder.kt | 23 +++++++------------ .../alibi/helpers/VideoBatchesFolder.kt | 18 ++++++++------- .../organisms/RecorderEventsHandler.kt | 21 +++++++---------- 4 files changed, 32 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/helpers/AudioBatchesFolder.kt b/app/src/main/java/app/myzel394/alibi/helpers/AudioBatchesFolder.kt index 46c50f0..4f1b607 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/AudioBatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/AudioBatchesFolder.kt @@ -40,18 +40,17 @@ class AudioBatchesFolder( override fun getOutputFileForFFmpeg( date: LocalDateTime, extension: String, + fileName: String, ): String { return when (type) { - BatchType.INTERNAL -> asInternalGetOutputFile(date, extension).absolutePath + BatchType.INTERNAL -> asInternalGetOutputFile(fileName).absolutePath BatchType.CUSTOM -> { - val name = getName(date, extension) - FFmpegKitConfig.getSafParameterForWrite( context, - (customFolder!!.findFile(name) ?: customFolder.createFile( + (customFolder!!.findFile(fileName) ?: customFolder.createFile( "audio/${extension}", - getName(date, extension), + fileName, )!!).uri )!! } @@ -59,7 +58,7 @@ class AudioBatchesFolder( BatchType.MEDIA -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val mediaUri = getOrCreateMediaFile( - name = getName(date, extension), + name = fileName, mimeType = "audio/$extension", relativePath = BASE_SCOPED_STORAGE_RELATIVE_PATH + "/" + MEDIA_SUBFOLDER_NAME, ) @@ -72,7 +71,7 @@ class AudioBatchesFolder( val path = arrayOf( Environment.getExternalStoragePublicDirectory(BASE_LEGACY_STORAGE_FOLDER), MEDIA_SUBFOLDER_NAME, - getName(date, extension) + fileName, ).joinToString("/") return File(path) .apply { diff --git a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt index e60be72..e55d301 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/BatchesFolder.kt @@ -191,8 +191,8 @@ abstract class BatchesFolder( return "$name.$extension" } - fun asInternalGetOutputFile(date: LocalDateTime, extension: String): File { - return File(getInternalFolder(), getName(date, extension)) + fun asInternalGetOutputFile(fileName: String): File { + return File(getInternalFolder(), fileName) } fun asMediaGetLegacyFile(name: String): File = File( @@ -203,16 +203,8 @@ abstract class BatchesFolder( } fun checkIfOutputAlreadyExists( - date: LocalDateTime, - extension: String + fileName: String, ): Boolean { - val stem = date - .format(DateTimeFormatter.ISO_DATE_TIME) - .toString() - .replace(":", "-") - .replace(".", "_") - val fileName = "$stem.$extension" - return when (type) { BatchType.INTERNAL -> File(getInternalFolder(), fileName).exists() @@ -245,6 +237,7 @@ abstract class BatchesFolder( abstract fun getOutputFileForFFmpeg( date: LocalDateTime, extension: String, + fileName: String, ): String abstract fun cleanup() @@ -255,18 +248,17 @@ abstract class BatchesFolder( disableCache: Boolean? = null, onNextParameterTry: (String) -> Unit = {}, onProgress: (Float?) -> Unit = {}, + fileName: String, ): String { val disableCache = disableCache ?: (type != BatchType.INTERNAL) val date = recording.getStartDateForFilename(filenameFormat) - if (!disableCache && checkIfOutputAlreadyExists( - recording.recordingStart, - recording.fileExtension - ) + if (!disableCache && checkIfOutputAlreadyExists(fileName) ) { return getOutputFileForFFmpeg( date = recording.recordingStart, extension = recording.fileExtension, + fileName = fileName, ) } @@ -282,6 +274,7 @@ abstract class BatchesFolder( val outputFile = getOutputFileForFFmpeg( date = date, extension = recording.fileExtension, + fileName = fileName, ) concatenationFunction( diff --git a/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt b/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt index e143681..7daf27e 100644 --- a/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt +++ b/app/src/main/java/app/myzel394/alibi/helpers/VideoBatchesFolder.kt @@ -39,18 +39,20 @@ class VideoBatchesFolder( private var customParcelFileDescriptor: ParcelFileDescriptor? = null - override fun getOutputFileForFFmpeg(date: LocalDateTime, extension: String): String { + override fun getOutputFileForFFmpeg( + date: LocalDateTime, + extension: String, + fileName: String, + ): String { return when (type) { - BatchType.INTERNAL -> asInternalGetOutputFile(date, extension).absolutePath + BatchType.INTERNAL -> asInternalGetOutputFile(fileName).absolutePath BatchType.CUSTOM -> { - val name = getName(date, extension) - FFmpegKitConfig.getSafParameterForWrite( context, - (customFolder!!.findFile(name) ?: customFolder.createFile( + (customFolder!!.findFile(fileName) ?: customFolder.createFile( "video/${extension}", - getName(date, extension), + fileName, )!!).uri )!! } @@ -58,7 +60,7 @@ class VideoBatchesFolder( BatchType.MEDIA -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val mediaUri = getOrCreateMediaFile( - name = getName(date, extension), + name = fileName, mimeType = "video/$extension", relativePath = BASE_SCOPED_STORAGE_RELATIVE_PATH + "/" + MEDIA_SUBFOLDER_NAME, ) @@ -71,7 +73,7 @@ class VideoBatchesFolder( val path = arrayOf( Environment.getExternalStoragePublicDirectory(BASE_LEGACY_STORAGE_FOLDER), MEDIA_SUBFOLDER_NAME, - getName(date, extension) + fileName, ).joinToString("/") return File(path) .apply { diff --git a/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt b/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt index 5040e45..91b7e0d 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/components/RecorderScreen/organisms/RecorderEventsHandler.kt @@ -177,38 +177,33 @@ fun RecorderEventsHandler( else -> throw Exception("Unknown recorder type") } + val fileName = batchesFolder.getName( + recording.recordingStart, + recording.fileExtension, + ) + batchesFolder.concatenate( recording, filenameFormat = settings.filenameFormat, + fileName = fileName, onProgress = { percentage -> processingProgress = percentage } ) // Save file - val name = batchesFolder.getName( - recording.recordingStart, - recording.fileExtension, - ) - when (batchesFolder.type) { BatchesFolder.BatchType.INTERNAL -> { when (batchesFolder) { is AudioBatchesFolder -> { saveAudioFile( - batchesFolder.asInternalGetOutputFile( - recording.recordingStart, - recording.fileExtension, - ), name + batchesFolder.asInternalGetOutputFile(fileName), fileName ) } is VideoBatchesFolder -> { saveVideoFile( - batchesFolder.asInternalGetOutputFile( - recording.recordingStart, - recording.fileExtension, - ), name + batchesFolder.asInternalGetOutputFile(fileName), fileName ) } }