From 0a2072452a2bc82878a046cf249c24f08d9816b4 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Wed, 3 May 2023 13:32:57 +0530 Subject: [PATCH] Detect motion photos during upload --- lib/utils/file_uploader.dart | 13 +++++++++---- lib/utils/file_uploader_util.dart | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/utils/file_uploader.dart b/lib/utils/file_uploader.dart index 2eaef0a1d..d4c79bf94 100644 --- a/lib/utils/file_uploader.dart +++ b/lib/utils/file_uploader.dart @@ -458,12 +458,17 @@ class FileUploader { MetadataRequest? pubMetadataRequest; if ((mediaUploadData.height ?? 0) != 0 && (mediaUploadData.width ?? 0) != 0) { + final pubMetadata = { + publicMagicKeyHeight: mediaUploadData.height, + publicMagicKeyWidth: mediaUploadData.width + }; + if (mediaUploadData.motionPhotoStartIndex != null) { + pubMetadata[pubMotionVideoIndex] = + mediaUploadData.motionPhotoStartIndex; + } pubMetadataRequest = await getPubMetadataRequest( file, - { - publicMagicKeyHeight: mediaUploadData.height, - publicMagicKeyWidth: mediaUploadData.width - }, + pubMetadata, fileAttributes.key!, ); } diff --git a/lib/utils/file_uploader_util.dart b/lib/utils/file_uploader_util.dart index ddf3251de..3f0e71644 100644 --- a/lib/utils/file_uploader_util.dart +++ b/lib/utils/file_uploader_util.dart @@ -6,6 +6,7 @@ import 'dart:ui' as ui; import 'package:archive/archive_io.dart'; import 'package:logging/logging.dart'; +import "package:motion_photos/motion_photos.dart"; import 'package:motionphoto/motionphoto.dart'; import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; @@ -33,6 +34,9 @@ class MediaUploadData { final FileHashData? hashData; final int? height; final int? width; + // For android motion photos, the startIndex is the index of the first frame + // For iOS, this value will be always null. + final int? motionPhotoStartIndex; MediaUploadData( this.sourceFile, @@ -41,6 +45,7 @@ class MediaUploadData { this.hashData, { this.height, this.width, + this.motionPhotoStartIndex, }); } @@ -154,6 +159,15 @@ Future _getMediaUploadDataFromAssetFile(ente.File file) async { h = asset.height; w = asset.width; } + int? motionPhotoStartingIndex; + if (io.Platform.isAndroid && asset.type == AssetType.image) { + try { + motionPhotoStartingIndex = + MotionPhotos(sourceFile.path).getMotionVideoIndex()?.start; + } catch (e) { + _logger.severe('error while detecthing motion photo start index', e); + } + } return MediaUploadData( sourceFile, thumbnailData, @@ -161,6 +175,7 @@ Future _getMediaUploadDataFromAssetFile(ente.File file) async { FileHashData(fileHash, zipHash: zipHash), height: h, width: w, + motionPhotoStartIndex: motionPhotoStartingIndex, ); } @@ -198,7 +213,7 @@ Future getPubMetadataRequest( fileKey, ); return MetadataRequest( - version: file.pubMmdVersion, + version: file.pubMmdVersion == 0 ? 1 : file.pubMmdVersion, count: jsonToUpdate.length, data: CryptoUtil.bin2base64(encryptedMMd.encryptedData!), header: CryptoUtil.bin2base64(encryptedMMd.header!),