From 2e941b85e9ece01face78e7a327d1915a6aaf817 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 6 Oct 2020 11:48:34 +0200 Subject: [PATCH] fix: Delete files older than 30 days --- lib/src/client.dart | 2 ++ lib/src/database/database.g.dart | 9 +++++++++ lib/src/database/database.moor | 1 + 3 files changed, 12 insertions(+) diff --git a/lib/src/client.dart b/lib/src/client.dart index a3cec5d..ff23881 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -749,6 +749,8 @@ class Client extends MatrixApi { _sortRooms(); } prevBatch = syncResp.nextBatch; + await database?.deleteOldFiles( + DateTime.now().subtract(Duration(days: 30)).millisecondsSinceEpoch); await _updateUserDeviceKeys(); if (encryptionEnabled) { encryption.onSync(); diff --git a/lib/src/database/database.g.dart b/lib/src/database/database.g.dart index 984288a..52a2ac4 100644 --- a/lib/src/database/database.g.dart +++ b/lib/src/database/database.g.dart @@ -6892,6 +6892,15 @@ abstract class _$Database extends GeneratedDatabase { ); } + Future deleteOldFiles(int saved_at) { + return customUpdate( + 'DELETE FROM files WHERE saved_at < :saved_at', + variables: [Variable.withInt(saved_at)], + updates: {files}, + updateKind: UpdateKind.delete, + ); + } + @override Iterable get allTables => allSchemaEntities.whereType(); @override diff --git a/lib/src/database/database.moor b/lib/src/database/database.moor index 7624359..6399574 100644 --- a/lib/src/database/database.moor +++ b/lib/src/database/database.moor @@ -234,3 +234,4 @@ removeSuccessfulRoomEvents: DELETE FROM events WHERE client_id = :client_id AND storeFile: INSERT OR REPLACE INTO files (mxc_uri, bytes, saved_at) VALUES (:mxc_uri, :bytes, :time); dbGetFile: SELECT * FROM files WHERE mxc_uri = :mxc_uri; markPendingEventsAsError: UPDATE events SET status = -1 WHERE client_id = :client_id AND status = 0; +deleteOldFiles: DELETE FROM files WHERE saved_at < :saved_at;