chore: Crop shortcut file on android and cache it

This commit is contained in:
krille-chan 2025-05-12 18:28:48 +02:00
commit 1305171219
No known key found for this signature in database
3 changed files with 50 additions and 14 deletions

View file

@ -0,0 +1,36 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:image/image.dart';
import 'package:matrix/matrix.dart';
extension ShortcutMemoryIcon on Uint8List {
Future<String?> toShortcutMemoryIcon(
String roomId,
DatabaseApi? database,
) async {
final cacheKey = Uri.parse('im.fluffychat://shortcuts/$roomId');
final cachedFile = await database?.getFile(cacheKey);
if (cachedFile != null) return base64Encode(cachedFile);
final image = decodeImage(this);
if (image == null) return null;
final size = image.width < image.height ? image.width : image.height;
final x = (image.width - size) ~/ 2;
final y = (image.height - size) ~/ 2;
final croppedImage = copyCrop(
image,
x: x,
y: y,
width: size,
height: size,
);
final bytes = croppedImage.toUint8List();
await database?.storeFile(cacheKey, bytes, 0);
return base64Encode(croppedImage.toUint8List());
}
}