π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Add memory share feature [dart-only]
AmanRajSinghMourya3ea8ae94d593
diff --git a/mobile/apps/photos/lib/db/memory_shares_db.dart b/mobile/apps/photos/lib/db/memory_shares_db.dartnew file mode 100644index 0000000000..4a0040404f--- /dev/null+++ b/mobile/apps/photos/lib/db/memory_shares_db.dart@@ -0,0 +1,140 @@+import 'dart:io';++import 'package:path/path.dart';+import 'package:path_provider/path_provider.dart';+import 'package:photos/models/api/memory_share/memory_share.dart';+import 'package:sqflite/sqflite.dart';++class MemorySharesDB {+ static const _databaseName = "ente.memory_shares.db";+ static const _databaseVersion = 1;++ static const _table = 'memory_shares';++ static const _columnID = 'id';+ static const _columnType = 'type';+ static const _columnMetadataCipher = 'metadata_cipher';+ static const _columnMetadataNonce = 'metadata_nonce';+ static const _columnMemEncKey = 'mem_enc_key';+ static const _columnMemKeyDecryptionNonce = 'mem_key_decryption_nonce';+ static const _columnAccessToken = 'access_token';+ static const _columnIsDeleted = 'is_deleted';+ static const _columnCreatedAt = 'created_at';+ static const _columnUpdatedAt = 'updated_at';+ static const _columnUrl = 'url';++ MemorySharesDB._();+ static final MemorySharesDB instance = MemorySharesDB._();++ static Future<Database>? _dbFuture;++ Future<Database> get database async {+ _dbFuture ??= _initDatabase();+ return _dbFuture!;+ }++ Future<Database> _initDatabase() async {+ final Directory documentsDirectory =+ await getApplicationDocumentsDirectory();+ final String path = join(documentsDirectory.path, _databaseName);+ return await openDatabase(+ path,+ version: _databaseVersion,+ onCreate: _onCreate,+ );+ }++ Future<void> _onCreate(Database db, int version) async {+ await db.execute('''+ CREATE TABLE $_table (+ $_columnID INTEGER PRIMARY KEY NOT NULL,+ $_columnType TEXT NOT NULL,+ $_columnMetadataCipher TEXT,+ $_columnMetadataNonce TEXT,+ $_columnMemEncKey TEXT NOT NULL,+ $_columnMemKeyDecryptionNonce TEXT NOT NULL,+ $_columnAccessToken TEXT NOT NULL,+ $_columnIsDeleted INTEGER NOT NULL DEFAULT 0,+ $_columnCreatedAt INTEGER NOT NULL,+ $_columnUpdatedAt INTEGER,+ $_columnUrl TEXT NOT NULL+ )+ ''');+ }++ Future<void> upsert(MemoryShare share) async {+ final db = await database;+ await db.insert(+ _table,+ _toRow(share),+ conflictAlgorithm: ConflictAlgorithm.replace,+ );+ }++ Future<List<MemoryShare>> getAll() async {+ final db = await database;+ final rows = await db.query(+ _table,+ where: '$_columnIsDeleted = 0',+ orderBy: '$_columnCreatedAt DESC',+ );+ return rows.map(_fromRow).toList();+ }++ Future<MemoryShare?> getById(int id) async {+ final db = await database;+ final rows = await db.query(+ _table,+ where: '$_columnID = ?',+ whereArgs: [id],+ );+ if (rows.isEmpty) return null;+ return _fromRow(rows.first);+ }++ Future<void> delete(int id) async {+ final db = await database;+ await db.delete(+ _table,+ where: '$_columnID = ?',+ whereArgs: [id],+ );+ }++ Future<void> clearTable() async {+ final db = await database;+ await db.delete(_table);+ }++ Map<String, dynamic> _toRow(MemoryShare share) {+ return {+ _columnID: share.id,+ _columnType: share.type.name,+ _columnMetadataCipher: share.metadataCipher,+ _columnMetadataNonce: share.metadataNonce,+ _columnMemEncKey: share.encryptedKey,+ _columnMemKeyDecryptionNonce: share.keyDecryptionNonce,+ _columnAccessToken: share.accessToken,+ _columnIsDeleted: share.isDeleted ? 1 : 0,+ _columnCreatedAt: share.createdAt,+ _columnUpdatedAt: share.updatedAt,+ _columnUrl: share.url,+ };+ }++ MemoryShare _fromRow(Map<String, dynamic> row) {+ return MemoryShare(+ id: row[_columnID] as int,+ type: MemoryShareType.fromString(row[_columnType] as String),+ metadataCipher: row[_columnMetadataCipher] as String?,+ metadataNonce: row[_columnMetadataNonce] as String?,+ encryptedKey: row[_columnMemEncKey] as String,+ keyDecryptionNonce: row[_columnMemKeyDecryptionNonce] as String,+ accessToken: row[_columnAccessToken] as String,+ isDeleted: (row[_columnIsDeleted] as int) == 1,+ createdAt: row[_columnCreatedAt] as int,+ updatedAt: row[_columnUpdatedAt] as int?,+ url: row[_columnUrl] as String,+ );+ }+}diff --git a/mobile/apps/photos/lib/models/api/memory_share/memory_share.dart b/mobile/apps/photos/lib/models/api/memory_share/memory_share.dartnew file mode 100644index 0000000000..f43c44d611--- /dev/null+++ b/mobile/apps/photos/lib/models/api/memory_share/memory_share.dart@@ -0,0 +1,93 @@+enum MemoryShareType {+ share,+ lane;++ static MemoryShareType fromString(String value) {+ switch (value) {+ case 'lane':+ return MemoryShareType.lane;+ default:+ return MemoryShareType.share;+ }+ }+}++class MemoryShare {+ final int id;+ final MemoryShareType type;+ final String? metadataCipher;+ final String? metadataNonce;+ final String encryptedKey;+ final String keyDecryptionNonce;+ final String accessToken;+ final bool isDeleted;+ final int createdAt;+ final int? updatedAt;+ final String url;++ MemoryShare({+ required this.id,+ required this.type,+ this.metadataCipher,+ this.metadataNonce,+ required this.encryptedKey,+ required this.keyDecryptionNonce,+ required this.accessToken,+ required this.isDeleted,+ required this.createdAt,+ this.updatedAt,+ required this.url,+ });++ factory MemoryShare.fromJson(Map<String, dynamic> json) {+ return MemoryShare(+ id: json['id'] as int,+ type: MemoryShareType.fromString(json['type'] as String? ?? 'share'),+ metadataCipher: json['metadataCipher'] as String?,+ metadataNonce: json['metadataNonce'] as String?,+ encryptedKey: json['encryptedKey'] as String,+ keyDecryptionNonce: json['keyDecryptionNonce'] as String,+ accessToken: json['accessToken'] as String,+ isDeleted: json['isDeleted'] as bool? ?? false,+ createdAt: json['createdAt'] as int,+ updatedAt: json['updatedAt'] as int?,+ url: json['url'] as String,+ );+ }++ Map<String, dynamic> toJson() {+ return {+ 'id': id,+ 'type': type.name,+ 'metadataCipher': metadataCipher,+ 'metadataNonce': metadataNonce,+ 'encryptedKey': encryptedKey,+ 'keyDecryptionNonce': keyDecryptionNonce,+ 'accessToken': accessToken,+ 'isDeleted': isDeleted,+ 'createdAt': createdAt,+ 'updatedAt': updatedAt,+ 'url': url,+ };+ }+}++class MemoryShareFileItem {+ final int fileID;+ final String encryptedKey;+ final String keyDecryptionNonce;++ MemoryShareFileItem({+ required this.fileID,+ required this.encryptedKey,+ required this.keyDecryptionNonce,+ });++ Map<String, dynamic> toJson() {+ return {+ 'fileID': fileID,+ 'encryptedKey': encryptedKey,+ 'keyDecryptionNonce': keyDecryptionNonce,+ };+ }+}diff --git a/mobile/apps/photos/lib/services/memory_share_service.dart b/mobile/apps/photos/lib/services/memory_share_service.dartnew file mode 100644index 0000000000..11b68c346e--- /dev/null+++ b/mobile/apps/photos/lib/services/memory_share_service.dart@@ -0,0 +1,115 @@+import 'dart:convert';+import 'dart:typed_data';++import 'package:ente_crypto/ente_crypto.dart';+import 'package:fast_base58/fast_base58.dart';+import 'package:photos/core/network/network.dart';+import 'package:photos/db/memory_shares_db.dart';+import 'package:photos/models/api/entity/type.dart';+import 'package:photos/models/api/memory_share/memory_share.dart';+import 'package:photos/models/file/file.dart';+import 'package:photos/models/memories/memory.dart';+import 'package:photos/service_locator.dart' show entityService;+import 'package:photos/utils/file_key.dart';++class MemoryShareService {+ static final MemoryShareService instance = MemoryShareService._();+ MemoryShareService._();++ final _enteDio = NetworkClient.instance.enteDio;+ final _db = MemorySharesDB.instance;++ /// Creates a memory share and returns the shareable URL with key in fragment.+ Future<String> createMemoryShare({+ required List<EnteFile> files,+ required String title,+ String? memoryId,+ }) async {+ final uploadedFiles = files.where((f) => f.uploadedFileID != null).toList();++ if (uploadedFiles.isEmpty) {+ throw Exception("No uploaded files to share");+ }++ final memoryEntityKey =+ await entityService.getOrCreateEntityKey(EntityType.memory);+ final shareKey = CryptoUtil.generateKey();+ final encryptedShareKey = CryptoUtil.encryptSync(+ shareKey,+ memoryEntityKey,+ );++ final metadataMap = <String, dynamic>{'name': title};+ if (memoryId != null) {+ metadataMap['memoryId'] = memoryId;+ }+ final metadata = jsonEncode(metadataMap);+ final metadataBytes = utf8.encode(metadata);+ final encryptedMetadata = CryptoUtil.encryptSync(+ Uint8List.fromList(metadataBytes),+ shareKey,+ );++ final fileItems = <Map<String, dynamic>>[];+ for (final file in uploadedFiles) {+ final fileKey = getFileKey(file);+ final reEncryptedKey = CryptoUtil.encryptSync(fileKey, shareKey);+ fileItems.add({+ 'fileID': file.uploadedFileID,+ 'encryptedKey': CryptoUtil.bin2base64(reEncryptedKey.encryptedData!),+ 'keyDecryptionNonce': CryptoUtil.bin2base64(reEncryptedKey.nonce!),+ });+ }++ final requestData = {+ 'metadataCipher': CryptoUtil.bin2base64(encryptedMetadata.encryptedData!),+ 'metadataNonce': CryptoUtil.bin2base64(encryptedMetadata.nonce!),+ 'encryptedKey': CryptoUtil.bin2base64(encryptedShareKey.encryptedData!),+ 'keyDecryptionNonce': CryptoUtil.bin2base64(encryptedShareKey.nonce!),+ 'files': fileItems,+ };++ final response = await _enteDio.post('/memory-share', data: requestData);+ final memoryShare = MemoryShare.fromJson(response.data['memoryShare']);++ await _db.upsert(memoryShare);++ // Key in URL fragment is never sent to server (E2E encryption)+ final keyBase58 = Base58Encode(shareKey);+ final shareUrl = "${memoryShare.url}#$keyBase58";++ return shareUrl;+ }++ Future<List<MemoryShare>> listMemoryShares() async {+ final response = await _enteDio.get('/memory-share');+ final List<dynamic> shares = response.data['memoryShares'] ?? [];+ final result = shares+ .map((s) => MemoryShare.fromJson(s as Map<String, dynamic>))+ .toList();++ for (final share in result) {+ await _db.upsert(share);+ }++ return result;+ }++ Future<List<MemoryShare>> getLocalMemoryShares() async {+ return _db.getAll();+ }++ Future<void> deleteMemoryShare(int id) async {+ await _enteDio.delete('/memory-share/$id');+ await _db.delete(id);+ }++ Future<String> shareMemories({+ required List<Memory> memories,+ required String title,+ String? memoryId,+ }) async {+ final files = Memory.filesFromMemories(memories);+ return createMemoryShare(files: files, title: title, memoryId: memoryId);+ }+}diff --git a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dartindex 7c92eba1f5..ac3c42080b 100644--- a/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart+++ b/mobile/apps/photos/lib/ui/home/memories/full_screen_memory.dart@@ -6,6 +6,7 @@ import "dart:ui";import "package:ente_pure_utils/ente_pure_utils.dart";import "package:flutter/cupertino.dart";import "package:flutter/material.dart";+import "package:flutter/services.dart";import "package:photos/core/configuration.dart";import "package:photos/core/event_bus.dart";import "package:photos/events/details_sheet_event.dart";@@ -15,6 +16,7 @@ import "package:photos/events/resume_video_event.dart";import "package:photos/models/file/file_type.dart";import "package:photos/models/memories/memory.dart";import "package:photos/service_locator.dart";+import "package:photos/services/memory_share_service.dart";import "package:photos/services/smart_memories_service.dart";import "package:photos/theme/colors.dart";import "package:photos/theme/ente_theme.dart";@@ -22,10 +24,12 @@ import "package:photos/theme/text_style.dart";import "package:photos/ui/actions/file/file_actions.dart";import "package:photos/ui/home/memories/custom_listener.dart";import "package:photos/ui/home/memories/memory_progress_indicator.dart";+import "package:photos/ui/notification/toast.dart";import "package:photos/ui/viewer/file/file_widget.dart";import "package:photos/ui/viewer/file/thumbnail_widget.dart";import "package:photos/ui/viewer/file_details/favorite_widget.dart";import "package:photos/ui/viewer/gallery/jump_to_date_gallery.dart";+import "package:photos/utils/dialog_util.dart";import "package:photos/utils/file_util.dart";import "package:photos/utils/share_util.dart";@@ -429,6 +433,27 @@ class _FullScreenMemoryState extends State<FullScreenMemory> {),),),+ const Spacer(),+ GestureDetector(+ onTap: () async {+ final fullScreenState =+ context.findAncestorStateOfType<+ _FullScreenMemoryState>();+ fullScreenState?._toggleAnimation(pause: true);+ await _shareMemoryAsLink(+ context,+ inheritedData,+ widget.title,+ );+ fullScreenState?._toggleAnimation(pause: false);+ },+ child: const Icon(+ Icons.link,+ color: Colors.white,+ size: 20,+ ),+ ),+ const SizedBox(width: 8),],),],@@ -844,3 +869,34 @@ class _MemoriesZoomWidgetState extends State<MemoriesZoomWidget>);}}++Future<void> _shareMemoryAsLink(+ BuildContext context,+ FullScreenMemoryData inheritedData,+ String title,+) async {+ final memories = inheritedData.memories;++ final dialog = createProgressDialog(context, "Creating share link...");+ await dialog.show();++ try {+ final shareUrl = await MemoryShareService.instance.shareMemories(+ memories: memories,+ title: title,+ );++ await dialog.hide();++ await Clipboard.setData(ClipboardData(text: shareUrl));++ if (context.mounted) {+ showShortToast(context, "Link copied to clipboard");+ }+ } catch (e) {+ await dialog.hide();+ if (context.mounted) {+ await showGenericErrorDialog(context: context, error: e);+ }+ }+}