Sfoglia il codice sorgente

Ensure file deletion only after read

Vishnu Mohandas 4 anni fa
parent
commit
9f6cfebcb1
1 ha cambiato i file con 11 aggiunte e 9 eliminazioni
  1. 11 9
      lib/utils/file_util.dart

+ 11 - 9
lib/utils/file_util.dart

@@ -157,24 +157,26 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
   )
       .then((_) async {
     var attributes = ChaChaAttributes(
-        EncryptionAttribute(base64: file.fileDecryptionParams.header),
-        EncryptionAttribute(
-            bytes: await CryptoUtil.decrypt(
-          file.fileDecryptionParams.encryptedKey,
-          Configuration.instance.getBase64EncodedKey(),
-          file.fileDecryptionParams.nonce,
-        )));
+      EncryptionAttribute(
+          bytes: await CryptoUtil.decrypt(
+        file.fileDecryptionParams.encryptedKey,
+        Configuration.instance.getBase64EncodedKey(),
+        file.fileDecryptionParams.keyDecryptionNonce,
+      )),
+      EncryptionAttribute(base64: file.fileDecryptionParams.header),
+    );
     await CryptoUtil.chachaDecrypt(encryptedFile, decryptedFile, attributes);
     encryptedFile.deleteSync();
-    decryptedFile.deleteSync();
     final fileExtension = extension(file.title).substring(1).toLowerCase();
-    return cacheManager.putFile(
+    final cachedFile = await cacheManager.putFile(
       file.getDownloadUrl(),
       decryptedFile.readAsBytesSync(),
       eTag: file.getDownloadUrl(),
       maxAge: Duration(days: 365),
       fileExtension: fileExtension,
     );
+    decryptedFile.deleteSync();
+    return cachedFile;
   });
 }