Skip to content

armgit / ente

Public Imported from GitHub
0

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

locker: reuse id-keyed decrypted cache on open

Neeraj Guptac062746e27d1
diff --git a/mobile/apps/locker/lib/services/files/download/file_downloader.dart b/mobile/apps/locker/lib/services/files/download/file_downloader.dart
index 5382301151..849992730b 100644
--- a/mobile/apps/locker/lib/services/files/download/file_downloader.dart
+++ b/mobile/apps/locker/lib/services/files/download/file_downloader.dart
@@ -14,6 +14,24 @@ import 'package:path/path.dart' as p;
final _logger = Logger("FileDownloader");
+String getCachedEncryptedFilePath(EnteFile file) {
+ final String cacheDir = Configuration.instance.getCacheDirectory();
+ return "$cacheDir${file.uploadedFileID}.encrypted";
+}
+
+String getCachedDecryptedFilePath(EnteFile file) {
+ final String cacheDir = Configuration.instance.getCacheDirectory();
+ final String extension = _safeExtension(file.displayName);
+ return "$cacheDir${file.uploadedFileID}.decrypted$extension";
+}
+
+String _safeExtension(String fileName) {
+ final ext = p.extension(p.basename(fileName));
+ if (ext.isEmpty) return '';
+ final sanitized = ext.replaceAll(RegExp(r'[\\/:*?"<>|]'), '');
+ return sanitized == '.' ? '' : sanitized;
+}
+
Future<File?> downloadAndDecrypt(
EnteFile file,
Uint8List fileKey, {
@@ -25,10 +43,9 @@ Future<File?> downloadAndDecrypt(
_logger.info('$logPrefix starting download');
final String tempDir = Configuration.instance.getTempDirectory();
- final String cacheDir = Configuration.instance.getCacheDirectory();
- final String cachedEncryptedFilePath =
- "$cacheDir${file.uploadedFileID}.encrypted";
+ final String cachedEncryptedFilePath = getCachedEncryptedFilePath(file);
+ final String cachedDecryptedFilePath = getCachedDecryptedFilePath(file);
final String tempEncryptedFilePath =
"$tempDir${file.uploadedFileID}.encrypted";
String encryptedFilePath = tempEncryptedFilePath;
@@ -37,13 +54,25 @@ Future<File?> downloadAndDecrypt(
bool downloadedFreshEncryptedFile = false;
final String safeDisplayName = p.basename(file.displayName);
- final String decryptedFilePath =
- "$tempDir${file.uploadedFileID}_$safeDisplayName";
+ final String decryptedFilePath = shouldUseCache
+ ? cachedDecryptedFilePath
+ : "$tempDir${file.uploadedFileID}_$safeDisplayName";
final File decryptedFile = File(decryptedFilePath);
final startTime = DateTime.now().millisecondsSinceEpoch;
try {
+ if (shouldUseCache && await decryptedFile.exists()) {
+ final decryptedSize = await decryptedFile.length();
+ if (decryptedSize > 0) {
+ _logger.info('$logPrefix using cached decrypted file');
+ progressCallback?.call(decryptedSize, decryptedSize);
+ return decryptedFile;
+ } else {
+ await decryptedFile.delete();
+ }
+ }
+
bool shouldDownload = true;
if (shouldUseCache) {
final cachedEncryptedFile = File(cachedEncryptedFilePath);
diff --git a/mobile/apps/locker/lib/utils/file_util.dart b/mobile/apps/locker/lib/utils/file_util.dart
index 58c0679e3a..c21fd96039 100644
--- a/mobile/apps/locker/lib/utils/file_util.dart
+++ b/mobile/apps/locker/lib/utils/file_util.dart
@@ -37,6 +37,12 @@ class FileUtil {
}
}
+ final cachedDecryptedFile = File(getCachedDecryptedFilePath(file));
+ if (await cachedDecryptedFile.exists()) {
+ await _launchFile(context, cachedDecryptedFile, file.displayName);
+ return;
+ }
+
final dialog = createProgressDialog(
context,
context.l10n.downloading,