fluffychat/lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart

149 lines
4.6 KiB
Dart
Raw Normal View History

2021-11-18 15:26:53 +01:00
import 'dart:convert';
2021-11-17 12:20:15 +01:00
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/foundation.dart' hide Key;
import 'package:flutter/services.dart';
2021-12-27 09:05:16 +01:00
import 'package:fluffybox/hive.dart' as fluffybox;
2021-11-17 12:20:15 +01:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2021-11-18 15:26:53 +01:00
import 'package:hive_flutter/hive_flutter.dart';
2021-11-17 12:20:15 +01:00
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
import '../client_manager.dart';
import '../famedlysdk_store.dart';
// ignore: deprecated_member_use
2021-11-17 19:17:40 +01:00
class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
FlutterFluffyBoxDatabase(
2021-11-18 15:26:53 +01:00
String name,
String path, {
2021-12-27 09:05:16 +01:00
fluffybox.HiveCipher? key,
2021-11-17 12:20:15 +01:00
}) : super(
name,
2021-11-18 15:26:53 +01:00
path,
key: key,
2021-11-17 12:20:15 +01:00
);
static const String _cipherStorageKey = 'database_encryption_key';
// ignore: deprecated_member_use
2021-11-17 12:20:15 +01:00
static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
2021-11-17 12:48:45 +01:00
Logs().d('Open FluffyBox...');
2021-12-27 09:05:16 +01:00
fluffybox.HiveAesCipher? hiverCipher;
2021-11-18 11:18:55 +01:00
try {
2021-11-18 15:26:53 +01:00
// Workaround for secure storage is calling Platform.operatingSystem on web
if (kIsWeb) throw MissingPluginException();
2021-11-18 11:18:55 +01:00
const secureStorage = FlutterSecureStorage();
final containsEncryptionKey =
await secureStorage.containsKey(key: _cipherStorageKey);
if (!containsEncryptionKey) {
// do not try to create a buggy secure storage for new Linux users
if (Platform.isLinux) throw MissingPluginException();
2021-11-18 15:26:53 +01:00
final key = Hive.generateSecureKey();
2021-11-18 11:18:55 +01:00
await secureStorage.write(
key: _cipherStorageKey,
2021-11-18 15:26:53 +01:00
value: base64UrlEncode(key),
2021-11-18 11:18:55 +01:00
);
2021-11-17 12:20:15 +01:00
}
2021-11-18 11:18:55 +01:00
// workaround for if we just wrote to the key and it still doesn't exist
2021-11-18 15:26:53 +01:00
final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey);
if (rawEncryptionKey == null) throw MissingPluginException();
2021-12-27 09:05:16 +01:00
hiverCipher = fluffybox.HiveAesCipher(base64Url.decode(rawEncryptionKey));
2021-11-18 11:18:55 +01:00
} on MissingPluginException catch (_) {
Logs().i('FluffyBox encryption is not supported on this platform');
2021-11-21 09:07:33 +01:00
} catch (_) {
const FlutterSecureStorage().delete(key: _cipherStorageKey);
rethrow;
2021-11-17 12:20:15 +01:00
}
// ignore: deprecated_member_use
2021-11-17 12:20:15 +01:00
final db = FluffyBoxDatabase(
2021-11-17 19:17:40 +01:00
'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
2021-11-18 15:26:53 +01:00
await _findDatabasePath(client),
key: hiverCipher,
2021-11-17 12:20:15 +01:00
);
try {
await db.open();
} catch (_) {
Logs().w('Unable to open FluffyBox. Delete database and storage key...');
await Store().deleteItem(ClientManager.clientNamespace);
const FlutterSecureStorage().delete(key: _cipherStorageKey);
await db.clear();
rethrow;
}
2021-11-17 12:48:45 +01:00
Logs().d('FluffyBox is ready');
2021-11-17 12:20:15 +01:00
return db;
}
static Future<String> _findDatabasePath(Client client) async {
String path = client.clientName;
if (!kIsWeb) {
Directory directory;
try {
2021-11-30 22:13:40 +01:00
if (Platform.isLinux) {
directory = await getApplicationSupportDirectory();
} else {
directory = await getApplicationDocumentsDirectory();
}
2021-11-17 12:20:15 +01:00
} catch (_) {
try {
directory = await getLibraryDirectory();
} catch (_) {
directory = Directory.current;
}
}
// do not destroy your stable FluffyChat in debug mode
if (kDebugMode) {
directory = Directory(directory.uri.resolve('debug').toFilePath());
directory.create(recursive: true);
}
2021-11-18 15:26:53 +01:00
path = directory.path;
2021-11-17 12:20:15 +01:00
}
return path;
}
@override
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
@override
2021-11-17 19:29:17 +01:00
bool get supportsFileStoring => !kIsWeb;
2021-11-17 12:20:15 +01:00
Future<String> _getFileStoreDirectory() async {
try {
try {
return (await getApplicationSupportDirectory()).path;
} catch (_) {
return (await getApplicationDocumentsDirectory()).path;
}
} catch (_) {
2021-11-17 19:29:17 +01:00
return (await getDownloadsDirectory())!.path;
2021-11-17 12:20:15 +01:00
}
}
@override
2021-11-17 19:29:17 +01:00
Future<Uint8List?> getFile(Uri mxcUri) async {
2021-11-17 12:20:15 +01:00
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
final file =
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
if (await file.exists() == false) return null;
final bytes = await file.readAsBytes();
return bytes;
}
@override
Future storeFile(Uri mxcUri, Uint8List bytes, int time) async {
if (!supportsFileStoring) return null;
final tempDirectory = await _getFileStoreDirectory();
final file =
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
if (await file.exists()) return;
await file.writeAsBytes(bytes);
return;
}
}