Store manual backup media in a separate directory

This commit is contained in:
Neeraj Gupta 2021-07-24 19:00:33 +05:30
parent 2559e90352
commit 4bab7ca586
6 changed files with 30 additions and 13 deletions

View file

@ -64,6 +64,7 @@ class Configuration {
FlutterSecureStorage _secureStorage; FlutterSecureStorage _secureStorage;
String _tempDirectory; String _tempDirectory;
String _thumbnailCacheDirectory; String _thumbnailCacheDirectory;
String _sharedMediaDirectory;
String _volatilePassword; String _volatilePassword;
final _secureStorageOptionsIOS = final _secureStorageOptionsIOS =
@ -90,9 +91,11 @@ class Configuration {
_logger.warning(e); _logger.warning(e);
} }
tempDirectory.createSync(recursive: true); tempDirectory.createSync(recursive: true);
_thumbnailCacheDirectory = var tempDirectoryPath = (await getTemporaryDirectory()).path;
(await getTemporaryDirectory()).path + "/thumbnail-cache"; _thumbnailCacheDirectory = tempDirectoryPath + "/thumbnail-cache";
io.Directory(_thumbnailCacheDirectory).createSync(recursive: true); io.Directory(_thumbnailCacheDirectory).createSync(recursive: true);
_sharedMediaDirectory = tempDirectoryPath + "/ente-shared-media";
io.Directory(_sharedMediaDirectory).createSync(recursive: true);
if (!_preferences.containsKey(tokenKey)) { if (!_preferences.containsKey(tokenKey)) {
await _secureStorage.deleteAll(iOptions: _secureStorageOptionsIOS); await _secureStorage.deleteAll(iOptions: _secureStorageOptionsIOS);
} else { } else {
@ -410,6 +413,10 @@ class Configuration {
return _thumbnailCacheDirectory; return _thumbnailCacheDirectory;
} }
String getSharedMediaCacheDirectory() {
return _sharedMediaDirectory;
}
bool hasConfiguredAccount() { bool hasConfiguredAccount() {
return getToken() != null && _key != null; return getToken() != null && _key != null;
} }

View file

@ -13,4 +13,4 @@ const int kAndroid11SDKINT = 30;
const int kGalleryLoadStartTime = -8000000000000000; // Wednesday, March 6, 1748 const int kGalleryLoadStartTime = -8000000000000000; // Wednesday, March 6, 1748
// used to identify which ente file are available in app cache // used to identify which ente file are available in app cache
const String kAppCacheIdentifier = 'ente-cache://'; const String kSharedMediaIdentifier = 'ente-shared://';

View file

@ -137,7 +137,7 @@ class File {
bool isCachedInAppSandbox() { bool isCachedInAppSandbox() {
return localID != null && localID.startsWith(kAppCacheIdentifier); return localID != null && localID.startsWith(kSharedMediaIdentifier);
} }
bool hasLocation() { bool hasLocation() {

View file

@ -100,7 +100,7 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(ente.File file) async {
io.File sourceFile; io.File sourceFile;
Uint8List thumbnailData; Uint8List thumbnailData;
bool isDeleted = false; bool isDeleted = false;
var localPath = file.localID.replaceAll(kAppCacheIdentifier, ''); var localPath = getSharedMediaFilePath(file);
sourceFile = io.File(localPath); sourceFile = io.File(localPath);
if (!sourceFile.existsSync()) { if (!sourceFile.existsSync()) {
_logger.warning("File doesn't exist in app sandbox"); _logger.warning("File doesn't exist in app sandbox");
@ -111,7 +111,7 @@ Future<MediaUploadData> _getMediaUploadDataFromAppCache(ente.File file) async {
} }
Future<Uint8List> getThumbnailFromInAppCacheFile(ente.File file) async { Future<Uint8List> getThumbnailFromInAppCacheFile(ente.File file) async {
var localPath = file.localID.replaceAll(kAppCacheIdentifier, ''); var localPath = getSharedMediaFilePath(file);
var thumbnailData = io.File(localPath).readAsBytesSync(); var thumbnailData = io.File(localPath).readAsBytesSync();
int compressionAttempts = 0; int compressionAttempts = 0;
while (thumbnailData.length > kThumbnailDataLimit && while (thumbnailData.length > kThumbnailDataLimit &&

View file

@ -9,7 +9,6 @@ import 'package:flutter_sodium/flutter_sodium.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'package:photos/core/cache/image_cache.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/cache/video_cache_manager.dart';
import 'package:photos/core/configuration.dart'; import 'package:photos/core/configuration.dart';
import 'package:photos/core/constants.dart'; import 'package:photos/core/constants.dart';
@ -47,8 +46,7 @@ Future<io.File> getFile(ente.File file) async {
Future<io.File> _getLocalDiskFile(ente.File file) async { Future<io.File> _getLocalDiskFile(ente.File file) async {
if (file.isCachedInAppSandbox()) { if (file.isCachedInAppSandbox()) {
var localPath = file.localID.replaceAll(kAppCacheIdentifier, ''); return io.File(getSharedMediaFilePath(file));
return io.File(localPath);
} else { } else {
return file.getAsset().then((asset) async { return file.getAsset().then((asset) async {
if (asset == null || !(await asset.exists)) { if (asset == null || !(await asset.exists)) {
@ -59,6 +57,11 @@ Future<io.File> _getLocalDiskFile(ente.File file) async {
} }
} }
String getSharedMediaFilePath(ente.File file) {
return Configuration.instance.getSharedMediaCacheDirectory()
+ "/" + file.localID.replaceAll(kSharedMediaIdentifier, '');
}
void preloadThumbnail(ente.File file) { void preloadThumbnail(ente.File file) {
if (file.isRemoteFile()) { if (file.isRemoteFile()) {
getThumbnailFromServer(file); getThumbnailFromServer(file);

View file

@ -9,6 +9,7 @@ import 'package:photos/core/constants.dart';
import 'package:photos/models/file_type.dart'; import 'package:photos/models/file_type.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:share/share.dart'; import 'package:share/share.dart';
import 'package:photos/core/configuration.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:photos/models/file.dart'; import 'package:photos/models/file.dart';
import 'package:photos/utils/dialog_util.dart'; import 'package:photos/utils/dialog_util.dart';
@ -38,12 +39,18 @@ Future<List<File>> convertIncomingSharedMediaToFile(
List<File> localFiles = []; List<File> localFiles = [];
for (var media in sharedMedia) { for (var media in sharedMedia) {
var enteFile = File(); var enteFile = File();
enteFile.localID = kAppCacheIdentifier + media.path; // fileName: img_x.jpg
enteFile.collectionID = collectionID;
enteFile.fileType = FileType.image;
enteFile.title = basename(media.path); 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 && if (exifMap != null &&
exifMap["Image DateTime"] != null && exifMap["Image DateTime"] != null &&
'0000:00:00 00:00:00' != exifMap["Image DateTime"].toString()) { '0000:00:00 00:00:00' != exifMap["Image DateTime"].toString()) {