| 
						
						
						
					 | 
					 | 
					@ -1,3 +1,5 @@
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					//@dart=2.12
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import 'dart:io';
 | 
					 | 
					 | 
					 | 
					import 'dart:io';
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import 'dart:typed_data';
 | 
					 | 
					 | 
					 | 
					import 'dart:typed_data';
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -8,15 +10,12 @@ import 'package:encrypt/encrypt.dart';
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import 'package:flutter_secure_storage/flutter_secure_storage.dart';
 | 
					 | 
					 | 
					 | 
					import 'package:flutter_secure_storage/flutter_secure_storage.dart';
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import 'package:matrix/matrix.dart';
 | 
					 | 
					 | 
					 | 
					import 'package:matrix/matrix.dart';
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import 'package:path_provider/path_provider.dart';
 | 
					 | 
					 | 
					 | 
					import 'package:path_provider/path_provider.dart';
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import 'package:sqflite/sqflite.dart' as sqflite;
 | 
					 | 
					 | 
					 | 
					import 'package:sqflite_sqlcipher/sqflite.dart' as sqflite;
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					import '../platform_infos.dart';
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
 | 
					 | 
					 | 
					 | 
					class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  FlutterFluffyBoxDatabase(
 | 
					 | 
					 | 
					 | 
					  FlutterFluffyBoxDatabase(
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    String name, {
 | 
					 | 
					 | 
					 | 
					    String name, {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    String path,
 | 
					 | 
					 | 
					 | 
					    Future<sqflite.Database> Function()? openSqlDatabase,
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Future<sqflite.Database> Function() openSqlDatabase,
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  }) : super(
 | 
					 | 
					 | 
					 | 
					  }) : super(
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          name,
 | 
					 | 
					 | 
					 | 
					          name,
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					          openSqlDatabase: openSqlDatabase,
 | 
					 | 
					 | 
					 | 
					          openSqlDatabase: openSqlDatabase,
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -27,6 +26,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
 | 
					 | 
					 | 
					 | 
					  static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Logs().d('Open FluffyBox...');
 | 
					 | 
					 | 
					 | 
					    Logs().d('Open FluffyBox...');
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    String? password;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    try {
 | 
					 | 
					 | 
					 | 
					    try {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      // Workaround for secure storage is calling Platform.operatingSystem on web
 | 
					 | 
					 | 
					 | 
					      // Workaround for secure storage is calling Platform.operatingSystem on web
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      if (kIsWeb) throw MissingPluginException();
 | 
					 | 
					 | 
					 | 
					      if (kIsWeb) throw MissingPluginException();
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -43,24 +43,27 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      }
 | 
					 | 
					 | 
					 | 
					      }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      // workaround for if we just wrote to the key and it still doesn't exist
 | 
					 | 
					 | 
					 | 
					      // workaround for if we just wrote to the key and it still doesn't exist
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey);
 | 
					 | 
					 | 
					 | 
					      password = await secureStorage.read(key: _cipherStorageKey);
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      if (rawEncryptionKey == null) throw MissingPluginException();
 | 
					 | 
					 | 
					 | 
					      if (password == null) throw MissingPluginException();
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    } on MissingPluginException catch (_) {
 | 
					 | 
					 | 
					 | 
					    } on MissingPluginException catch (_) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      Logs().i('FluffyBox encryption is not supported on this platform');
 | 
					 | 
					 | 
					 | 
					      Logs().i('FluffyBox encryption is not supported on this platform');
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    final db = FluffyBoxDatabase(
 | 
					 | 
					 | 
					 | 
					    final db = FluffyBoxDatabase(
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
 | 
					 | 
					 | 
					 | 
					      'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      openSqlDatabase: () => _openSqlDatabase(client),
 | 
					 | 
					 | 
					 | 
					      openSqlDatabase: () => _openSqlDatabase(client, password),
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    );
 | 
					 | 
					 | 
					 | 
					    );
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    await db.open();
 | 
					 | 
					 | 
					 | 
					    await db.open();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    Logs().d('FluffyBox is ready');
 | 
					 | 
					 | 
					 | 
					    Logs().d('FluffyBox is ready');
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return db;
 | 
					 | 
					 | 
					 | 
					    return db;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  }
 | 
					 | 
					 | 
					 | 
					  }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  static Future<sqflite.Database> _openSqlDatabase(Client client) async {
 | 
					 | 
					 | 
					 | 
					  static Future<sqflite.Database> _openSqlDatabase(
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    Client client,
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					    String? password,
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					 | 
					  ) async {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    final path = await _findDatabasePath(client);
 | 
					 | 
					 | 
					 | 
					    final path = await _findDatabasePath(client);
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    return await sqflite.openDatabase(path);
 | 
					 | 
					 | 
					 | 
					    return await sqflite.openDatabase(path, password: password);
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  }
 | 
					 | 
					 | 
					 | 
					  }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  static Future<String> _findDatabasePath(Client client) async {
 | 
					 | 
					 | 
					 | 
					  static Future<String> _findDatabasePath(Client client) async {
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -85,9 +88,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  @override
 | 
					 | 
					 | 
					 | 
					  @override
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
 | 
					 | 
					 | 
					 | 
					  int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  @override
 | 
					 | 
					 | 
					 | 
					  @override
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  bool get supportsFileStoring => (PlatformInfos.isIOS ||
 | 
					 | 
					 | 
					 | 
					  bool get supportsFileStoring => !kIsWeb;
 | 
				
			
			
				
				
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      PlatformInfos.isAndroid ||
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      PlatformInfos.isDesktop);
 | 
					 | 
					 | 
					 | 
					 | 
				
			
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  Future<String> _getFileStoreDirectory() async {
 | 
					 | 
					 | 
					 | 
					  Future<String> _getFileStoreDirectory() async {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    try {
 | 
					 | 
					 | 
					 | 
					    try {
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
					 | 
					@ -97,12 +98,12 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					        return (await getApplicationDocumentsDirectory()).path;
 | 
					 | 
					 | 
					 | 
					        return (await getApplicationDocumentsDirectory()).path;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      }
 | 
					 | 
					 | 
					 | 
					      }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    } catch (_) {
 | 
					 | 
					 | 
					 | 
					    } catch (_) {
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					      return (await getDownloadsDirectory()).path;
 | 
					 | 
					 | 
					 | 
					      return (await getDownloadsDirectory())!.path;
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    }
 | 
					 | 
					 | 
					 | 
					    }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  }
 | 
					 | 
					 | 
					 | 
					  }
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					
 | 
					 | 
					 | 
					 | 
					
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  @override
 | 
					 | 
					 | 
					 | 
					  @override
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					  Future<Uint8List> getFile(Uri mxcUri) async {
 | 
					 | 
					 | 
					 | 
					  Future<Uint8List?> getFile(Uri mxcUri) async {
 | 
				
			
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    if (!supportsFileStoring) return null;
 | 
					 | 
					 | 
					 | 
					    if (!supportsFileStoring) return null;
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    final tempDirectory = await _getFileStoreDirectory();
 | 
					 | 
					 | 
					 | 
					    final tempDirectory = await _getFileStoreDirectory();
 | 
				
			
			
		
	
		
		
			
				
					
					 | 
					 | 
					 | 
					    final file =
 | 
					 | 
					 | 
					 | 
					    final file =
 | 
				
			
			
		
	
	
		
		
			
				
					| 
						
							
								
							
						
						
						
					 | 
					 | 
					
 
 |