Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Separate Live Photo hashing from archive preparation

Neeraj Guptacd4456c74e0e
diff --git a/mobile/apps/photos/lib/module/live_photo/upload.dart b/mobile/apps/photos/lib/module/live_photo/upload.dart
index 452b129fba..58303bacd8 100644
--- a/mobile/apps/photos/lib/module/live_photo/upload.dart
+++ b/mobile/apps/photos/lib/module/live_photo/upload.dart
@@ -19,6 +19,10 @@ typedef LivePhotoUploadData = ({
String zipHash,
});
+typedef LivePhotoHashData = ({String fileHash, String? zipHash});
+
+typedef _LivePhotoVideoData = ({File file, String fileHash});
+
final _logger = Logger('LivePhotoUpload');
/// Uploads may prepare several files concurrently. Keep this per-file pipeline
@@ -28,6 +32,63 @@ Future<LivePhotoUploadData> prepareLivePhotoForUpload(
EnteFile file,
File imageFile,
String imageHash,
+) async {
+ final videoData = await _getLivePhotoVideoData(file, imageHash);
+ final archiveFile = await _createLivePhotoArchiveFile(
+ file,
+ imageFile,
+ videoData.file,
+ );
+ try {
+ // photo_manager can return the same iOS temp copy to concurrent upload or
+ // hash-check paths, where cleanup may have already run.
+ await deleteFileSystemEntityIfPresent(imageFile);
+ final zipHash = CryptoUtil.bin2base64(
+ await CryptoUtil.getHash(archiveFile),
+ );
+ return (
+ sourceFile: archiveFile,
+ fileHash: videoData.fileHash,
+ zipHash: zipHash,
+ );
+ } catch (_) {
+ await deleteFileSystemEntityIfPresent(archiveFile);
+ rethrow;
+ }
+}
+
+/// Computes the modern component hash first and creates the legacy ZIP only
+/// when it is still needed to compare an older stored hash.
+Future<LivePhotoHashData> getLivePhotoHashDataForComparison(
+ EnteFile file,
+ File imageFile,
+ String imageHash,
+) async {
+ final videoData = await _getLivePhotoVideoData(file, imageHash);
+ final storedHash = file.hash;
+ if (storedHash == null ||
+ storedHash == videoData.fileHash ||
+ storedHash.contains(kLivePhotoHashSeparator)) {
+ return (fileHash: videoData.fileHash, zipHash: null);
+ }
+ final archiveFile = await _createLivePhotoArchiveFile(
+ file,
+ imageFile,
+ videoData.file,
+ );
+ try {
+ return (
+ fileHash: videoData.fileHash,
+ zipHash: CryptoUtil.bin2base64(await CryptoUtil.getHash(archiveFile)),
+ );
+ } finally {
+ await deleteFileSystemEntityIfPresent(archiveFile);
+ }
+}
+
+Future<_LivePhotoVideoData> _getLivePhotoVideoData(
+ EnteFile file,
+ String imageHash,
) async {
final videoFile = await Motionphoto.getLivePhotoFile(file.localID!);
if (videoFile == null || !videoFile.existsSync()) {
@@ -39,6 +100,14 @@ Future<LivePhotoUploadData> prepareLivePhotoForUpload(
final videoHash = CryptoUtil.bin2base64(await CryptoUtil.getHash(videoFile));
final fileHash = '$imageHash$kLivePhotoHashSeparator$videoHash';
+ return (file: videoFile, fileHash: fileHash);
+}
+
+Future<File> _createLivePhotoArchiveFile(
+ EnteFile file,
+ File imageFile,
+ File videoFile,
+) async {
final archivePath =
'${Configuration.instance.getTempDirectory()}${const Uuid().v4()}_${file.generatedID}.elp';
final archiveFile = File(archivePath);
@@ -50,13 +119,7 @@ Future<LivePhotoUploadData> prepareLivePhotoForUpload(
imagePath: imageFile.path,
videoPath: videoFile.path,
);
- // photo_manager can return the same iOS temp copy to concurrent upload or
- // hash-check paths, where cleanup may have already run.
- await deleteFileSystemEntityIfPresent(imageFile);
- final zipHash = CryptoUtil.bin2base64(
- await CryptoUtil.getHash(archiveFile),
- );
- return (sourceFile: archiveFile, fileHash: fileHash, zipHash: zipHash);
+ return archiveFile;
} catch (_) {
await deleteFileSystemEntityIfPresent(archiveFile);
rethrow;