Skip to content

armgit / ente

Public Imported from GitHub
0

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

Enable single-file download for locally available files

ashilkn19c43be32472
diff --git a/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dart b/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dart
index 0138f23b69..32c07dc89b 100644
--- a/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dart
+++ b/mobile/apps/photos/lib/ui/viewer/file/file_app_bar.dart
@@ -10,6 +10,7 @@ import 'package:logging/logging.dart';
import 'package:media_extension/media_extension.dart';
import "package:photos/core/configuration.dart";
import "package:photos/core/event_bus.dart";
+import "package:photos/db/files_db.dart";
import "package:photos/events/guest_view_event.dart";
import "package:photos/generated/l10n.dart";
import "package:photos/l10n/l10n.dart";
@@ -31,6 +32,7 @@ import "package:photos/theme/ente_theme.dart";
import "package:photos/ui/actions/file/file_actions.dart";
import 'package:photos/ui/collections/collection_action_sheet.dart';
import "package:photos/ui/common/popup_item.dart";
+import "package:photos/ui/components/buttons/button_widget.dart";
import 'package:photos/ui/notification/toast.dart';
import 'package:photos/ui/viewer/actions/suggest_delete_sheet.dart';
import "package:photos/ui/viewer/file/detail_page.dart";
@@ -61,6 +63,12 @@ class FileAppBar extends StatefulWidget {
FileAppBarState createState() => FileAppBarState();
}
+class _LocalFileLocation {
+ final String locationName;
+
+ const _LocalFileLocation(this.locationName);
+}
+
class FileAppBarState extends State<FileAppBar> {
final _logger = Logger("FadingAppBar");
final List<Widget> _actions = [];
@@ -312,7 +320,7 @@ class FileAppBarState extends State<FileAppBar> {
),
);
} else {
- if (widget.file.isRemoteFile) {
+ if (isFileUploaded) {
items.add(
EntePopupMenuItem(
AppLocalizations.of(context).download,
@@ -618,6 +626,26 @@ class FileAppBarState extends State<FileAppBar> {
}
Future<void> _download(EnteFile file) async {
+ final _LocalFileLocation? localFileLocation =
+ await _getExistingLocalFileLocation(file);
+ if (localFileLocation != null && mounted) {
+ final actionResult = await showChoiceActionSheet(
+ context,
+ title: "Already available on device",
+ body: Platform.isAndroid
+ ? "You can find this file in the \"${localFileLocation.locationName}\" folder. Download it again?"
+ : "You can find this file in the \"${localFileLocation.locationName}\" album. Download it again?",
+ firstButtonLabel: "Download anyway",
+ secondButtonLabel: "Cancel",
+ );
+ if (actionResult?.action != ButtonAction.first) {
+ return;
+ }
+ }
+ if (!mounted) {
+ return;
+ }
+
final dialog = createProgressDialog(
context,
context.l10n.downloading,
@@ -635,6 +663,61 @@ class FileAppBarState extends State<FileAppBar> {
}
}
+ Future<_LocalFileLocation?> _getExistingLocalFileLocation(
+ EnteFile file,
+ ) async {
+ if (file.localID == null || file.localID!.isEmpty) {
+ return null;
+ }
+
+ final asset = await file.getAsset;
+ if (asset == null || !(await asset.exists)) {
+ _logger.info(
+ "Removing stale localID reference before download for ${file.tag}",
+ );
+ file.localID = null;
+ if (file.generatedID != null) {
+ unawaited(FilesDB.instance.update(file));
+ }
+ return null;
+ }
+
+ final String relativePath =
+ (asset.relativePath ?? "").trim().replaceFirst(RegExp(r'[/\\]+$'), '');
+ final String deviceFolder = (file.deviceFolder ?? "").trim();
+
+ if (Platform.isAndroid) {
+ final String folderName;
+ if (relativePath.isNotEmpty) {
+ folderName = _getLastPathSegment(relativePath);
+ } else if (deviceFolder.isNotEmpty) {
+ folderName = _getLastPathSegment(deviceFolder);
+ } else {
+ folderName = "Recent";
+ }
+ return _LocalFileLocation(folderName);
+ }
+
+ final String albumSource = deviceFolder.isNotEmpty
+ ? _getLastPathSegment(deviceFolder)
+ : relativePath.isNotEmpty
+ ? _getLastPathSegment(relativePath)
+ : "Recents";
+ return _LocalFileLocation(albumSource);
+ }
+
+ String _getLastPathSegment(String path) {
+ final normalized = path.trim().replaceFirst(RegExp(r'[/\\]+$'), '');
+ if (normalized.isEmpty) {
+ return normalized;
+ }
+ final segments = normalized
+ .split(RegExp(r'[/\\]+'))
+ .where((segment) => segment.trim().isNotEmpty)
+ .toList();
+ return segments.isEmpty ? normalized : segments.last;
+ }
+
Future<void> _setAs(EnteFile file) async {
final dialog =
createProgressDialog(context, AppLocalizations.of(context).pleaseWait);