Skip to content

Commit c08161e

Browse files
committed
feat: Store sending attachment in temporary file storage
This could make retrying to send files more easy as the file is stored in the file storage. This just replaces the current approach to store the file in the memory instead so they are gone on the next session. For this we need a deleteFile() method for the database.
1 parent f3e249d commit c08161e

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

lib/src/event.dart

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ class Event extends MatrixEvent {
378378

379379
await room.client.database?.removeEvent(eventId, room.id);
380380

381+
// Delete possible send file requests:
382+
await room.client.database?.deleteFile(
383+
Uri.parse('com.famedly.sendingAttachment://file/$eventId'),
384+
);
385+
await room.client.database?.deleteFile(
386+
Uri.parse('com.famedly.sendingAttachment://thumbnail/$eventId'),
387+
);
388+
381389
if (room.lastEvent != null && room.lastEvent!.eventId == eventId) {
382390
final redactedBecause = Event.fromMatrixEvent(
383391
MatrixEvent(
@@ -420,12 +428,29 @@ class Event extends MatrixEvent {
420428
MessageTypes.Audio,
421429
MessageTypes.File,
422430
}.contains(messageType)) {
423-
final file = room.sendingFilePlaceholders[eventId];
431+
final bytes = await room.client.database?.getFile(
432+
Uri.parse('com.famedly.sendingAttachment://file/$eventId'),
433+
);
434+
final file = bytes == null
435+
? null
436+
: MatrixFile(
437+
bytes: bytes,
438+
name: content.tryGet<String>('filename') ?? 'image',
439+
);
424440
if (file == null) {
425441
await cancelSend();
426442
throw Exception('Can not try to send again. File is no longer cached.');
427443
}
428-
final thumbnail = room.sendingFileThumbnails[eventId];
444+
final thumbnailBytes = await room.client.database?.getFile(
445+
Uri.parse('com.famedly.sendingAttachment://thumbnail/$txid'),
446+
);
447+
final thumbnail = thumbnailBytes == null
448+
? null
449+
: MatrixImageFile(
450+
bytes: thumbnailBytes,
451+
name:
452+
'thumbnail_${content.tryGet<String>('filename') ?? 'image'}',
453+
);
429454
final credentials = FileSendRequestCredentials.fromJson(unsigned ?? {});
430455
final inReplyTo = credentials.inReplyTo == null
431456
? null
@@ -708,7 +733,15 @@ class Event extends MatrixEvent {
708733
throw ("This event has the type '$type' and so it can't contain an attachment.");
709734
}
710735
if (status.isSending) {
711-
final localFile = room.sendingFilePlaceholders[eventId];
736+
final bytes = await room.client.database?.getFile(
737+
Uri.parse('com.famedly.sendingAttachment://file/$eventId'),
738+
);
739+
final localFile = bytes == null
740+
? null
741+
: MatrixImageFile(
742+
bytes: bytes,
743+
name: content.tryGet<String>('filename') ?? 'image',
744+
);
712745
if (localFile != null) return localFile;
713746
}
714747
final database = room.client.database;

lib/src/room.dart

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,6 @@ class Room {
687687
return sendEvent(event, txid: txid);
688688
}
689689

690-
final Map<String, MatrixFile> sendingFilePlaceholders = {};
691-
final Map<String, MatrixImageFile> sendingFileThumbnails = {};
692-
693690
/// Sends a [file] to this room after uploading it. Returns the mxc uri of
694691
/// the uploaded file. If [waitUntilSent] is true, the future will wait until
695692
/// the message event has received the server. Otherwise the future will only
@@ -713,10 +710,6 @@ class Room {
713710
String? threadLastEventId,
714711
}) async {
715712
txid ??= client.generateUniqueTransactionId();
716-
sendingFilePlaceholders[txid] = file;
717-
if (thumbnail != null) {
718-
sendingFileThumbnails[txid] = thumbnail;
719-
}
720713

721714
// Create a fake Event object as a placeholder for the uploading file:
722715
final syncUpdate = SyncUpdate(
@@ -753,6 +746,22 @@ class Room {
753746
},
754747
),
755748
);
749+
await _handleFakeSync(syncUpdate);
750+
751+
if (client.database?.supportsFileStoring == true) {
752+
await client.database?.storeFile(
753+
Uri.parse('com.famedly.sendingAttachment://file/$txid'),
754+
file.bytes,
755+
DateTime.now().millisecondsSinceEpoch,
756+
);
757+
if (thumbnail != null) {
758+
await client.database?.storeFile(
759+
Uri.parse('com.famedly.sendingAttachment://thumbnail/$txid'),
760+
file.bytes,
761+
DateTime.now().millisecondsSinceEpoch,
762+
);
763+
}
764+
}
756765

757766
MatrixFile uploadFile = file; // ignore: omit_local_variable_types
758767
// computing the thumbnail in case we can
@@ -838,12 +847,22 @@ class Room {
838847
syncUpdate.rooms!.join!.values.first.timeline!.events!.first
839848
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
840849
await _handleFakeSync(syncUpdate);
850+
851+
if (client.database?.supportsFileStoring != true) {
852+
final sendEvent = await getEventById(txid);
853+
await sendEvent?.cancelSend();
854+
}
841855
rethrow;
842856
} catch (_) {
843857
if (DateTime.now().isAfter(timeoutDate)) {
844858
syncUpdate.rooms!.join!.values.first.timeline!.events!.first
845859
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
846860
await _handleFakeSync(syncUpdate);
861+
862+
if (client.database?.supportsFileStoring != true) {
863+
final sendEvent = await getEventById(txid);
864+
await sendEvent?.cancelSend();
865+
}
847866
rethrow;
848867
}
849868
Logs().v('Send File into room failed. Try again...');
@@ -907,8 +926,13 @@ class Room {
907926
threadRootEventId: threadRootEventId,
908927
threadLastEventId: threadLastEventId,
909928
);
910-
sendingFilePlaceholders.remove(txid);
911-
sendingFileThumbnails.remove(txid);
929+
await client.database?.deleteFile(
930+
Uri.parse('com.famedly.sendingAttachment://file/$txid'),
931+
);
932+
await client.database?.deleteFile(
933+
Uri.parse('com.famedly.sendingAttachment://thumbnail/$txid'),
934+
);
935+
912936
return eventId;
913937
}
914938

0 commit comments

Comments
 (0)