Skip to content

armgit / ente

Public Imported from GitHub
0

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

Move live photo archiving into live photo module

Neeraj Gupta0f08fbc6c512
diff --git a/mobile/apps/photos/lib/module/live_photo/archive.dart b/mobile/apps/photos/lib/module/live_photo/archive.dart
new file mode 100644
index 0000000000..696de83e14
--- /dev/null
+++ b/mobile/apps/photos/lib/module/live_photo/archive.dart
@@ -0,0 +1,73 @@
+import "dart:io";
+
+import "package:archive/archive_io.dart";
+import "package:computer/computer.dart";
+import "package:path/path.dart";
+
+enum LivePhotoArchivePartType { image, video }
+
+class LivePhotoArchivePart {
+ const LivePhotoArchivePart({
+ required this.type,
+ required this.fileName,
+ required this.bytes,
+ });
+
+ final LivePhotoArchivePartType type;
+ final String fileName;
+ final List<int> bytes;
+}
+
+Future<void> _computeLivePhotoArchive(Map<String, dynamic> args) async {
+ final String archivePath = args['archivePath'];
+ final String imagePath = args['imagePath'];
+ final String videoPath = args['videoPath'];
+ final encoder = ZipFileEncoder();
+ encoder.create(archivePath);
+ await encoder.addFile(File(imagePath), "image${extension(imagePath)}");
+ await encoder.addFile(File(videoPath), "video${extension(videoPath)}");
+ await encoder.close();
+}
+
+Future<void> createLivePhotoArchive({
+ required String archivePath,
+ required String imagePath,
+ required String videoPath,
+}) {
+ return Computer.shared().compute(
+ _computeLivePhotoArchive,
+ param: {
+ 'archivePath': archivePath,
+ 'imagePath': imagePath,
+ 'videoPath': videoPath,
+ },
+ taskName: 'zip',
+ );
+}
+
+List<LivePhotoArchivePart> decodeLivePhotoArchive(List<int> bytes) {
+ final archive = ZipDecoder().decodeBytes(bytes);
+ final parts = <LivePhotoArchivePart>[];
+ for (final entry in archive) {
+ if (!entry.isFile) {
+ continue;
+ }
+ final type = switch (entry.name) {
+ final name when name.startsWith("image") =>
+ LivePhotoArchivePartType.image,
+ final name when name.startsWith("video") =>
+ LivePhotoArchivePartType.video,
+ _ => null,
+ };
+ if (type != null) {
+ parts.add(
+ LivePhotoArchivePart(
+ type: type,
+ fileName: entry.name,
+ bytes: entry.content,
+ ),
+ );
+ }
+ }
+ return parts;
+}
diff --git a/mobile/apps/photos/lib/utils/file_uploader_util.dart b/mobile/apps/photos/lib/utils/file_uploader_util.dart
index 17cc1668f0..239714af45 100644
--- a/mobile/apps/photos/lib/utils/file_uploader_util.dart
+++ b/mobile/apps/photos/lib/utils/file_uploader_util.dart
@@ -2,8 +2,6 @@ import 'dart:async';
import "dart:io";
import 'dart:typed_data';
-import "package:archive/archive_io.dart";
-import "package:computer/computer.dart";
import 'package:ente_crypto/ente_crypto.dart';
import "package:ente_pure_utils/ente_pure_utils.dart"
show deleteFileSystemEntityIfPresent;
@@ -21,6 +19,7 @@ import "package:photos/models/file/extensions/file_props.dart";
import 'package:photos/models/file/file.dart';
import 'package:photos/models/file/file_type.dart';
import "package:photos/models/location/location.dart";
+import "package:photos/module/live_photo/archive.dart";
import 'package:photos/module/upload/model/media_upload_data.dart';
import "package:photos/services/sync/local_sync_service.dart";
import "package:photos/src/rust/api/motion_photo_api.dart";
@@ -135,8 +134,8 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(
_logger.info(
"Creating zip for live photo from " + basename(livePhotoPath),
);
- await zip(
- zipPath: livePhotoPath,
+ await createLivePhotoArchive(
+ archivePath: livePhotoPath,
imagePath: sourceFile.path,
videoPath: videoUrl.path,
);
@@ -199,29 +198,6 @@ Future<int?> motionVideoIndex(Map<String, dynamic> args) async {
return videoIndex?.start.toInt();
}
-Future<void> _computeZip(Map<String, dynamic> args) async {
- final String zipPath = args['zipPath'];
- final String imagePath = args['imagePath'];
- final String videoPath = args['videoPath'];
- final encoder = ZipFileEncoder();
- encoder.create(zipPath);
- await encoder.addFile(File(imagePath), "image" + extension(imagePath));
- await encoder.addFile(File(videoPath), "video" + extension(videoPath));
- await encoder.close();
-}
-
-Future<void> zip({
- required String zipPath,
- required String imagePath,
- required String videoPath,
-}) {
- return Computer.shared().compute(
- _computeZip,
- param: {'zipPath': zipPath, 'imagePath': imagePath, 'videoPath': videoPath},
- taskName: 'zip',
- );
-}
-
Future<Uint8List?> _getThumbnailForUpload(
AssetEntity asset,
EnteFile file,
diff --git a/mobile/apps/photos/lib/utils/file_util.dart b/mobile/apps/photos/lib/utils/file_util.dart
index 1fc7e95319..3bd9863d30 100644
--- a/mobile/apps/photos/lib/utils/file_util.dart
+++ b/mobile/apps/photos/lib/utils/file_util.dart
@@ -1,7 +1,6 @@
import 'dart:async';
import 'dart:io';
-import 'package:archive/archive.dart';
import 'package:crypto/crypto.dart';
import "package:dio/dio.dart";
import 'package:flutter/foundation.dart';
@@ -18,6 +17,7 @@ import 'package:photos/core/constants.dart';
import "package:photos/models/file/extensions/file_props.dart";
import 'package:photos/models/file/file.dart';
import 'package:photos/models/file/file_type.dart';
+import 'package:photos/module/live_photo/archive.dart';
import 'package:photos/utils/file_download_util.dart';
final _logger = Logger("FileUtil");
@@ -277,57 +277,52 @@ Future<_LivePhoto?> _downloadLivePhoto(
_logger.info("Decoded zipped live photo from " + decryptedFile.path);
File? imageFileCache, videoFileCache;
final List<int> bytes = await decryptedFile.readAsBytes();
- final Archive archive = ZipDecoder().decodeBytes(bytes);
+ final parts = decodeLivePhotoArchive(bytes);
final tempPath = Configuration.instance.getTempDirectory();
- // Extract the contents of Zip compressed archive to disk
- for (ArchiveFile archiveFile in archive) {
- if (archiveFile.isFile) {
- final String filename = archiveFile.name;
- final String fileExtension = getExtension(archiveFile.name);
- final String decodePath =
- tempPath + file.uploadedFileID.toString() + filename;
- final List<int> data = archiveFile.content;
- if (filename.startsWith("image")) {
- final imageFile = File(decodePath);
- await imageFile.create(recursive: true);
- await imageFile.writeAsBytes(data);
- File imageConvertedFile = imageFile;
- if ((fileExtension == "unknown") ||
- (Platform.isAndroid && fileExtension == "heic")) {
- final compressResult =
- await FlutterImageCompress.compressAndGetFile(
- decodePath,
- decodePath + ".jpg",
- keepExif: true,
- );
- await imageFile.delete();
- if (compressResult == null) {
- throw Exception("Failed to compress file");
- } else {
- imageConvertedFile = File(compressResult.path);
- }
+ for (final part in parts) {
+ final String fileExtension = getExtension(part.fileName);
+ final String decodePath =
+ tempPath + file.uploadedFileID.toString() + part.fileName;
+ if (part.type == LivePhotoArchivePartType.image) {
+ final imageFile = File(decodePath);
+ await imageFile.create(recursive: true);
+ await imageFile.writeAsBytes(part.bytes);
+ File imageConvertedFile = imageFile;
+ if ((fileExtension == "unknown") ||
+ (Platform.isAndroid && fileExtension == "heic")) {
+ final compressResult =
+ await FlutterImageCompress.compressAndGetFile(
+ decodePath,
+ decodePath + ".jpg",
+ keepExif: true,
+ );
+ await imageFile.delete();
+ if (compressResult == null) {
+ throw Exception("Failed to compress file");
+ } else {
+ imageConvertedFile = File(compressResult.path);
}
- imageFileCache = await DefaultCacheManager().putFile(
- file.downloadUrl,
- await imageConvertedFile.readAsBytes(),
- eTag: file.downloadUrl,
- maxAge: const Duration(days: 365),
- fileExtension: fileExtension,
- );
- await imageConvertedFile.delete();
- } else if (filename.startsWith("video")) {
- final videoFile = File(decodePath);
- await videoFile.create(recursive: true);
- await videoFile.writeAsBytes(data);
- videoFileCache = await VideoCacheManager.instance.putFileStream(
- file.downloadUrl,
- videoFile.openRead(),
- eTag: file.downloadUrl,
- maxAge: const Duration(days: 365),
- fileExtension: fileExtension,
- );
- await videoFile.delete();
}
+ imageFileCache = await DefaultCacheManager().putFile(
+ file.downloadUrl,
+ await imageConvertedFile.readAsBytes(),
+ eTag: file.downloadUrl,
+ maxAge: const Duration(days: 365),
+ fileExtension: fileExtension,
+ );
+ await imageConvertedFile.delete();
+ } else {
+ final videoFile = File(decodePath);
+ await videoFile.create(recursive: true);
+ await videoFile.writeAsBytes(part.bytes);
+ videoFileCache = await VideoCacheManager.instance.putFileStream(
+ file.downloadUrl,
+ videoFile.openRead(),
+ eTag: file.downloadUrl,
+ maxAge: const Duration(days: 365),
+ fileExtension: fileExtension,
+ );
+ await videoFile.delete();
}
}
if (imageFileCache != null && videoFileCache != null) {