From 7c3e50925e3be16f439cd1f2ac637956c05c9f93 Mon Sep 17 00:00:00 2001 From: Jan Guegel Date: Thu, 4 Dec 2025 18:07:01 +0100 Subject: [PATCH 1/6] change SaveAsDialog now appends "_{index}" to filename if file already exists --- CHANGELOG.md | 4 +++ .../filemanager/activities/SaveAsActivity.kt | 30 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3af47ee7..1145507c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed files from hidden folders showing up in storage tab browser ([#217]) +### Changed +- SaveAsDialog now appends "_{index}" to filename if file already exists ([#131]) + ## [1.3.0] - 2025-09-30 ### Added - Added a separate "Save as" option in the text editor ([#224]) @@ -96,6 +99,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#250]: https://github.com/FossifyOrg/File-Manager/issues/250 [#251]: https://github.com/FossifyOrg/File-Manager/issues/251 [#267]: https://github.com/FossifyOrg/File-Manager/issues/267 +[#267]: https://github.com/FossifyOrg/File-Manager/issues/131 [Unreleased]: https://github.com/FossifyOrg/File-Manager/compare/1.3.0...HEAD [1.3.0]: https://github.com/FossifyOrg/File-Manager/compare/1.2.3...1.3.0 diff --git a/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt b/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt index 0681f30c..fb0e1f9f 100644 --- a/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt +++ b/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt @@ -58,7 +58,7 @@ class SaveAsActivity : SimpleActivity() { ?: filename.getMimeType() val inputStream = contentResolver.openInputStream(source) - val destinationPath = "$destination/$filename" + val destinationPath = setAvailablePath("$destination/$filename") val outputStream = getFileOutputStreamSync(destinationPath, mimeType, null)!! inputStream!!.copyTo(outputStream) rescanPaths(arrayListOf(destinationPath)) @@ -86,4 +86,32 @@ class SaveAsActivity : SimpleActivity() { return filename.replace("[/\\\\<>:\"|?*\u0000-\u001F]".toRegex(), "_") .takeIf { it.isNotBlank() } ?: "unnamed_file" } + + private fun setAvailablePath(path: String): String { + val file = File(path) + + return if (!file.exists()) { + path + } else { + findAvailableNameForExistingFile(file) + } + } + + private fun findAvailableNameForExistingFile(file: File): String { + val parent = file.parent ?: return file.absolutePath + val name = file.nameWithoutExtension + val ext = if (file.extension.isNotEmpty()) ".${file.extension}" else "" + + var index = 1 + var newFile: File + + do { + val newName = "${name}_${index}$ext" + newFile = File(parent, newName) + index++ + } while (newFile.exists()) + + return newFile.absolutePath + } + } From 12424cd6f282b271877e9f3e7b4a968a3a601b5a Mon Sep 17 00:00:00 2001 From: Jan <4600407+jguegel@users.noreply.github.com> Date: Thu, 4 Dec 2025 18:15:00 +0100 Subject: [PATCH 2/6] Fix issue link in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1145507c..d13dd300 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,7 +99,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#250]: https://github.com/FossifyOrg/File-Manager/issues/250 [#251]: https://github.com/FossifyOrg/File-Manager/issues/251 [#267]: https://github.com/FossifyOrg/File-Manager/issues/267 -[#267]: https://github.com/FossifyOrg/File-Manager/issues/131 +[#131]: https://github.com/FossifyOrg/File-Manager/issues/131 [Unreleased]: https://github.com/FossifyOrg/File-Manager/compare/1.3.0...HEAD [1.3.0]: https://github.com/FossifyOrg/File-Manager/compare/1.2.3...1.3.0 From 166a7880379e42896dc010adadc4ef2b2e303d0f Mon Sep 17 00:00:00 2001 From: Jan <4600407+jguegel@users.noreply.github.com> Date: Fri, 5 Dec 2025 15:03:54 +0100 Subject: [PATCH 3/6] Update CHANGELOG.md according to feedback Co-authored-by: Naveen Singh <36371707+naveensingh@users.noreply.github.com> --- CHANGELOG.md | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d13dd300..35cdc48a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,15 +2,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -## [Unreleased] -### Fixed -- Fixed files from hidden folders showing up in storage tab browser ([#217]) - -### Changed -- SaveAsDialog now appends "_{index}" to filename if file already exists ([#131]) - +- Fixed an issue where existing files were overwritten when saving new files ([#131]) ## [1.3.0] - 2025-09-30 ### Added - Added a separate "Save as" option in the text editor ([#224]) From eb26e3e3721c7600c1819ad867bff61621d508a4 Mon Sep 17 00:00:00 2001 From: Jan <4600407+jguegel@users.noreply.github.com> Date: Fri, 5 Dec 2025 15:08:42 +0100 Subject: [PATCH 4/6] fix Changelog after commit suggested change Updated changelog with recent fixes and additions. --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35cdc48a..b269aeec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] +### Fixed +- Fixed files from hidden folders showing up in storage tab browser ([#217]) - Fixed an issue where existing files were overwritten when saving new files ([#131]) + ## [1.3.0] - 2025-09-30 ### Added - Added a separate "Save as" option in the text editor ([#224]) From a6fdcc7715589343ce70306b1addc40e00168ff3 Mon Sep 17 00:00:00 2001 From: Jan Guegel Date: Fri, 5 Dec 2025 15:09:45 +0100 Subject: [PATCH 5/6] rename function to getAvailablePath and using getDoesFilePathExist make it compatible with external filestorage --- .../filemanager/activities/SaveAsActivity.kt | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt b/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt index fb0e1f9f..29c62dea 100644 --- a/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt +++ b/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt @@ -58,7 +58,7 @@ class SaveAsActivity : SimpleActivity() { ?: filename.getMimeType() val inputStream = contentResolver.openInputStream(source) - val destinationPath = setAvailablePath("$destination/$filename") + val destinationPath = getAvailablePath("$destination/$filename") val outputStream = getFileOutputStreamSync(destinationPath, mimeType, null)!! inputStream!!.copyTo(outputStream) rescanPaths(arrayListOf(destinationPath)) @@ -87,17 +87,16 @@ class SaveAsActivity : SimpleActivity() { .takeIf { it.isNotBlank() } ?: "unnamed_file" } - private fun setAvailablePath(path: String): String { - val file = File(path) - - return if (!file.exists()) { - path - } else { - findAvailableNameForExistingFile(file) + private fun getAvailablePath(destinationPath: String): String { + if (!getDoesFilePathExist(destinationPath)) { + return destinationPath } + + val file = File(destinationPath) + return findAvailableName(file) } - private fun findAvailableNameForExistingFile(file: File): String { + private fun findAvailableName(file: File): String { val parent = file.parent ?: return file.absolutePath val name = file.nameWithoutExtension val ext = if (file.extension.isNotEmpty()) ".${file.extension}" else "" From 8cebbd213fedf509df0e1e3080934273ea42125a Mon Sep 17 00:00:00 2001 From: Jan Guegel Date: Fri, 5 Dec 2025 15:24:28 +0100 Subject: [PATCH 6/6] change to use getDoesFilePathExists function --- .../org/fossify/filemanager/activities/SaveAsActivity.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt b/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt index 29c62dea..9255c03d 100644 --- a/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt +++ b/app/src/main/kotlin/org/fossify/filemanager/activities/SaveAsActivity.kt @@ -102,15 +102,14 @@ class SaveAsActivity : SimpleActivity() { val ext = if (file.extension.isNotEmpty()) ".${file.extension}" else "" var index = 1 - var newFile: File + var newPath: String do { - val newName = "${name}_${index}$ext" - newFile = File(parent, newName) + newPath = "$parent/${name}_$index$ext" index++ - } while (newFile.exists()) + } while (getDoesFilePathExist(newPath)) - return newFile.absolutePath + return newPath } }