Skip to content

Commit

Permalink
Merge pull request #1946 from famedly/krille/delete-file
Browse files Browse the repository at this point in the history
feat: Add deleteFile() endpoint to database
  • Loading branch information
nico-famedly authored Nov 12, 2024
2 parents 03416cd + 60d5009 commit fd2f909
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/src/database/database_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ abstract class DatabaseApi {

Future storeFile(Uri mxcUri, Uint8List bytes, int time);

Future<bool> deleteFile(Uri mxcUri);

Future storeSyncFilterId(
String syncFilterId,
);
Expand Down
24 changes: 18 additions & 6 deletions lib/src/database/database_file_storage_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ mixin DatabaseFileStorage {
late final Uri? fileStorageLocation;
late final Duration? deleteFilesAfterDuration;

File _getFileFromMxc(Uri mxcUri) => File(
'${Directory.fromUri(fileStorageLocation!).path}/${mxcUri.toString().split('/').last}',
);

Future<void> storeFile(Uri mxcUri, Uint8List bytes, int time) async {
final fileStorageLocation = this.fileStorageLocation;
if (!supportsFileStoring || fileStorageLocation == null) return;

final dir = Directory.fromUri(fileStorageLocation);

final file = File('${dir.path}/${mxcUri.toString().split('/').last}');
final file = _getFileFromMxc(mxcUri);

if (await file.exists()) return;
await file.writeAsBytes(bytes);
Expand All @@ -25,14 +27,24 @@ mixin DatabaseFileStorage {
final fileStorageLocation = this.fileStorageLocation;
if (!supportsFileStoring || fileStorageLocation == null) return null;

final dir = Directory.fromUri(fileStorageLocation);

final file = File('${dir.path}/${mxcUri.toString().split('/').last}');
final file = _getFileFromMxc(mxcUri);

if (await file.exists()) return await file.readAsBytes();
return null;
}

Future<bool> deleteFile(Uri mxcUri) async {
final fileStorageLocation = this.fileStorageLocation;
if (!supportsFileStoring || fileStorageLocation == null) return false;

final file = _getFileFromMxc(mxcUri);

if (await file.exists() == false) return false;

await file.delete();
return true;
}

Future<void> deleteOldFiles(int savedAt) async {
final dirUri = fileStorageLocation;
final deleteFilesAfterDuration = this.deleteFilesAfterDuration;
Expand Down
2 changes: 2 additions & 0 deletions lib/src/database/database_file_storage_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ mixin DatabaseFileStorage {
Future<void> deleteOldFiles(int savedAt) async {
return;
}

Future<bool> deleteFile(Uri mxcUri) async => false;
}
5 changes: 5 additions & 0 deletions lib/src/database/hive_collections_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,11 @@ class HiveCollectionsDatabase extends DatabaseApi {
return null;
}

@override
Future<bool> deleteFile(Uri mxcUri) async {
return false;
}

@override
Future<StoredInboundGroupSession?> getInboundGroupSession(
String roomId,
Expand Down
5 changes: 5 additions & 0 deletions lib/src/database/hive_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ class FamedlySdkHiveDatabase extends DatabaseApi with ZoneTransactionMixin {
return null;
}

@override
Future<bool> deleteFile(Uri mxcUri) async {
return false;
}

@override
Future<StoredInboundGroupSession?> getInboundGroupSession(
String roomId,
Expand Down
9 changes: 8 additions & 1 deletion test/database_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,21 @@ void main() {
final toDeviceQueue = await database.getToDeviceEventQueue();
expect(toDeviceQueue.isEmpty, true);
});
test('storeFile', () async {
test('storeFile and deleteFile', () async {
await database.storeFile(
Uri.parse('mxc://test'),
Uint8List.fromList([0]),
0,
);
final file = await database.getFile(Uri.parse('mxc://test'));
expect(file != null, database.supportsFileStoring);

final result = await database.deleteFile(Uri.parse('mxc://test'));
expect(result, database.supportsFileStoring);
if (result) {
final file = await database.getFile(Uri.parse('mxc://test'));
expect(file, null);
}
});
test('getFile', () async {
await database.getFile(Uri.parse('mxc://test'));
Expand Down

0 comments on commit fd2f909

Please sign in to comment.