瀏覽代碼

Assert assetFileType and enteFile type

Neeraj Gupta 1 年之前
父節點
當前提交
46b547a66e
共有 3 個文件被更改,包括 28 次插入2 次删除
  1. 3 1
      lib/core/errors.dart
  2. 1 1
      lib/models/file.dart
  3. 24 0
      lib/utils/file_uploader_util.dart

+ 3 - 1
lib/core/errors.dart

@@ -2,9 +2,11 @@ enum InvalidReason {
   assetDeleted,
   assetDeletedEvent,
   sourceFileMissing,
-  livePhotoTypeChanged,
+  livePhotoToImageTypeChanged,
+  imageToLivePhotoTypeChanged,
   livePhotoVideoMissing,
   thumbnailMissing,
+  unknown,
 }
 
 class InvalidFileError extends ArgumentError {

+ 1 - 1
lib/models/file.dart

@@ -75,7 +75,7 @@ class File extends EnteFile {
     file.deviceFolder = pathName;
     file.location =
         Location(latitude: asset.latitude, longitude: asset.longitude);
-    file.fileType = _fileTypeFromAsset(asset);
+    file.fileType = fileTypeFromAsset(asset);
     file.creationTime = parseFileCreationTime(file.title, asset);
     file.modificationTime = asset.modifiedDateTime.microsecondsSinceEpoch;
     file.fileSubType = asset.subtype;

+ 24 - 0
lib/utils/file_uploader_util.dart

@@ -90,6 +90,7 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(ente.File file) async {
   if (asset == null) {
     throw InvalidFileError("", InvalidReason.assetDeleted);
   }
+  _assertFileType(asset, file);
   sourceFile = await asset.originFile
       .timeout(const Duration(seconds: 3))
       .catchError((e) async {
@@ -186,6 +187,29 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(ente.File file) async {
   );
 }
 
+// check if the assetType is still the same. This can happen for livePhotos
+// if the user turns off the video using native photos app
+void _assertFileType(AssetEntity asset, ente.File file) {
+  final assetType = fileTypeFromAsset(asset);
+  if (assetType == file.fileType) {
+    return;
+  }
+  if (io.Platform.isIOS || io.Platform.isMacOS) {
+    if (assetType == FileType.image && file.fileType == FileType.livePhoto) {
+      throw InvalidFileError(
+          'id ${asset.id}', InvalidReason.imageToLivePhotoTypeChanged,);
+    } else if (assetType == FileType.livePhoto &&
+        file.fileType == FileType.image) {
+      throw InvalidFileError(
+          'id ${asset.id}', InvalidReason.livePhotoToImageTypeChanged,);
+    }
+  }
+  throw InvalidFileError(
+    'fileType mismatch for id ${asset.id} assetType $assetType fileType ${file.fileType}',
+    InvalidReason.unknown,
+  );
+}
+
 Future<void> _decorateEnteFileData(ente.File file, AssetEntity asset) async {
   // h4ck to fetch location data if missing (thank you Android Q+) lazily only during uploads
   if (file.location == null ||