Ver código fonte

Merge pull request #813 from ente-io/fix_redundant_fetch

Fix: Avoid redundant fetch for upload urls
Vishnu Mohandas 2 anos atrás
pai
commit
bbef528135

+ 1 - 0
lib/services/remote_sync_service.dart

@@ -477,6 +477,7 @@ class RemoteSyncService {
     final int toBeUploaded = filesToBeUploaded.length + updatedFileIDs.length;
     final int toBeUploaded = filesToBeUploaded.length + updatedFileIDs.length;
     if (toBeUploaded > 0) {
     if (toBeUploaded > 0) {
       Bus.instance.fire(SyncStatusUpdate(SyncStatus.preparingForUpload));
       Bus.instance.fire(SyncStatusUpdate(SyncStatus.preparingForUpload));
+      await _uploader.checkNetworkForUpload();
       // verify if files upload is allowed based on their subscription plan and
       // verify if files upload is allowed based on their subscription plan and
       // storage limit. To avoid creating new endpoint, we are using
       // storage limit. To avoid creating new endpoint, we are using
       // fetchUploadUrls as alternative method.
       // fetchUploadUrls as alternative method.

+ 15 - 6
lib/utils/file_uploader.dart

@@ -271,18 +271,27 @@ class FileUploader {
     }
     }
   }
   }
 
 
-  Future<File> _tryToUpload(
-    File file,
-    int collectionID,
-    bool forcedUpload,
-  ) async {
+  Future<void> checkNetworkForUpload({bool isForceUpload = false}) async {
+    // Note: We don't support force uploading currently. During force upload,
+    // network check is skipped completely
+    if (isForceUpload) {
+      return;
+    }
     final connectivityResult = await (Connectivity().checkConnectivity());
     final connectivityResult = await (Connectivity().checkConnectivity());
     final canUploadUnderCurrentNetworkConditions =
     final canUploadUnderCurrentNetworkConditions =
         (connectivityResult == ConnectivityResult.wifi ||
         (connectivityResult == ConnectivityResult.wifi ||
             Configuration.instance.shouldBackupOverMobileData());
             Configuration.instance.shouldBackupOverMobileData());
-    if (!canUploadUnderCurrentNetworkConditions && !forcedUpload) {
+    if (!canUploadUnderCurrentNetworkConditions) {
       throw WiFiUnavailableError();
       throw WiFiUnavailableError();
     }
     }
+  }
+
+  Future<File> _tryToUpload(
+    File file,
+    int collectionID,
+    bool forcedUpload,
+  ) async {
+    await checkNetworkForUpload(isForceUpload: forcedUpload);
     final fileOnDisk = await FilesDB.instance.getFile(file.generatedID!);
     final fileOnDisk = await FilesDB.instance.getFile(file.generatedID!);
     final wasAlreadyUploaded = fileOnDisk != null &&
     final wasAlreadyUploaded = fileOnDisk != null &&
         fileOnDisk.uploadedFileID != null &&
         fileOnDisk.uploadedFileID != null &&