Skip to content

Commit 6f29d5e

Browse files
committed
fix(backup): overwite previous file
1 parent 5156d51 commit 6f29d5e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mobile/android/qt6/src/app/status/mobile/SafHelper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
public final class SafHelper {
2121
private SafHelper() {}
2222

23+
// Find a direct child file by name (non-recursive)
24+
private static DocumentFile findDirectChildByName(DocumentFile dir, String name) {
25+
if (dir == null || name == null) return null;
26+
for (DocumentFile f : dir.listFiles()) {
27+
if (name.equals(f.getName())) return f;
28+
}
29+
return null;
30+
}
31+
2332
/**
2433
* Persist read/write permission to a previously selected tree URI.
2534
*/
@@ -44,6 +53,13 @@ public static String copyFromPathToTree(Context context, String srcPath, String
4453
if (tree == null) return "";
4554
if (mime == null || mime.isEmpty()) mime = "application/octet-stream";
4655
if (displayName == null || displayName.isEmpty()) displayName = "backup.bkp";
56+
57+
// Overwrite behavior: if a file already exists with this name, delete it first
58+
DocumentFile existing = findDirectChildByName(tree, displayName);
59+
if (existing != null) {
60+
try { existing.delete(); } catch (Throwable ignored) {}
61+
}
62+
4763
DocumentFile file = tree.createFile(mime, displayName);
4864
if (file == null) return "";
4965
Uri destUri = file.getUri();

src/app_service/service/devices/service.nim

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ QtObject:
132132
safTakePersistablePermission(backupPath)
133133

134134
let fileName = splitFile(receivedData.fileName).name & splitFile(receivedData.fileName).ext
135-
# TODO overwrite existing file
136135
let destUri = safCopyFromPathToTree(receivedData.fileName, backupPath, "application/octet-stream", fileName)
137136
if destUri.len == 0:
138137
raise newException(CatchableError, "Failed to export backup into selected folder (SAF)")

0 commit comments

Comments
 (0)