Skip to content

Commit 8412d72

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 992c8e4 commit 8412d72

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
@@ -689,9 +689,6 @@ class Room {
689689
return sendEvent(event, txid: txid);
690690
}
691691

692-
final Map<String, MatrixFile> sendingFilePlaceholders = {};
693-
final Map<String, MatrixImageFile> sendingFileThumbnails = {};
694-
695692
/// Sends a [file] to this room after uploading it. Returns the mxc uri of
696693
/// the uploaded file. If [waitUntilSent] is true, the future will wait until
697694
/// the message event has received the server. Otherwise the future will only
@@ -715,10 +712,6 @@ class Room {
715712
String? threadLastEventId,
716713
}) async {
717714
txid ??= client.generateUniqueTransactionId();
718-
sendingFilePlaceholders[txid] = file;
719-
if (thumbnail != null) {
720-
sendingFileThumbnails[txid] = thumbnail;
721-
}
722715

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

759768
MatrixFile uploadFile = file; // ignore: omit_local_variable_types
760769
// computing the thumbnail in case we can
@@ -840,12 +849,22 @@ class Room {
840849
syncUpdate.rooms!.join!.values.first.timeline!.events!.first
841850
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
842851
await _handleFakeSync(syncUpdate);
852+
853+
if (client.database?.supportsFileStoring != true) {
854+
final sendEvent = await getEventById(txid);
855+
await sendEvent?.cancelSend();
856+
}
843857
rethrow;
844858
} catch (_) {
845859
if (DateTime.now().isAfter(timeoutDate)) {
846860
syncUpdate.rooms!.join!.values.first.timeline!.events!.first
847861
.unsigned![messageSendingStatusKey] = EventStatus.error.intValue;
848862
await _handleFakeSync(syncUpdate);
863+
864+
if (client.database?.supportsFileStoring != true) {
865+
final sendEvent = await getEventById(txid);
866+
await sendEvent?.cancelSend();
867+
}
849868
rethrow;
850869
}
851870
Logs().v('Send File into room failed. Try again...');
@@ -909,8 +928,13 @@ class Room {
909928
threadRootEventId: threadRootEventId,
910929
threadLastEventId: threadLastEventId,
911930
);
912-
sendingFilePlaceholders.remove(txid);
913-
sendingFileThumbnails.remove(txid);
931+
await client.database?.deleteFile(
932+
Uri.parse('com.famedly.sendingAttachment://file/$txid'),
933+
);
934+
await client.database?.deleteFile(
935+
Uri.parse('com.famedly.sendingAttachment://thumbnail/$txid'),
936+
);
937+
914938
return eventId;
915939
}
916940

0 commit comments

Comments
 (0)