From 4bab7ca5861d72c57a8da98c84cb47214da6c2f8 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta Date: Sat, 24 Jul 2021 19:00:33 +0530 Subject: [PATCH] Store manual backup media in a separate directory --- lib/core/configuration.dart | 11 +++++++++-- lib/core/constants.dart | 2 +- lib/models/file.dart | 2 +- lib/utils/file_uploader_util.dart | 4 ++-- lib/utils/file_util.dart | 9 ++++++--- lib/utils/share_util.dart | 15 +++++++++++---- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lib/core/configuration.dart b/lib/core/configuration.dart index dbc1b1e2d..b82a2b612 100644 --- a/lib/core/configuration.dart +++ b/lib/core/configuration.dart @@ -64,6 +64,7 @@ class Configuration { FlutterSecureStorage _secureStorage; String _tempDirectory; String _thumbnailCacheDirectory; + String _sharedMediaDirectory; String _volatilePassword; final _secureStorageOptionsIOS = @@ -90,9 +91,11 @@ class Configuration { _logger.warning(e); } tempDirectory.createSync(recursive: true); - _thumbnailCacheDirectory = - (await getTemporaryDirectory()).path + "/thumbnail-cache"; + var tempDirectoryPath = (await getTemporaryDirectory()).path; + _thumbnailCacheDirectory = tempDirectoryPath + "/thumbnail-cache"; io.Directory(_thumbnailCacheDirectory).createSync(recursive: true); + _sharedMediaDirectory = tempDirectoryPath + "/ente-shared-media"; + io.Directory(_sharedMediaDirectory).createSync(recursive: true); if (!_preferences.containsKey(tokenKey)) { await _secureStorage.deleteAll(iOptions: _secureStorageOptionsIOS); } else { @@ -410,6 +413,10 @@ class Configuration { return _thumbnailCacheDirectory; } + String getSharedMediaCacheDirectory() { + return _sharedMediaDirectory; + } + bool hasConfiguredAccount() { return getToken() != null && _key != null; } diff --git a/lib/core/constants.dart b/lib/core/constants.dart index 185ac10f4..a000c1540 100644 --- a/lib/core/constants.dart +++ b/lib/core/constants.dart @@ -13,4 +13,4 @@ const int kAndroid11SDKINT = 30; const int kGalleryLoadStartTime = -8000000000000000; // Wednesday, March 6, 1748 // used to identify which ente file are available in app cache -const String kAppCacheIdentifier = 'ente-cache://'; +const String kSharedMediaIdentifier = 'ente-shared://'; diff --git a/lib/models/file.dart b/lib/models/file.dart index 2e31f3802..056574301 100644 --- a/lib/models/file.dart +++ b/lib/models/file.dart @@ -137,7 +137,7 @@ class File { bool isCachedInAppSandbox() { - return localID != null && localID.startsWith(kAppCacheIdentifier); + return localID != null && localID.startsWith(kSharedMediaIdentifier); } bool hasLocation() { diff --git a/lib/utils/file_uploader_util.dart b/lib/utils/file_uploader_util.dart index c1c33d6d0..af628f954 100644 --- a/lib/utils/file_uploader_util.dart +++ b/lib/utils/file_uploader_util.dart @@ -100,7 +100,7 @@ Future _getMediaUploadDataFromAppCache(ente.File file) async { io.File sourceFile; Uint8List thumbnailData; bool isDeleted = false; - var localPath = file.localID.replaceAll(kAppCacheIdentifier, ''); + var localPath = getSharedMediaFilePath(file); sourceFile = io.File(localPath); if (!sourceFile.existsSync()) { _logger.warning("File doesn't exist in app sandbox"); @@ -111,7 +111,7 @@ Future _getMediaUploadDataFromAppCache(ente.File file) async { } Future getThumbnailFromInAppCacheFile(ente.File file) async { - var localPath = file.localID.replaceAll(kAppCacheIdentifier, ''); + var localPath = getSharedMediaFilePath(file); var thumbnailData = io.File(localPath).readAsBytesSync(); int compressionAttempts = 0; while (thumbnailData.length > kThumbnailDataLimit && diff --git a/lib/utils/file_util.dart b/lib/utils/file_util.dart index 311c1d471..4e0887ac3 100644 --- a/lib/utils/file_util.dart +++ b/lib/utils/file_util.dart @@ -9,7 +9,6 @@ import 'package:flutter_sodium/flutter_sodium.dart'; import 'package:logging/logging.dart'; import 'package:path/path.dart'; import 'package:photos/core/cache/image_cache.dart'; -import 'package:photos/core/cache/thumbnail_cache.dart'; import 'package:photos/core/cache/video_cache_manager.dart'; import 'package:photos/core/configuration.dart'; import 'package:photos/core/constants.dart'; @@ -47,8 +46,7 @@ Future getFile(ente.File file) async { Future _getLocalDiskFile(ente.File file) async { if (file.isCachedInAppSandbox()) { - var localPath = file.localID.replaceAll(kAppCacheIdentifier, ''); - return io.File(localPath); + return io.File(getSharedMediaFilePath(file)); } else { return file.getAsset().then((asset) async { if (asset == null || !(await asset.exists)) { @@ -59,6 +57,11 @@ Future _getLocalDiskFile(ente.File file) async { } } +String getSharedMediaFilePath(ente.File file) { + return Configuration.instance.getSharedMediaCacheDirectory() + + "/" + file.localID.replaceAll(kSharedMediaIdentifier, ''); +} + void preloadThumbnail(ente.File file) { if (file.isRemoteFile()) { getThumbnailFromServer(file); diff --git a/lib/utils/share_util.dart b/lib/utils/share_util.dart index 5a48a17e4..e90833a37 100644 --- a/lib/utils/share_util.dart +++ b/lib/utils/share_util.dart @@ -9,6 +9,7 @@ import 'package:photos/core/constants.dart'; import 'package:photos/models/file_type.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:share/share.dart'; +import 'package:photos/core/configuration.dart'; import 'package:flutter/widgets.dart'; import 'package:photos/models/file.dart'; import 'package:photos/utils/dialog_util.dart'; @@ -38,12 +39,18 @@ Future> convertIncomingSharedMediaToFile( List localFiles = []; for (var media in sharedMedia) { var enteFile = File(); - enteFile.localID = kAppCacheIdentifier + media.path; - enteFile.collectionID = collectionID; - enteFile.fileType = FileType.image; + // fileName: img_x.jpg enteFile.title = basename(media.path); - var exifMap = await readExifFromFile(dartio.File(media.path)); + var ioFile = dartio.File(media.path); + ioFile = ioFile.renameSync(Configuration.instance.getSharedMediaCacheDirectory() + + "/" + + enteFile.title); + enteFile.localID = kSharedMediaIdentifier + enteFile.title; + enteFile.collectionID = collectionID; + enteFile.fileType = FileType.image; + + var exifMap = await readExifFromFile(ioFile); if (exifMap != null && exifMap["Image DateTime"] != null && '0000:00:00 00:00:00' != exifMap["Image DateTime"].toString()) {