2021-06-11 11:40:38 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
2021-06-18 10:29:48 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
2021-06-11 11:40:38 +02:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
|
2021-07-11 16:11:41 +02:00
|
|
|
import '../platform_infos.dart';
|
|
|
|
|
2021-07-23 18:39:18 +02:00
|
|
|
class FlutterMatrixHiveStore extends FamedlySdkHiveDatabase {
|
|
|
|
FlutterMatrixHiveStore(String name, {HiveCipher encryptionCipher})
|
2021-06-11 11:40:38 +02:00
|
|
|
: super(
|
|
|
|
name,
|
|
|
|
encryptionCipher: encryptionCipher,
|
2021-06-14 08:18:55 +02:00
|
|
|
);
|
2021-06-11 11:40:38 +02:00
|
|
|
|
2021-07-23 18:39:18 +02:00
|
|
|
Box _customBox;
|
|
|
|
String get _customBoxName => '$name.box.custom';
|
|
|
|
|
2021-06-11 11:40:38 +02:00
|
|
|
static bool _hiveInitialized = false;
|
|
|
|
static const String _hiveCipherStorageKey = 'hive_encryption_key';
|
|
|
|
|
2021-07-23 18:39:18 +02:00
|
|
|
@override
|
|
|
|
Future<void> open() async {
|
|
|
|
await super.open();
|
|
|
|
_customBox = await Hive.openBox(
|
|
|
|
_customBoxName,
|
|
|
|
encryptionCipher: encryptionCipher,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> clear(int clientId) async {
|
|
|
|
await super.clear(clientId);
|
|
|
|
await _customBox.deleteAll(_customBox.keys);
|
|
|
|
await _customBox.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic get(dynamic key) => _customBox.get(key);
|
|
|
|
Future<void> put(dynamic key, dynamic value) => _customBox.put(key, value);
|
|
|
|
|
2021-06-11 11:40:38 +02:00
|
|
|
static Future<FamedlySdkHiveDatabase> hiveDatabaseBuilder(
|
|
|
|
Client client) async {
|
|
|
|
if (!kIsWeb && !_hiveInitialized) {
|
|
|
|
Logs().i('Init Hive database...');
|
2021-07-11 16:11:41 +02:00
|
|
|
if (PlatformInfos.isLinux) {
|
2021-06-29 08:49:47 +02:00
|
|
|
Hive.init((await getApplicationSupportDirectory()).path);
|
2021-06-29 07:59:37 +02:00
|
|
|
} else {
|
|
|
|
await Hive.initFlutter();
|
|
|
|
}
|
2021-06-11 11:40:38 +02:00
|
|
|
_hiveInitialized = true;
|
|
|
|
}
|
|
|
|
HiveCipher hiverCipher;
|
|
|
|
try {
|
2021-06-12 13:41:22 +02:00
|
|
|
// Workaround for secure storage is calling Platform.operatingSystem on web
|
|
|
|
if (kIsWeb) throw MissingPluginException();
|
|
|
|
|
2021-06-11 11:40:38 +02:00
|
|
|
final secureStorage = const FlutterSecureStorage();
|
|
|
|
final containsEncryptionKey =
|
|
|
|
await secureStorage.containsKey(key: _hiveCipherStorageKey);
|
|
|
|
if (!containsEncryptionKey) {
|
|
|
|
final key = Hive.generateSecureKey();
|
|
|
|
await secureStorage.write(
|
|
|
|
key: _hiveCipherStorageKey,
|
|
|
|
value: base64UrlEncode(key),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-20 17:56:13 +02:00
|
|
|
// workaround for if we just wrote to the key and it still doesn't exist
|
|
|
|
final rawEncryptionKey =
|
|
|
|
await secureStorage.read(key: _hiveCipherStorageKey);
|
|
|
|
if (rawEncryptionKey == null) throw MissingPluginException();
|
|
|
|
|
|
|
|
final encryptionKey = base64Url.decode(rawEncryptionKey);
|
2021-06-11 11:40:38 +02:00
|
|
|
hiverCipher = HiveAesCipher(encryptionKey);
|
|
|
|
} on MissingPluginException catch (_) {
|
2021-06-14 08:18:55 +02:00
|
|
|
Logs().i('Hive encryption is not supported on this platform');
|
2021-06-11 11:40:38 +02:00
|
|
|
}
|
2021-07-23 18:39:18 +02:00
|
|
|
final db = FlutterMatrixHiveStore(
|
2021-06-11 11:40:38 +02:00
|
|
|
client.clientName,
|
|
|
|
encryptionCipher: hiverCipher,
|
|
|
|
);
|
|
|
|
Logs().i('Open Hive database...');
|
2021-08-02 18:38:53 +02:00
|
|
|
try {
|
|
|
|
await db.open();
|
|
|
|
} catch (e, s) {
|
|
|
|
Logs().e('Unable to open Hive. Delete and try again...', e, s);
|
2021-09-14 14:55:27 +02:00
|
|
|
await db.clear(client.id);
|
2021-08-02 18:38:53 +02:00
|
|
|
await db.open();
|
|
|
|
}
|
2021-06-11 11:40:38 +02:00
|
|
|
Logs().i('Hive database is ready!');
|
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-06-14 08:18:55 +02:00
|
|
|
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
|
2021-06-11 11:40:38 +02:00
|
|
|
@override
|
2021-07-11 16:11:41 +02:00
|
|
|
bool get supportsFileStoring => (PlatformInfos.isIOS ||
|
|
|
|
PlatformInfos.isAndroid ||
|
|
|
|
PlatformInfos.isDesktop);
|
2021-06-11 11:40:38 +02:00
|
|
|
|
2021-06-14 08:18:55 +02:00
|
|
|
Future<String> _getFileStoreDirectory() async {
|
|
|
|
try {
|
2021-07-11 16:11:41 +02:00
|
|
|
try {
|
|
|
|
return (await getApplicationSupportDirectory()).path;
|
2021-08-08 08:56:30 +02:00
|
|
|
} catch (_) {
|
2021-07-11 16:11:41 +02:00
|
|
|
return (await getApplicationDocumentsDirectory()).path;
|
|
|
|
}
|
2021-08-08 08:56:30 +02:00
|
|
|
} catch (_) {
|
2021-06-14 08:18:55 +02:00
|
|
|
return (await getDownloadsDirectory()).path;
|
|
|
|
}
|
2021-06-11 11:40:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-08-26 19:03:08 +02:00
|
|
|
Future<Uint8List> getFile(Uri mxcUri) async {
|
2021-06-14 08:18:55 +02:00
|
|
|
if (!supportsFileStoring) return null;
|
|
|
|
final tempDirectory = await _getFileStoreDirectory();
|
2021-08-26 19:03:08 +02:00
|
|
|
final file =
|
|
|
|
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
|
2021-06-11 11:40:38 +02:00
|
|
|
if (await file.exists() == false) return null;
|
|
|
|
final bytes = await file.readAsBytes();
|
2021-06-14 08:18:55 +02:00
|
|
|
return bytes;
|
2021-06-11 11:40:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-08-26 19:03:08 +02:00
|
|
|
Future storeFile(Uri mxcUri, Uint8List bytes, int time) async {
|
2021-06-14 08:18:55 +02:00
|
|
|
if (!supportsFileStoring) return null;
|
|
|
|
final tempDirectory = await _getFileStoreDirectory();
|
2021-08-26 19:03:08 +02:00
|
|
|
final file =
|
|
|
|
File('$tempDirectory/${Uri.encodeComponent(mxcUri.toString())}');
|
2021-06-11 11:40:38 +02:00
|
|
|
if (await file.exists()) return;
|
2021-06-14 08:18:55 +02:00
|
|
|
await file.writeAsBytes(bytes);
|
2021-06-11 11:40:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|