LCOV - code coverage report
Current view: top level - lib/src/database - database_file_storage_io.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 11 26 42.3 %
Date: 2024-07-12 20:20:16 Functions: 0 0 -

          Line data    Source code
       1             : import 'dart:io';
       2             : import 'dart:typed_data';
       3             : 
       4             : import 'package:matrix/matrix.dart';
       5             : 
       6             : mixin DatabaseFileStorage {
       7          60 :   bool get supportsFileStoring => fileStorageLocation != null;
       8             : 
       9             :   late final Uri? fileStorageLocation;
      10             :   late final Duration? deleteFilesAfterDuration;
      11             : 
      12           5 :   Future<void> storeFile(Uri mxcUri, Uint8List bytes, int time) async {
      13           5 :     final fileStorageLocation = this.fileStorageLocation;
      14           5 :     if (!supportsFileStoring || fileStorageLocation == null) return;
      15             : 
      16           0 :     final dir = Directory.fromUri(fileStorageLocation);
      17             : 
      18           0 :     final file = File('${dir.path}/${mxcUri.toString().split('/').last}');
      19             : 
      20           0 :     if (await file.exists()) return;
      21           0 :     await file.writeAsBytes(bytes);
      22             :   }
      23             : 
      24           2 :   Future<Uint8List?> getFile(Uri mxcUri) async {
      25           2 :     final fileStorageLocation = this.fileStorageLocation;
      26           2 :     if (!supportsFileStoring || fileStorageLocation == null) return null;
      27             : 
      28           0 :     final dir = Directory.fromUri(fileStorageLocation);
      29             : 
      30           0 :     final file = File('${dir.path}/${mxcUri.toString().split('/').last}');
      31             : 
      32           0 :     if (await file.exists()) return await file.readAsBytes();
      33             :     return null;
      34             :   }
      35             : 
      36          30 :   Future<void> deleteOldFiles(int savedAt) async {
      37          30 :     final dirUri = fileStorageLocation;
      38          30 :     final deleteFilesAfterDuration = this.deleteFilesAfterDuration;
      39          30 :     if (!supportsFileStoring ||
      40             :         dirUri == null ||
      41             :         deleteFilesAfterDuration == null) {
      42             :       return;
      43             :     }
      44           0 :     final dir = Directory.fromUri(dirUri);
      45           0 :     final entities = await dir.list().toList();
      46           0 :     for (final file in entities) {
      47           0 :       if (file is! File) continue;
      48           0 :       final stat = await file.stat();
      49           0 :       if (DateTime.now().difference(stat.modified) > deleteFilesAfterDuration) {
      50           0 :         Logs().v('Delete old file', file.path);
      51           0 :         await file.delete();
      52             :       }
      53             :     }
      54             :   }
      55             : }

Generated by: LCOV version 1.14