Skip to content

Commit 668204f

Browse files
authored
Merge pull request #3207 from jim-kirisame/fix/webdav-tempfile-extension
Preserve original file extension in WebDAV temporary files
2 parents 1db0e0a + b1ae048 commit 668204f

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

  • src/java/JavaFileStorage/app/src/main/java/keepass2android/javafilestorage

src/java/JavaFileStorage/app/src/main/java/keepass2android/javafilestorage/WebDavStorage.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,24 @@ public void uploadFile(String path, byte[] data, boolean writeTransactional)
389389

390390
if (writeTransactional)
391391
{
392-
String randomSuffix = ".tmp." + generateRandomHexString(8);
393-
uploadFile(path + randomSuffix, data, false);
394-
renameOrMoveWebDavResource(path+randomSuffix, path, true);
392+
// Generate a unique temporary file suffix
393+
String tempSuffix = ".tmp." + generateRandomHexString(8);
394+
String tempPath;
395+
396+
// Preserve the original file extension for WebDAV providers that don't support
397+
// changing extensions. Insert temp suffix before the extension instead of after it.
398+
// Example: "file.kdbx" -> "file.tmp.abc12345.kdbx" (instead of "file.kdbx.tmp.abc12345")
399+
int lastDot = path.lastIndexOf('.');
400+
int lastSlash = path.lastIndexOf('/');
401+
if (lastDot > lastSlash && lastDot >= 0) {
402+
// File has an extension: insert temp suffix before the extension
403+
tempPath = path.substring(0, lastDot) + tempSuffix + path.substring(lastDot);
404+
} else {
405+
// No extension: append temp suffix at the end
406+
tempPath = path + tempSuffix;
407+
}
408+
uploadFile(tempPath, data, false);
409+
renameOrMoveWebDavResource(tempPath, path, true);
395410
return;
396411
}
397412

0 commit comments

Comments
 (0)