Skip to content

Commit cf0501c

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 cf0501c

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

lib/src/event.dart

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,12 +420,29 @@ class Event extends MatrixEvent {
420420
MessageTypes.Audio,
421421
MessageTypes.File,
422422
}.contains(messageType)) {
423-
final file = room.sendingFilePlaceholders[eventId];
423+
final bytes = await room.client.database?.getFile(
424+
Uri.parse('com.famedly.sendingAttachment://file/$eventId'),
425+
);
426+
final file = bytes == null
427+
? null
428+
: MatrixFile(
429+
bytes: bytes,
430+
name: content.tryGet<String>('filename') ?? 'image',
431+
);
424432
if (file == null) {
425433
await cancelSend();
426434
throw Exception('Can not try to send again. File is no longer cached.');
427435
}
428-
final thumbnail = room.sendingFileThumbnails[eventId];
436+
final thumbnailBytes = await room.client.database?.getFile(
437+
Uri.parse('com.famedly.sendingAttachment://thumbnail/$txid'),
438+
);
439+
final thumbnail = thumbnailBytes == null
440+
? null
441+
: MatrixImageFile(
442+
bytes: thumbnailBytes,
443+
name:
444+
'thumbnail_${content.tryGet<String>('filename') ?? 'image'}',
445+
);
429446
final credentials = FileSendRequestCredentials.fromJson(unsigned ?? {});
430447
final inReplyTo = credentials.inReplyTo == null
431448
? null
@@ -708,7 +725,15 @@ class Event extends MatrixEvent {
708725
throw ("This event has the type '$type' and so it can't contain an attachment.");
709726
}
710727
if (status.isSending) {
711-
final localFile = room.sendingFilePlaceholders[eventId];
728+
final bytes = await room.client.database?.getFile(
729+
Uri.parse('com.famedly.sendingAttachment://file/$eventId'),
730+
);
731+
final localFile = bytes == null
732+
? null
733+
: MatrixImageFile(
734+
bytes: bytes,
735+
name: content.tryGet<String>('filename') ?? 'image',
736+
);
712737
if (localFile != null) return localFile;
713738
}
714739
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)