Use a numbered file when destination already exists

Only applicable in cases when we don't have access to the parent folder (e.g. Downloads)
This commit is contained in:
Naveen 2023-04-23 15:04:29 +05:30
parent 7ec164ae42
commit 710a87f226
1 changed files with 8 additions and 2 deletions

View File

@ -259,8 +259,8 @@ fun Context.backupEventsAndTasks() {
mkdirs()
}
val exportFile = File(outputFolder, "$filename.ics")
val exportFilePath = exportFile.absolutePath
var exportFile = File(outputFolder, "$filename.ics")
var exportFilePath = exportFile.absolutePath
val outputStream = try {
if (hasProperStoredFirstParentUri(exportFilePath)) {
val exportFileUri = createDocumentUriUsingFirstParentTreeUri(exportFilePath)
@ -269,6 +269,12 @@ fun Context.backupEventsAndTasks() {
}
applicationContext.contentResolver.openOutputStream(exportFileUri, "wt") ?: FileOutputStream(exportFile)
} else {
var num = 0
while (getDoesFilePathExist(exportFilePath) && !exportFile.canWrite()) {
num++
exportFile = File(outputFolder, "${filename}_${num}.ics")
exportFilePath = exportFile.absolutePath
}
FileOutputStream(exportFile)
}
} catch (e: Exception) {