Skip to content

Commit ea8af83

Browse files
committed
[Android] deletes existing file when trying to create new file
1 parent cc62a52 commit ea8af83

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

android/src/main/kotlin/vn/hunghd/flutterdownloader/DownloadWorker.kt

+22-6
Original file line numberDiff line numberDiff line change
@@ -497,17 +497,33 @@ class DownloadWorker(context: Context, params: WorkerParameters) :
497497
private fun createFileInAppSpecificDir(filename: String, savedDir: String): File? {
498498
val newFile = File(savedDir, filename)
499499
try {
500-
val rs: Boolean = newFile.createNewFile()
501-
if (rs) {
502-
return newFile
503-
} else {
504-
logError("It looks like you are trying to save file in public storage but not setting 'saveInPublicStorage' to 'true'")
500+
if (newFile.exists()) {
501+
val deleted = newFile.delete()
502+
if (!deleted) {
503+
logError("Unable to delete existing file: ${newFile.absolutePath}")
504+
return null
505+
}
506+
}
507+
508+
val created: Boolean = newFile.createNewFile()
509+
if (!created) {
510+
logError(
511+
"""
512+
Unable to create new file: ${newFile.absolutePath}.
513+
Are are trying to save file in public storage
514+
but not setting 'saveInPublicStorage' to 'true'?
515+
""".trimIndent()
516+
)
517+
return null
505518
}
519+
520+
return newFile
521+
506522
} catch (e: IOException) {
507523
e.printStackTrace()
508524
logError("Create a file using java.io API failed ")
525+
return null
509526
}
510-
return null
511527
}
512528

513529
/**

0 commit comments

Comments
 (0)