Browse Source

Mark file invalid for upload if > 5Gib

Signed-off-by: Neeraj Gupta <254676+ua741@users.noreply.github.com>
Neeraj Gupta 1 year ago
parent
commit
c896fc59f3
2 changed files with 43 additions and 33 deletions
  1. 1 0
      lib/core/errors.dart
  2. 42 33
      lib/utils/file_uploader.dart

+ 1 - 0
lib/core/errors.dart

@@ -6,6 +6,7 @@ enum InvalidReason {
   imageToLivePhotoTypeChanged,
   livePhotoVideoMissing,
   thumbnailMissing,
+  tooLargeFile,
   unknown,
 }
 

+ 42 - 33
lib/utils/file_uploader.dart

@@ -42,6 +42,7 @@ class FileUploader {
   static const kMaximumConcurrentVideoUploads = 2;
   static const kMaximumThumbnailCompressionAttempts = 2;
   static const kMaximumUploadAttempts = 4;
+  static const kMaxFileSize5Gib = 5368709120;
   static const kBlockedUploadsPollFrequency = Duration(seconds: 2);
   static const kFileUploadTimeout = Duration(minutes: 50);
   static const k20MBStorageBuffer = 20 * 1024 * 1024;
@@ -283,7 +284,6 @@ class FileUploader {
     }
   }
 
-
   Future<void> removeStaleFiles() async {
     try {
       final String dir = Configuration.instance.getTempDirectory();
@@ -381,15 +381,9 @@ class FileUploader {
         'starting ${forcedUpload ? 'forced' : ''} '
         '${isUpdatedFile ? 're-upload' : 'upload'} of ${file.toString()}',
       );
-      try {
-        mediaUploadData = await getUploadDataFromEnteFile(file);
-      } catch (e) {
-        if (e is InvalidFileError) {
-          await _onInvalidFileError(file, e);
-        } else {
-          rethrow;
-        }
-      }
+
+      mediaUploadData = await getUploadDataFromEnteFile(file);
+
       Uint8List? key;
       if (isUpdatedFile) {
         key = getFileKey(file);
@@ -399,7 +393,7 @@ class FileUploader {
         // uploaded file. If map is found, it also returns the corresponding
         // mapped or update file entry.
         final result = await _mapToExistingUploadWithSameHash(
-          mediaUploadData!,
+          mediaUploadData,
           file,
           collectionID,
         );
@@ -416,7 +410,7 @@ class FileUploader {
       if (File(encryptedFilePath).existsSync()) {
         await File(encryptedFilePath).delete();
       }
-      await _checkIfWithinStorageLimit(mediaUploadData!.sourceFile!);
+      await _checkIfWithinStorageLimit(mediaUploadData.sourceFile!);
       final encryptedFile = File(encryptedFilePath);
       final EncryptionResult fileAttributes = await CryptoUtil.encryptFile(
         mediaUploadData.sourceFile!.path,
@@ -540,9 +534,14 @@ class FileUploader {
           e is StorageLimitExceededError ||
           e is WiFiUnavailableError ||
           e is SilentlyCancelUploadsError ||
+          e is InvalidFileError ||
           e is FileTooLargeForPlanError)) {
         _logger.severe("File upload failed for " + file.toString(), e, s);
       }
+      if (e is InvalidFileError) {
+        _logger.severe("File upload ignored for " + file.toString(), e, s);
+        await _onInvalidFileError(file, e);
+      }
       if ((e is StorageLimitExceededError ||
           e is FileTooLargeForPlanError ||
           e is NoActiveSubscriptionError)) {
@@ -748,8 +747,15 @@ class FileUploader {
             'freeStorage $freeStorage');
         throw StorageLimitExceededError();
       }
+      if (fileSize > kMaxFileSize5Gib) {
+        _logger.warning('File size exceeds 5GiB fileSize $fileSize');
+        throw InvalidFileError(
+          'file size above 5GiB',
+          InvalidReason.tooLargeFile,
+        );
+      }
     } catch (e) {
-      if (e is StorageLimitExceededError) {
+      if (e is StorageLimitExceededError || e is InvalidFileError) {
         rethrow;
       } else {
         _logger.severe('Error checking storage limit', e);
@@ -758,28 +764,31 @@ class FileUploader {
   }
 
   Future _onInvalidFileError(EnteFile file, InvalidFileError e) async {
-    final bool canIgnoreFile = file.localID != null &&
-        file.deviceFolder != null &&
-        file.title != null &&
-        !file.isSharedMediaToAppSandbox;
-    // If the file is not uploaded yet and either it can not be ignored or the
-    // err is related to live photo media, delete the local entry
-    final bool deleteEntry = !file.isUploaded &&
-        (!canIgnoreFile || e.reason.isLivePhotoErr);
+    try {
+      final bool canIgnoreFile = file.localID != null &&
+          file.deviceFolder != null &&
+          file.title != null &&
+          !file.isSharedMediaToAppSandbox;
+      // If the file is not uploaded yet and either it can not be ignored or the
+      // err is related to live photo media, delete the local entry
+      final bool deleteEntry =
+          !file.isUploaded && (!canIgnoreFile || e.reason.isLivePhotoErr);
 
-    if (e.reason != InvalidReason.thumbnailMissing || !canIgnoreFile) {
-      _logger.severe(
-        "Invalid file, localDelete: $deleteEntry, ignored: $canIgnoreFile",
-        e,
-      );
-    }
-    if (deleteEntry) {
-      await FilesDB.instance.deleteLocalFile(file);
-    }
-    if (canIgnoreFile) {
-      await LocalSyncService.instance.ignoreUpload(file, e);
+      if (e.reason != InvalidReason.thumbnailMissing || !canIgnoreFile) {
+        _logger.severe(
+          "Invalid file, localDelete: $deleteEntry, ignored: $canIgnoreFile",
+          e,
+        );
+      }
+      if (deleteEntry) {
+        await FilesDB.instance.deleteLocalFile(file);
+      }
+      if (canIgnoreFile) {
+        await LocalSyncService.instance.ignoreUpload(file, e);
+      }
+    } catch (e, s) {
+      _logger.severe("Failed to handle invalid file error", e, s);
     }
-    throw e;
   }
 
   Future<EnteFile> _uploadFile(