π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
fix: improve file opening logic and error handling in FileUtil
AmanRajSinghMouryac79320bce213
diff --git a/mobile/apps/locker/lib/utils/file_util.dart b/mobile/apps/locker/lib/utils/file_util.dartindex e7a0fbfc8c..80c5c54f0a 100644--- a/mobile/apps/locker/lib/utils/file_util.dart+++ b/mobile/apps/locker/lib/utils/file_util.dart@@ -34,22 +34,27 @@ class FileUtil {return _openInfoFile(context, file);}- if (file.localPath != null) {- final localFile = File(file.localPath!);- if (await localFile.exists()) {- await _launchFile(context, localFile, displayName: file.displayName);- return;- }+ if (file.uploadedFileID == null) {+ await showGenericErrorBottomSheet(+ context: context,+ error: Exception(context.l10n.errorOpeningFile),+ );+ return;}final cachedDecryptedFile = File(getCachedDecryptedFilePath(file));if (await cachedDecryptedFile.exists()) {- await _launchFile(- context,- cachedDecryptedFile,- displayName: file.displayName,- );- return;+ final cachedSize = await cachedDecryptedFile.length();+ if (cachedSize > 0) {+ await _launchFile(+ context,+ cachedDecryptedFile,+ displayName: file.displayName,+ lockerFile: file,+ );+ return;+ }+ await cachedDecryptedFile.delete();}final dialog = createProgressDialog(@@ -64,9 +69,7 @@ class FileUtil {void progressCallback(int downloaded, int total) {if (total > 0 && downloaded >= 0) {final percentage = ((downloaded / total) * 100).clamp(0, 100).round();- dialog.update(- message: context.l10n.downloadingProgress(percentage),- );+ dialog.update(message: context.l10n.downloadingProgress(percentage));} else {dialog.update(message: context.l10n.downloading);}@@ -85,6 +88,7 @@ class FileUtil {context,decryptedFile,displayName: file.displayName,+ lockerFile: file,);} else {await showAlertBottomSheet(@@ -104,17 +108,11 @@ class FileUtil {}} catch (e) {await dialog.hide();- await showGenericErrorBottomSheet(- context: context,- error: e,- );+ await showGenericErrorBottomSheet(context: context, error: e);}}- static Future<bool> downloadFile(- BuildContext context,- EnteFile file,- ) {+ static Future<bool> downloadFile(BuildContext context, EnteFile file) {return _downloadFiles(context, [file]);}@@ -157,8 +155,9 @@ class FileUtil {// Skip info items for now; they are meant to be viewed in-app.if (InfoFileService.instance.isInfoFile(file)) {- _logger- .fine('Skipping info file download (ID: ${file.uploadedFileID})');+ _logger.fine(+ 'Skipping info file download (ID: ${file.uploadedFileID})',+ );if (!hasShownInfoSkipToast) {hasShownInfoSkipToast = true;showToast(@@ -205,15 +204,9 @@ class FileUtil {_logger.severe('Failed to save files', e, s);if (context.mounted) {if (e is UnsupportedError) {- showToast(- context,- 'This file type is not supported for download',- );+ showToast(context, 'This file type is not supported for download');} else {- showToast(- context,- context.l10n.failedToDownloadOrDecrypt,- );+ showToast(context, context.l10n.failedToDownloadOrDecrypt);}}return false;@@ -369,10 +362,7 @@ class FileUtil {Widget page;switch (infoItem.type) {case InfoType.note:- page = PersonalNotePage(- mode: InfoPageMode.view,- existingFile: file,- );+ page = PersonalNotePage(mode: InfoPageMode.view, existingFile: file);break;case InfoType.accountCredential:page = AccountCredentialsPage(@@ -394,14 +384,11 @@ class FileUtil {break;}- await Navigator.of(context).push(- MaterialPageRoute(builder: (context) => page),- );+ await Navigator.of(+ context,+ ).push(MaterialPageRoute(builder: (context) => page));} catch (e) {- await showGenericErrorBottomSheet(- context: context,- error: e,- );+ await showGenericErrorBottomSheet(context: context, error: e);}}@@ -409,30 +396,152 @@ class FileUtil {BuildContext context,File file, {String? displayName,+ EnteFile? lockerFile,}) async {File fileToOpen = file;try {- if (displayName != null && displayName.isNotEmpty) {- try {- final sanitizedName = _sanitizeFileName(p.basename(displayName));- final launchPath = p.join(file.parent.path, sanitizedName);- await file.copy(launchPath);- fileToOpen = File(launchPath);- } catch (e) {- _logger.warning("Failed to create display-name copy: $e");- }+ fileToOpen = await _prepareOpenFile(+ file,+ displayName: displayName,+ lockerFile: lockerFile,+ );++ if (!await fileToOpen.exists() || await fileToOpen.length() == 0) {+ throw Exception("File is missing or empty");}final result = await OpenFile.open(fileToOpen.path);if (result.type != ResultType.done) {- throw Exception(result.message);+ if (context.mounted) {+ await _showOpenFileError(+ context,+ resultType: result.type,+ error: result.message,+ lockerFile: lockerFile,+ );+ }}} catch (e) {- await showGenericErrorBottomSheet(- context: context,- error: e,- );+ if (context.mounted) {+ await _showOpenFileError(+ context,+ error: e.toString(),+ lockerFile: lockerFile,+ );+ }+ }+ }++ static Future<File> _prepareOpenFile(+ File file, {+ required String? displayName,+ required EnteFile? lockerFile,+ }) async {+ final contentExtension = getPreferredFileExtension(+ lockerFile,+ fallbackPath: file.path,+ fallbackName: displayName,+ );++ final launchName = _openHandoffFileName(+ displayName: displayName,+ lockerFile: lockerFile,+ contentExtension: contentExtension,+ );+ final fileDirectoryName = lockerFile?.uploadedFileID?.toString() ??+ file.path.hashCode.toUnsigned(32).toRadixString(16);+ final launchDir = Directory(+ p.join(+ getOpenHandoffDirectoryPath(),+ fileDirectoryName,+ DateTime.now().microsecondsSinceEpoch.toString(),+ ),+ );++ try {+ await launchDir.create(recursive: true);+ final launchPath = p.join(launchDir.path, launchName);+ return await file.copy(launchPath);+ } catch (_) {+ throw Exception("Failed to prepare file for opening");}}++ @visibleForTesting+ static Future<File> prepareOpenFileForTest(+ File file, {+ required String? displayName,+ required EnteFile? lockerFile,+ }) {+ return _prepareOpenFile(+ file,+ displayName: displayName,+ lockerFile: lockerFile,+ );+ }++ static String _openHandoffFileName({+ required String? displayName,+ required EnteFile? lockerFile,+ required String contentExtension,+ }) {+ final rawName = displayName != null && displayName.trim().isNotEmpty+ ? displayName+ : lockerFile?.uploadedFileID != null+ ? "file-${lockerFile!.uploadedFileID}"+ : "file";+ final sanitizedName = _sanitizeFileName(p.basename(rawName));+ final sanitizedExtension = p.extension(sanitizedName);+ if (contentExtension.isEmpty ||+ sanitizedExtension.toLowerCase() == contentExtension.toLowerCase()) {+ return sanitizedName;+ }+ return "${_baseNameWithoutExtension(sanitizedName)}$contentExtension";+ }++ static Future<void> _showOpenFileError(+ BuildContext context, {+ required String error,+ ResultType? resultType,+ EnteFile? lockerFile,+ }) async {+ await showAlertBottomSheet(+ context,+ title: context.l10n.oops,+ message: _openFileErrorMessage(+ context,+ error,+ resultType: resultType,+ ),+ assetPath: "assets/warning-grey.png",+ buttons: [+ GradientButton(+ text: context.l10n.download,+ onTap: () async {+ Navigator.of(context).pop();+ await downloadFile(context, lockerFile!);+ },+ ),+ ],+ );+ }++ static String _openFileErrorMessage(+ BuildContext context,+ String error, {+ ResultType? resultType,+ }) {+ if (resultType == ResultType.noAppToOpen) {+ return context.l10n.noAppToOpenFileDownloadInstead;+ }++ final cleaned = error+ .replaceFirst(RegExp(r"^Exception:\s*"), "")+ .replaceAll("γ", ".")+ .trim();+ return context.l10n.couldNotOpenFile(+ cleaned.isEmpty ? context.l10n.noAppToOpenFileDownloadInstead : cleaned,+ );+ }}