feat: Use matrix authenticated media
This commit is contained in:
parent
f17b09f56c
commit
158a6855c3
7 changed files with 134 additions and 115 deletions
|
|
@ -330,8 +330,11 @@ class EmotesSettingsController extends State<EmotesSettings> {
|
|||
for (final entry in pack.images.entries) {
|
||||
final emote = entry.value;
|
||||
final name = entry.key;
|
||||
final url = emote.url.getDownloadLink(client);
|
||||
final response = await get(url);
|
||||
final url = await emote.url.getDownloadUri(client);
|
||||
final response = await get(
|
||||
url,
|
||||
headers: {'authorization': 'Bearer ${client.accessToken}'},
|
||||
);
|
||||
|
||||
archive.addFile(
|
||||
ArchiveFile(
|
||||
|
|
|
|||
53
lib/utils/client_download_content_extension.dart
Normal file
53
lib/utils/client_download_content_extension.dart
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
extension ClientDownloadContentExtension on Client {
|
||||
Future<Uint8List> downloadMxcCached(
|
||||
Uri mxc, {
|
||||
num? width,
|
||||
num? height,
|
||||
bool isThumbnail = false,
|
||||
bool? animated,
|
||||
ThumbnailMethod? thumbnailMethod,
|
||||
}) async {
|
||||
// To stay compatible with previous storeKeys:
|
||||
final cacheKey = isThumbnail
|
||||
// ignore: deprecated_member_use
|
||||
? mxc.getThumbnail(
|
||||
this,
|
||||
width: width,
|
||||
height: height,
|
||||
animated: animated,
|
||||
method: thumbnailMethod!,
|
||||
)
|
||||
: mxc;
|
||||
|
||||
final cachedData = await database?.getFile(cacheKey);
|
||||
if (cachedData != null) return cachedData;
|
||||
|
||||
final httpUri = isThumbnail
|
||||
? await mxc.getThumbnailUri(
|
||||
this,
|
||||
width: width,
|
||||
height: height,
|
||||
animated: animated,
|
||||
method: thumbnailMethod,
|
||||
)
|
||||
: await mxc.getDownloadUri(this);
|
||||
|
||||
final response = await httpClient.get(
|
||||
httpUri,
|
||||
headers:
|
||||
accessToken == null ? null : {'authorization': 'Bearer $accessToken'},
|
||||
);
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception();
|
||||
}
|
||||
final remoteData = response.bodyBytes;
|
||||
|
||||
await database?.storeFile(cacheKey, remoteData, 0);
|
||||
|
||||
return remoteData;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,11 +1,9 @@
|
|||
import 'dart:io';
|
||||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter_shortcuts/flutter_shortcuts.dart';
|
||||
|
|
@ -13,6 +11,7 @@ import 'package:matrix/matrix.dart';
|
|||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/utils/client_download_content_extension.dart';
|
||||
import 'package:fluffychat/utils/client_manager.dart';
|
||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
|
|
@ -177,28 +176,25 @@ Future<void> _tryPushHelper(
|
|||
);
|
||||
|
||||
// The person object for the android message style notification
|
||||
final avatar = event.room.avatar
|
||||
?.getThumbnail(
|
||||
client,
|
||||
width: 256,
|
||||
height: 256,
|
||||
)
|
||||
.toString();
|
||||
final avatar = event.room.avatar;
|
||||
final senderAvatar = event.room.isDirectChat
|
||||
? avatar
|
||||
: event.senderFromMemoryOrFallback.avatarUrl
|
||||
?.getThumbnail(
|
||||
client,
|
||||
width: 256,
|
||||
height: 256,
|
||||
)
|
||||
.toString();
|
||||
: event.senderFromMemoryOrFallback.avatarUrl;
|
||||
|
||||
File? roomAvatarFile, senderAvatarFile;
|
||||
Uint8List? roomAvatarFile, senderAvatarFile;
|
||||
try {
|
||||
roomAvatarFile = avatar == null
|
||||
? null
|
||||
: await DefaultCacheManager().getSingleFile(avatar);
|
||||
: await client
|
||||
.downloadMxcCached(
|
||||
avatar,
|
||||
thumbnailMethod: ThumbnailMethod.scale,
|
||||
width: 256,
|
||||
height: 256,
|
||||
animated: false,
|
||||
isThumbnail: true,
|
||||
)
|
||||
.timeout(const Duration(seconds: 3));
|
||||
} catch (e, s) {
|
||||
Logs().e('Unable to get avatar picture', e, s);
|
||||
}
|
||||
|
|
@ -207,7 +203,16 @@ Future<void> _tryPushHelper(
|
|||
? roomAvatarFile
|
||||
: senderAvatar == null
|
||||
? null
|
||||
: await DefaultCacheManager().getSingleFile(senderAvatar);
|
||||
: await client
|
||||
.downloadMxcCached(
|
||||
senderAvatar,
|
||||
thumbnailMethod: ThumbnailMethod.scale,
|
||||
width: 256,
|
||||
height: 256,
|
||||
animated: false,
|
||||
isThumbnail: true,
|
||||
)
|
||||
.timeout(const Duration(seconds: 3));
|
||||
} catch (e, s) {
|
||||
Logs().e('Unable to get avatar picture', e, s);
|
||||
}
|
||||
|
|
@ -225,7 +230,7 @@ Future<void> _tryPushHelper(
|
|||
name: event.senderFromMemoryOrFallback.calcDisplayname(),
|
||||
icon: senderAvatarFile == null
|
||||
? null
|
||||
: BitmapFilePathAndroidIcon(senderAvatarFile.path),
|
||||
: ByteArrayAndroidIcon(senderAvatarFile),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
@ -272,7 +277,7 @@ Future<void> _tryPushHelper(
|
|||
name: event.senderFromMemoryOrFallback.calcDisplayname(),
|
||||
icon: roomAvatarFile == null
|
||||
? null
|
||||
: BitmapFilePathAndroidIcon(roomAvatarFile.path),
|
||||
: ByteArrayAndroidIcon(roomAvatarFile),
|
||||
key: event.roomId,
|
||||
important: event.room.isFavourite,
|
||||
),
|
||||
|
|
@ -321,7 +326,7 @@ Future<void> _setShortcut(
|
|||
Event event,
|
||||
L10n l10n,
|
||||
String title,
|
||||
File? avatarFile,
|
||||
Uint8List? avatarFile,
|
||||
) async {
|
||||
final flutterShortcuts = FlutterShortcuts();
|
||||
await flutterShortcuts.initialize(debug: !kReleaseMode);
|
||||
|
|
@ -333,8 +338,7 @@ Future<void> _setShortcut(
|
|||
conversationShortcut: true,
|
||||
icon: avatarFile == null
|
||||
? null
|
||||
: ShortcutMemoryIcon(jpegImage: await avatarFile.readAsBytes())
|
||||
.toString(),
|
||||
: ShortcutMemoryIcon(jpegImage: avatarFile).toString(),
|
||||
shortcutIconAsset: avatarFile == null
|
||||
? ShortcutIconAsset.androidAsset
|
||||
: ShortcutIconAsset.memoryAsset,
|
||||
|
|
|
|||
|
|
@ -6,12 +6,11 @@ import 'package:flutter/material.dart';
|
|||
import 'package:desktop_notifications/desktop_notifications.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:matrix/matrix.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:universal_html/html.dart' as html;
|
||||
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/utils/client_download_content_extension.dart';
|
||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_locals.dart';
|
||||
import 'package:fluffychat/utils/platform_infos.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
|
|
@ -48,50 +47,38 @@ extension LocalNotificationsExtension on MatrixState {
|
|||
hideEdit: true,
|
||||
removeMarkdown: true,
|
||||
);
|
||||
final icon = event.senderFromMemoryOrFallback.avatarUrl?.getThumbnail(
|
||||
client,
|
||||
width: 64,
|
||||
height: 64,
|
||||
method: ThumbnailMethod.crop,
|
||||
) ??
|
||||
room.avatar?.getThumbnail(
|
||||
client,
|
||||
width: 64,
|
||||
height: 64,
|
||||
method: ThumbnailMethod.crop,
|
||||
);
|
||||
|
||||
if (kIsWeb) {
|
||||
final avatarUrl = event.senderFromMemoryOrFallback.avatarUrl;
|
||||
|
||||
final iconBytes = avatarUrl == null
|
||||
? null
|
||||
: await client.downloadMxcCached(
|
||||
avatarUrl,
|
||||
width: 64,
|
||||
height: 64,
|
||||
thumbnailMethod: ThumbnailMethod.crop,
|
||||
isThumbnail: true,
|
||||
animated: false,
|
||||
);
|
||||
|
||||
_audioPlayer.play();
|
||||
|
||||
html.Notification(
|
||||
title,
|
||||
body: body,
|
||||
icon: icon.toString(),
|
||||
icon: iconBytes == null
|
||||
? null
|
||||
: html.Url.createObjectUrl(html.Blob(iconBytes)),
|
||||
tag: event.room.id,
|
||||
);
|
||||
} else if (Platform.isLinux) {
|
||||
final appIconUrl = room.avatar?.getThumbnail(
|
||||
room.client,
|
||||
width: 56,
|
||||
height: 56,
|
||||
);
|
||||
File? appIconFile;
|
||||
if (appIconUrl != null) {
|
||||
final tempDirectory = await getApplicationSupportDirectory();
|
||||
final avatarDirectory =
|
||||
await Directory('${tempDirectory.path}/notiavatars/').create();
|
||||
appIconFile = File(
|
||||
'${avatarDirectory.path}/${Uri.encodeComponent(appIconUrl.toString())}',
|
||||
);
|
||||
if (await appIconFile.exists() == false) {
|
||||
final response = await http.get(appIconUrl);
|
||||
await appIconFile.writeAsBytes(response.bodyBytes);
|
||||
}
|
||||
}
|
||||
final notification = await linuxNotifications!.notify(
|
||||
title,
|
||||
body: body,
|
||||
replacesId: linuxNotificationIds[roomId] ?? 0,
|
||||
appName: AppConfig.applicationName,
|
||||
appIcon: appIconFile?.path ?? '',
|
||||
appIcon: 'fluffychat',
|
||||
actions: [
|
||||
NotificationAction(
|
||||
DesktopNotificationActions.openChat.name,
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@ import 'dart:typed_data';
|
|||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/config/themes.dart';
|
||||
import 'package:fluffychat/utils/client_download_content_extension.dart';
|
||||
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
|
||||
|
|
@ -63,8 +63,6 @@ class _MxcImageState extends State<MxcImage> {
|
|||
: _imageDataCache[cacheKey] = data;
|
||||
}
|
||||
|
||||
bool? _isCached;
|
||||
|
||||
Future<void> _load() async {
|
||||
final client = widget.client ?? Matrix.of(context).client;
|
||||
final uri = widget.uri;
|
||||
|
|
@ -77,45 +75,18 @@ class _MxcImageState extends State<MxcImage> {
|
|||
final height = widget.height;
|
||||
final realHeight = height == null ? null : height * devicePixelRatio;
|
||||
|
||||
final httpUri = widget.isThumbnail
|
||||
? uri.getThumbnail(
|
||||
client,
|
||||
width: realWidth,
|
||||
height: realHeight,
|
||||
animated: widget.animated,
|
||||
method: widget.thumbnailMethod,
|
||||
)
|
||||
: uri.getDownloadLink(client);
|
||||
|
||||
final storeKey = widget.isThumbnail ? httpUri : uri;
|
||||
|
||||
if (_isCached == null) {
|
||||
final cachedData = await client.database?.getFile(storeKey);
|
||||
if (cachedData != null) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_imageData = cachedData;
|
||||
_isCached = true;
|
||||
});
|
||||
return;
|
||||
}
|
||||
_isCached = false;
|
||||
}
|
||||
|
||||
final response = await http.get(httpUri);
|
||||
if (response.statusCode != 200) {
|
||||
if (response.statusCode == 404) {
|
||||
return;
|
||||
}
|
||||
throw Exception();
|
||||
}
|
||||
final remoteData = response.bodyBytes;
|
||||
|
||||
final remoteData = await client.downloadMxcCached(
|
||||
uri,
|
||||
width: realWidth,
|
||||
height: realHeight,
|
||||
thumbnailMethod: widget.thumbnailMethod,
|
||||
isThumbnail: widget.isThumbnail,
|
||||
animated: widget.animated,
|
||||
);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_imageData = remoteData;
|
||||
});
|
||||
await client.database?.storeFile(storeKey, remoteData, 0);
|
||||
}
|
||||
|
||||
if (event != null) {
|
||||
|
|
@ -179,7 +150,6 @@ class _MxcImageState extends State<MxcImage> {
|
|||
filterQuality:
|
||||
widget.isThumbnail ? FilterQuality.low : FilterQuality.medium,
|
||||
errorBuilder: (context, __, ___) {
|
||||
_isCached = false;
|
||||
_imageData = null;
|
||||
WidgetsBinding.instance.addPostFrameCallback(_tryLoad);
|
||||
return placeholder(context);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue