Skip to content

armgit / ente

Public Imported from GitHub
0

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

Use content identity for changed-file checks

Neeraj Gupta6aed62d83ec5
diff --git a/mobile/apps/photos/lib/module/upload/service/local_file_update_service.dart b/mobile/apps/photos/lib/module/upload/service/local_file_update_service.dart
index b36fffa47f..e84a58011a 100644
--- a/mobile/apps/photos/lib/module/upload/service/local_file_update_service.dart
+++ b/mobile/apps/photos/lib/module/upload/service/local_file_update_service.dart
@@ -1,8 +1,5 @@
import 'dart:async';
-import 'dart:io';
-import 'package:ente_pure_utils/ente_pure_utils.dart'
- show deleteFileSystemEntityIfPresent;
import 'package:logging/logging.dart';
import "package:photos/core/configuration.dart";
import 'package:photos/core/errors.dart';
@@ -11,7 +8,6 @@ import 'package:photos/db/files_db.dart';
import 'package:photos/models/file/file.dart';
import 'package:photos/models/file/file_type.dart';
import 'package:photos/module/download/file.dart';
-import 'package:photos/module/upload/model/media_upload_data.dart';
import "package:photos/module/upload/upload_data.dart";
import 'package:shared_preferences/shared_preferences.dart';
@@ -151,12 +147,11 @@ class LocalFileUpdateService {
processedIDs.add(file.localID!);
continue;
}
- MediaUploadData uploadData;
try {
- uploadData = await getUploadData(file);
+ final contentIdentity = await getFileContentIdentity(file);
if (file.hash != null &&
- (file.hash == uploadData.hashData.fileHash ||
- file.hash == uploadData.hashData.zipHash)) {
+ (file.hash == contentIdentity.fileHash ||
+ file.hash == contentIdentity.zipHash)) {
_logger.info("Skip file update as hash matched ${file.tag}");
} else {
_logger.info(
@@ -206,25 +201,4 @@ class LocalFileUpdateService {
FileUpdationDB.modificationTimeUpdated,
);
}
-
- Future<MediaUploadData> getUploadData(EnteFile file) async {
- final mediaUploadData = await getUploadDataFromEnteFile(file);
- // delete the file from app's internal cache if it was copied to app
- // for upload. Shared Media should only be cleared when the upload
- // succeeds.
- await _deleteCopiedIOSUploadFile(mediaUploadData.sourceFile);
- return mediaUploadData;
- }
-
- Future<void> _deleteCopiedIOSUploadFile(File? sourceFile) async {
- if (!Platform.isIOS || sourceFile == null) {
- return;
- }
- final deleted = await deleteFileSystemEntityIfPresent(sourceFile);
- if (!deleted) {
- _logger.info(
- "Copied upload temp file already missing: ${sourceFile.path}",
- );
- }
- }
}
diff --git a/mobile/apps/photos/lib/module/upload/upload_data.dart b/mobile/apps/photos/lib/module/upload/upload_data.dart
index 018b6804b5..5c92e8caf2 100644
--- a/mobile/apps/photos/lib/module/upload/upload_data.dart
+++ b/mobile/apps/photos/lib/module/upload/upload_data.dart
@@ -44,6 +44,43 @@ Future<MediaUploadData> getUploadDataFromEnteFile(
}
}
+/// Computes only the hashes needed to decide whether a local file changed.
+Future<FileHashData> getFileContentIdentity(EnteFile file) async {
+ if (file.isSharedMediaToAppSandbox) {
+ final sourceFile = File(getSharedMediaFilePath(file));
+ if (!sourceFile.existsSync()) {
+ throw InvalidFileError(
+ "source missing in sandbox",
+ InvalidReason.sourceFileMissing,
+ );
+ }
+ return FileHashData(
+ CryptoUtil.bin2base64(await CryptoUtil.getHash(sourceFile)),
+ );
+ }
+
+ final asset = await _getAsset(file);
+ final sourceFile = await _getOriginFile(asset, file);
+ try {
+ final sourceHash = CryptoUtil.bin2base64(
+ await CryptoUtil.getHash(sourceFile),
+ );
+ if (file.fileType == FileType.livePhoto && Platform.isIOS) {
+ final livePhoto = await getLivePhotoHashDataForComparison(
+ file,
+ sourceFile,
+ sourceHash,
+ );
+ return FileHashData(livePhoto.fileHash, zipHash: livePhoto.zipHash);
+ }
+ return FileHashData(sourceHash);
+ } finally {
+ if (Platform.isIOS) {
+ await deleteFileSystemEntityIfPresent(sourceFile);
+ }
+ }
+}
+
Future<MediaUploadData> _getMediaUploadDataFromAssetFile(
EnteFile file,
bool parseExif,
@@ -57,54 +94,12 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(
String? cameraMake;
String? cameraModel;
- // The timeouts are to safeguard against https://github.com/CaiJingLong/flutter_photo_manager/issues/467
- final asset = await file.getAsset
- .timeout(const Duration(seconds: 3))
- .catchError((e) async {
- if (e is TimeoutException) {
- _logger.info("Asset fetch timed out for " + file.toString());
- return await file.getAsset;
- } else {
- throw e;
- }
- });
- if (asset == null) {
- throw InvalidFileError("", InvalidReason.assetDeleted);
- }
- _assertFileType(asset, file);
+ final asset = await _getAsset(file);
file.fileSubType = asset.subtype;
if (file.fileType == FileType.video) {
file.duration = asset.duration;
}
- if (Platform.isIOS) {
- trackOriginFetchForUploadOrML.put(file.localID!, true);
- }
- try {
- sourceFile = await asset.originFile
- .timeout(const Duration(seconds: 15))
- .catchError((e) async {
- if (e is TimeoutException) {
- _logger.info("Origin file fetch timed out for " + file.tag);
- return await asset.originFile;
- } else {
- throw e;
- }
- });
- } catch (e) {
- if (isPHPhotosNetworkError(e)) {
- throw InvalidFileError(
- phPhotosResourceUnavailableReason,
- InvalidReason.photosResourceUnavailable,
- );
- }
- rethrow;
- }
- if (sourceFile == null || !sourceFile.existsSync()) {
- throw InvalidFileError(
- "id: ${file.localID}",
- InvalidReason.sourceFileMissing,
- );
- }
+ sourceFile = await _getOriginFile(asset, file);
try {
if (parseExif && shouldReadExif(file)) {
exifData = await tryExifFromFile(sourceFile);
@@ -173,6 +168,58 @@ Future<MediaUploadData> _getMediaUploadDataFromAssetFile(
}
}
+Future<AssetEntity> _getAsset(EnteFile file) async {
+ // The timeout safeguards against
+ // https://github.com/CaiJingLong/flutter_photo_manager/issues/467.
+ final asset = await file.getAsset
+ .timeout(const Duration(seconds: 3))
+ .catchError((e) async {
+ if (e is TimeoutException) {
+ _logger.info("Asset fetch timed out for " + file.toString());
+ return await file.getAsset;
+ }
+ throw e;
+ });
+ if (asset == null) {
+ throw InvalidFileError("", InvalidReason.assetDeleted);
+ }
+ _assertFileType(asset, file);
+ return asset;
+}
+
+Future<File> _getOriginFile(AssetEntity asset, EnteFile file) async {
+ if (Platform.isIOS) {
+ trackOriginFetchForUploadOrML.put(file.localID!, true);
+ }
+ File? sourceFile;
+ try {
+ sourceFile = await asset.originFile
+ .timeout(const Duration(seconds: 15))
+ .catchError((e) async {
+ if (e is TimeoutException) {
+ _logger.info("Origin file fetch timed out for " + file.tag);
+ return await asset.originFile;
+ }
+ throw e;
+ });
+ } catch (e) {
+ if (isPHPhotosNetworkError(e)) {
+ throw InvalidFileError(
+ phPhotosResourceUnavailableReason,
+ InvalidReason.photosResourceUnavailable,
+ );
+ }
+ rethrow;
+ }
+ if (sourceFile == null || !sourceFile.existsSync()) {
+ throw InvalidFileError(
+ "id: ${file.localID}",
+ InvalidReason.sourceFileMissing,
+ );
+ }
+ return sourceFile;
+}
+
Future<Uint8List?> _getThumbnailForUpload(
AssetEntity asset,
EnteFile file,