Skip to content

armgit / ente

Public Imported from GitHub
0

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

Add support for playing album on TV

Neeraj Gupta307419703efe
diff --git a/lib/gateways/cast_gw.dart b/lib/gateways/cast_gw.dart
new file mode 100644
index 0000000000..fb342c1a90
--- /dev/null
+++ b/lib/gateways/cast_gw.dart
@@ -0,0 +1,50 @@
+import "package:dio/dio.dart";
+
+class CastGateway {
+ final Dio _enteDio;
+
+ CastGateway(this._enteDio);
+
+ Future<String?> getPublicKey(String deviceCode) async {
+ try {
+ final response = await _enteDio.get(
+ "/cast/device-info/$deviceCode",
+ );
+ return response.data["publicKey"];
+ } catch (e) {
+ if (e is DioError &&
+ e.response != null &&
+ e.response!.statusCode == 404) {
+ return null;
+ }
+ rethrow;
+ }
+ }
+
+ Future<void> publishCastPayload(
+ String code,
+ String castPayload,
+ int collectionID,
+ String castToken,
+ ) {
+ return _enteDio.post(
+ "/cast/cast-data/",
+ data: {
+ "deviceCode": code,
+ "encPayload": castPayload,
+ "collectionID": collectionID,
+ "castToken": castToken,
+ },
+ );
+ }
+
+ Future<void> revokeAllTokens() async {
+ try {
+ await _enteDio.delete(
+ "/cast/revoke-all-tokens/",
+ );
+ } catch (e) {
+ // swallow error
+ }
+ }
+}
diff --git a/lib/generated/intl/messages_en.dart b/lib/generated/intl/messages_en.dart
index 254eacaa9b..df661e73ea 100644
--- a/lib/generated/intl/messages_en.dart
+++ b/lib/generated/intl/messages_en.dart
@@ -980,6 +980,7 @@ class MessageLookup extends MessageLookupByLibrary {
"pickCenterPoint":
MessageLookupByLibrary.simpleMessage("Pick center point"),
"pinAlbum": MessageLookupByLibrary.simpleMessage("Pin album"),
+ "playOnTv": MessageLookupByLibrary.simpleMessage("Play album on TV"),
"playStoreFreeTrialValidTill": m37,
"playstoreSubscription":
MessageLookupByLibrary.simpleMessage("PlayStore subscription"),
diff --git a/lib/generated/l10n.dart b/lib/generated/l10n.dart
index e882159ed6..cc2ec73688 100644
--- a/lib/generated/l10n.dart
+++ b/lib/generated/l10n.dart
@@ -5765,6 +5765,16 @@ class S {
);
}
+ /// `Play album on TV`
+ String get playOnTv {
+ return Intl.message(
+ 'Play album on TV',
+ name: 'playOnTv',
+ desc: '',
+ args: [],
+ );
+ }
+
/// `Calculating...`
String get calculating {
return Intl.message(
diff --git a/lib/l10n/intl_en.arb b/lib/l10n/intl_en.arb
index ae31c07d65..c1e4fa2729 100644
--- a/lib/l10n/intl_en.arb
+++ b/lib/l10n/intl_en.arb
@@ -817,6 +817,7 @@
"nothingToSeeHere": "Nothing to see here! πŸ‘€",
"unarchiveAlbum": "Unarchive album",
"archiveAlbum": "Archive album",
+ "playOnTv": "Play album on TV",
"calculating": "Calculating...",
"pleaseWaitDeletingAlbum": "Please wait, deleting album",
"searchHintText": "Albums, months, days, years, ...",
diff --git a/lib/services/collections_service.dart b/lib/services/collections_service.dart
index fc51ced3c7..3ea85746c8 100644
--- a/lib/services/collections_service.dart
+++ b/lib/services/collections_service.dart
@@ -473,6 +473,23 @@ class CollectionsService {
});
}
+ String getCastData(
+ String castToken,
+ Collection collection,
+ String publicKey,
+ ) {
+ final String payload = jsonEncode({
+ "collectionID": collection.id,
+ "castToken": castToken,
+ "collectionKey": CryptoUtil.bin2base64(getCollectionKey(collection.id)),
+ });
+ final encPayload = CryptoUtil.sealSync(
+ CryptoUtil.base642bin(base64Encode(payload.codeUnits)),
+ CryptoUtil.base642bin(publicKey),
+ );
+ return CryptoUtil.bin2base64(encPayload);
+ }
+
Future<List<User>> share(
int collectionID,
String email,
diff --git a/lib/ui/viewer/gallery/gallery_app_bar_widget.dart b/lib/ui/viewer/gallery/gallery_app_bar_widget.dart
index de471597a0..107d11761e 100644
--- a/lib/ui/viewer/gallery/gallery_app_bar_widget.dart
+++ b/lib/ui/viewer/gallery/gallery_app_bar_widget.dart
@@ -7,9 +7,12 @@ import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:photos/core/configuration.dart';
import 'package:photos/core/event_bus.dart';
+import "package:photos/core/network/network.dart";
import "package:photos/db/files_db.dart";
import 'package:photos/events/subscription_purchased_event.dart';
+import "package:photos/gateways/cast_gw.dart";
import "package:photos/generated/l10n.dart";
+import "package:photos/l10n/l10n.dart";
import 'package:photos/models/backup_status.dart';
import 'package:photos/models/collection/collection.dart';
import 'package:photos/models/device_collection.dart';
@@ -36,6 +39,7 @@ import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/magic_util.dart';
import 'package:photos/utils/navigation_util.dart';
import 'package:photos/utils/toast_util.dart';
+import "package:uuid/uuid.dart";
class GalleryAppBarWidget extends StatefulWidget {
final GalleryType type;
@@ -64,6 +68,7 @@ enum AlbumPopupAction {
ownedArchive,
sharedArchive,
ownedHide,
+ playOnTv,
sort,
leave,
freeUpSpace,
@@ -472,6 +477,22 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
),
);
}
+ if (widget.collection != null) {
+ items.add(
+ PopupMenuItem(
+ value: AlbumPopupAction.playOnTv,
+ child: Row(
+ children: [
+ const Icon(Icons.tv_outlined),
+ const Padding(
+ padding: EdgeInsets.all(8),
+ ),
+ Text(context.l10n.playOnTv),
+ ],
+ ),
+ ),
+ );
+ }
if (galleryType.canDelete()) {
items.add(
@@ -579,6 +600,8 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
await _removeQuickLink();
} else if (value == AlbumPopupAction.leave) {
await _leaveAlbum(context);
+ } else if (value == AlbumPopupAction.playOnTv) {
+ await castAlbum();
} else if (value == AlbumPopupAction.freeUpSpace) {
await _deleteBackedUpFiles(context);
} else if (value == AlbumPopupAction.setCover) {
@@ -797,4 +820,41 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
);
setState(() {});
}
+
+ Future<void> castAlbum() async {
+ final gw = CastGateway(NetworkClient.instance.enteDio);
+ // stop any existing cast session
+ gw.revokeAllTokens().ignore();
+ await showTextInputDialog(
+ context,
+ title: "Play album on TV",
+ body: "Visit cast.ente.io on the device you want to pair.\n\n"
+ "Enter the code below to play the album on your TV.\n",
+ submitButtonLabel: "Pair",
+ textInputType: TextInputType.streetAddress,
+ hintText: "Enter the code",
+ onSubmit: (String text) async {
+ try {
+ String code = text.trim();
+ final String? publicKey = await gw.getPublicKey(code);
+ if (publicKey == null) {
+ showToast(context, "Device not found");
+ return;
+ }
+ final String castToken = Uuid().v4().toString();
+ final castPayload = CollectionsService.instance
+ .getCastData(castToken, widget.collection!, publicKey);
+ await gw.publishCastPayload(
+ code,
+ castPayload,
+ widget.collection!.id,
+ castToken,
+ );
+ } catch (e, s) {
+ _logger.severe("Failed to cast album", e, s);
+ await showGenericErrorDialog(context: context, error: e);
+ }
+ },
+ );
+ }
}