Browse Source

Prevent multiple file download requests

Vishnu Mohandas 4 năm trước cách đây
mục cha
commit
04e2edd4e7
1 tập tin đã thay đổi với 20 bổ sung6 xóa
  1. 20 6
      lib/utils/file_util.dart

+ 20 - 6
lib/utils/file_util.dart

@@ -96,6 +96,9 @@ Future<Uint8List> getBytesFromDisk(File file, {int quality = 100}) async {
   }
   }
 }
 }
 
 
+final Map<int, Future<io.File>> downloadsInProgress =
+    Map<int, Future<io.File>>();
+
 Future<io.File> getFileFromServer(File file,
 Future<io.File> getFileFromServer(File file,
     {ProgressCallback progressCallback}) async {
     {ProgressCallback progressCallback}) async {
   final cacheManager = file.fileType == FileType.video
   final cacheManager = file.fileType == FileType.video
@@ -106,11 +109,14 @@ Future<io.File> getFileFromServer(File file,
   } else {
   } else {
     return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
     return cacheManager.getFileFromCache(file.getDownloadUrl()).then((info) {
       if (info == null) {
       if (info == null) {
-        return _downloadAndDecrypt(
-          file,
-          cacheManager,
-          progressCallback: progressCallback,
-        );
+        if (!downloadsInProgress.containsKey(file.uploadedFileID)) {
+          downloadsInProgress[file.uploadedFileID] = _downloadAndDecrypt(
+            file,
+            cacheManager,
+            progressCallback: progressCallback,
+          );
+        }
+        return downloadsInProgress[file.uploadedFileID];
       } else {
       } else {
         return info.file;
         return info.file;
       }
       }
@@ -152,6 +158,9 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
   final decryptedFilePath = Configuration.instance.getTempDirectory() +
   final decryptedFilePath = Configuration.instance.getTempDirectory() +
       file.generatedID.toString() +
       file.generatedID.toString() +
       ".decrypted";
       ".decrypted";
+
+  final encryptedFile = io.File(encryptedFilePath);
+  final decryptedFile = io.File(decryptedFilePath);
   return Dio()
   return Dio()
       .download(
       .download(
     file.getDownloadUrl(),
     file.getDownloadUrl(),
@@ -162,7 +171,11 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
     if (response.statusCode != 200) {
     if (response.statusCode != 200) {
       logger.warning("Could not download file: ", response.toString());
       logger.warning("Could not download file: ", response.toString());
       return null;
       return null;
+    } else if (!encryptedFile.existsSync()) {
+      logger.warning("File was not downloaded correctly.");
+      return null;
     }
     }
+    logger.info("File downloaded: " + file.uploadedFileID.toString());
     var attributes = ChaChaAttributes(
     var attributes = ChaChaAttributes(
       EncryptionAttribute(
       EncryptionAttribute(
           bytes: await CryptoUtil.decrypt(
           bytes: await CryptoUtil.decrypt(
@@ -174,8 +187,8 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
     );
     );
     await CryptoUtil.decryptFile(
     await CryptoUtil.decryptFile(
         encryptedFilePath, decryptedFilePath, attributes);
         encryptedFilePath, decryptedFilePath, attributes);
+    logger.info("File decrypted: " + file.uploadedFileID.toString());
     io.File(encryptedFilePath).deleteSync();
     io.File(encryptedFilePath).deleteSync();
-    final decryptedFile = io.File(decryptedFilePath);
     final fileExtension = extension(file.title).substring(1).toLowerCase();
     final fileExtension = extension(file.title).substring(1).toLowerCase();
     final cachedFile = await cacheManager.putFile(
     final cachedFile = await cacheManager.putFile(
       file.getDownloadUrl(),
       file.getDownloadUrl(),
@@ -185,6 +198,7 @@ Future<io.File> _downloadAndDecrypt(File file, BaseCacheManager cacheManager,
       fileExtension: fileExtension,
       fileExtension: fileExtension,
     );
     );
     decryptedFile.deleteSync();
     decryptedFile.deleteSync();
+    downloadsInProgress.remove(file.uploadedFileID);
     return cachedFile;
     return cachedFile;
   });
   });
 }
 }