Browse Source

refactor: extract thumbnail fetch logic

Neeraj Gupta 1 year ago
parent
commit
149dd74543
1 changed files with 24 additions and 20 deletions
  1. 24 20
      lib/utils/file_uploader_util.dart

+ 24 - 20
lib/utils/file_uploader_util.dart

@@ -107,7 +107,6 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(ente.File file) async {
       InvalidReason.sourceFileMissing,
       InvalidReason.sourceFileMissing,
     );
     );
   }
   }
-
   // h4ck to fetch location data if missing (thank you Android Q+) lazily only during uploads
   // h4ck to fetch location data if missing (thank you Android Q+) lazily only during uploads
   await _decorateEnteFileData(file, asset);
   await _decorateEnteFileData(file, asset);
   fileHash = CryptoUtil.bin2base64(await CryptoUtil.getHash(sourceFile));
   fileHash = CryptoUtil.bin2base64(await CryptoUtil.getHash(sourceFile));
@@ -142,25 +141,7 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(ente.File file) async {
     zipHash = CryptoUtil.bin2base64(await CryptoUtil.getHash(sourceFile));
     zipHash = CryptoUtil.bin2base64(await CryptoUtil.getHash(sourceFile));
   }
   }
 
 
-  thumbnailData = await asset.thumbnailDataWithSize(
-    const ThumbnailSize(thumbnailLargeSize, thumbnailLargeSize),
-    quality: thumbnailQuality,
-  );
-  if (thumbnailData == null) {
-    throw InvalidFileError(
-      "no thumbnail ${file.tag}",
-      InvalidReason.thumbnailMissing,
-    );
-  }
-  int compressionAttempts = 0;
-  while (thumbnailData!.length > thumbnailDataLimit &&
-      compressionAttempts < kMaximumThumbnailCompressionAttempts) {
-    _logger.info("Thumbnail size " + thumbnailData.length.toString());
-    thumbnailData = await compressThumbnail(thumbnailData);
-    _logger
-        .info("Compressed thumbnail size " + thumbnailData.length.toString());
-    compressionAttempts++;
-  }
+  thumbnailData = await _getThumbnailForUpload(asset, file);
   isDeleted = !(await asset.exists);
   isDeleted = !(await asset.exists);
   int? h, w;
   int? h, w;
   if (asset.width != 0 && asset.height != 0) {
   if (asset.width != 0 && asset.height != 0) {
@@ -187,6 +168,29 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(ente.File file) async {
   );
   );
 }
 }
 
 
+Future<Uint8List?> _getThumbnailForUpload(AssetEntity asset, ente.File file) async {
+  Uint8List? thumbnailData = await asset.thumbnailDataWithSize(
+    const ThumbnailSize(thumbnailLargeSize, thumbnailLargeSize),
+    quality: thumbnailQuality,
+  );
+  if (thumbnailData == null) {
+    throw InvalidFileError(
+      "no thumbnail ${file.tag}",
+      InvalidReason.thumbnailMissing,
+    );
+  }
+  int compressionAttempts = 0;
+  while (thumbnailData!.length > thumbnailDataLimit &&
+      compressionAttempts < kMaximumThumbnailCompressionAttempts) {
+    _logger.info("Thumbnail size " + thumbnailData.length.toString());
+    thumbnailData = await compressThumbnail(thumbnailData);
+    _logger
+        .info("Compressed thumbnail size " + thumbnailData.length.toString());
+    compressionAttempts++;
+  }
+  return thumbnailData;
+}
+
 // check if the assetType is still the same. This can happen for livePhotos
 // check if the assetType is still the same. This can happen for livePhotos
 // if the user turns off the video using native photos app
 // if the user turns off the video using native photos app
 void _assertFileType(AssetEntity asset, ente.File file) {
 void _assertFileType(AssetEntity asset, ente.File file) {