Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [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
Expand Down Expand Up @@ -96,6 +97,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
[#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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SaveAsActivity : SimpleActivity() {
?: filename.getMimeType()
val inputStream = contentResolver.openInputStream(source)

val destinationPath = "$destination/$filename"
val destinationPath = getAvailablePath("$destination/$filename")
val outputStream = getFileOutputStreamSync(destinationPath, mimeType, null)!!
inputStream!!.copyTo(outputStream)
rescanPaths(arrayListOf(destinationPath))
Expand Down Expand Up @@ -86,4 +86,30 @@ class SaveAsActivity : SimpleActivity() {
return filename.replace("[/\\\\<>:\"|?*\u0000-\u001F]".toRegex(), "_")
.takeIf { it.isNotBlank() } ?: "unnamed_file"
}

private fun getAvailablePath(destinationPath: String): String {
if (!getDoesFilePathExist(destinationPath)) {
return destinationPath
}

val file = File(destinationPath)
return findAvailableName(file)
}

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 ""

var index = 1
var newPath: String

do {
newPath = "$parent/${name}_$index$ext"
index++
} while (getDoesFilePathExist(newPath))

return newPath
}

}
Loading