From dbc907ceeb2f4d0cbe7a30ca7499a9ded46ff3fe Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Sat, 3 Aug 2024 11:19:29 +0500 Subject: [PATCH] web: write downloading files to temporary location before moving --- apps/web/src/interfaces/fs.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/apps/web/src/interfaces/fs.ts b/apps/web/src/interfaces/fs.ts index c750b35d3..f83e25647 100644 --- a/apps/web/src/interfaces/fs.ts +++ b/apps/web/src/interfaces/fs.ts @@ -514,10 +514,11 @@ async function downloadFile( logger.debug("Got attachment signed url", { filename }); - const fileHandle = await streamablefs.createFile( - filename, + const tempFileHandle = await streamablefs.createFile( + `${filename}-temp`, decryptedLength, - attachment.mimeType || "application/octet-stream" + attachment.mimeType || "application/octet-stream", + { overwrite: true } ); const response = await fetch(signedUrl, { @@ -541,7 +542,17 @@ async function downloadFile( isFeatureSupported("opfs") ? "copy" : "nocopy" ) ) - .pipeTo(fileHandle.writeable); + .pipeTo(tempFileHandle.writeable); + + await streamablefs.moveFile( + tempFileHandle, + await streamablefs.createFile( + filename, + decryptedLength, + attachment.mimeType || "application/octet-stream", + { overwrite: true } + ) + ); logger.debug("Attachment downloaded", { filename }); return true;