mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2025-02-20 08:00:39 +01:00
feat: FluffyBox
This commit is contained in:
parent
815d4d5a35
commit
4822737047
@ -10,7 +10,7 @@ import 'package:path_provider/path_provider.dart';
|
|||||||
|
|
||||||
import 'package:fluffychat/utils/platform_infos.dart';
|
import 'package:fluffychat/utils/platform_infos.dart';
|
||||||
import 'famedlysdk_store.dart';
|
import 'famedlysdk_store.dart';
|
||||||
import 'matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart';
|
import 'matrix_sdk_extensions.dart/fluffybox_database.dart';
|
||||||
import 'matrix_sdk_extensions.dart/flutter_matrix_sembast_database.dart';
|
import 'matrix_sdk_extensions.dart/flutter_matrix_sembast_database.dart';
|
||||||
|
|
||||||
abstract class ClientManager {
|
abstract class ClientManager {
|
||||||
@ -80,8 +80,8 @@ abstract class ClientManager {
|
|||||||
KeyVerificationMethod.emoji,
|
KeyVerificationMethod.emoji,
|
||||||
},
|
},
|
||||||
importantStateEvents: <String>{'im.ponies.room_emotes'},
|
importantStateEvents: <String>{'im.ponies.room_emotes'},
|
||||||
databaseBuilder: FlutterMatrixSembastDatabase.databaseBuilder,
|
databaseBuilder: FluffyBoxDatabase.databaseBuilder,
|
||||||
legacyDatabaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder,
|
legacyDatabaseBuilder: FlutterMatrixSembastDatabase.databaseBuilder,
|
||||||
supportedLoginTypes: {
|
supportedLoginTypes: {
|
||||||
AuthenticationTypes.password,
|
AuthenticationTypes.password,
|
||||||
if (PlatformInfos.isMobile || PlatformInfos.isWeb)
|
if (PlatformInfos.isMobile || PlatformInfos.isWeb)
|
||||||
|
134
lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart
Normal file
134
lib/utils/matrix_sdk_extensions.dart/fluffybox_database.dart
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart' hide Key;
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:idb_shim/idb_browser.dart';
|
||||||
|
import 'package:idb_shim/idb.dart' hide Event;
|
||||||
|
import 'package:sqflite/sqflite.dart' as sqflite;
|
||||||
|
|
||||||
|
import 'package:encrypt/encrypt.dart';
|
||||||
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
|
import 'package:idb_sqflite/idb_sqflite.dart';
|
||||||
|
import 'package:matrix/matrix.dart';
|
||||||
|
import 'package:path_provider/path_provider.dart';
|
||||||
|
|
||||||
|
import '../platform_infos.dart';
|
||||||
|
|
||||||
|
class FluffyBoxDatabase extends MatrixIndexedDatabase {
|
||||||
|
FluffyBoxDatabase(
|
||||||
|
String name, {
|
||||||
|
String path,
|
||||||
|
IdbFactory dbFactory,
|
||||||
|
}) : super(
|
||||||
|
name,
|
||||||
|
path,
|
||||||
|
factory: dbFactory,
|
||||||
|
);
|
||||||
|
|
||||||
|
static const String _cipherStorageKey = 'database_encryption_key';
|
||||||
|
static const int _cipherStorageKeyLength = 512;
|
||||||
|
|
||||||
|
static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
|
||||||
|
Logs().d('Open Sembast...');
|
||||||
|
try {
|
||||||
|
// Workaround for secure storage is calling Platform.operatingSystem on web
|
||||||
|
if (kIsWeb) throw MissingPluginException();
|
||||||
|
|
||||||
|
const secureStorage = FlutterSecureStorage();
|
||||||
|
final containsEncryptionKey =
|
||||||
|
await secureStorage.containsKey(key: _cipherStorageKey);
|
||||||
|
if (!containsEncryptionKey) {
|
||||||
|
final key = SecureRandom(_cipherStorageKeyLength).base64;
|
||||||
|
await secureStorage.write(
|
||||||
|
key: _cipherStorageKey,
|
||||||
|
value: key,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// workaround for if we just wrote to the key and it still doesn't exist
|
||||||
|
final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey);
|
||||||
|
if (rawEncryptionKey == null) throw MissingPluginException();
|
||||||
|
|
||||||
|
//codec = getEncryptSembastCodec(password: rawEncryptionKey);
|
||||||
|
} on MissingPluginException catch (_) {
|
||||||
|
Logs().i('Sembast encryption is not supported on this platform');
|
||||||
|
}
|
||||||
|
|
||||||
|
final db = FluffyBoxDatabase(
|
||||||
|
client.clientName,
|
||||||
|
path: await _findDatabasePath(client),
|
||||||
|
dbFactory: factory,
|
||||||
|
);
|
||||||
|
await db.open();
|
||||||
|
Logs().d('Sembast is ready');
|
||||||
|
return db;
|
||||||
|
}
|
||||||
|
|
||||||
|
static IdbFactory get factory {
|
||||||
|
if (kIsWeb) return idbFactoryBrowser;
|
||||||
|
if (Platform.isAndroid || Platform.isIOS) {
|
||||||
|
return getIdbFactorySqflite(sqflite.databaseFactory);
|
||||||
|
}
|
||||||
|
return idbFactoryNative;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<String> _findDatabasePath(Client client) async {
|
||||||
|
String path = client.clientName;
|
||||||
|
if (!kIsWeb) {
|
||||||
|
Directory directory;
|
||||||
|
try {
|
||||||
|
directory = await getApplicationSupportDirectory();
|
||||||
|
} catch (_) {
|
||||||
|
try {
|
||||||
|
directory = await getLibraryDirectory();
|
||||||
|
} catch (_) {
|
||||||
|
directory = Directory.current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path = '${directory.path}${client.clientName}.db';
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
|
||||||
|
@override
|
||||||
|
bool get supportsFileStoring => (PlatformInfos.isIOS ||
|
||||||
|
PlatformInfos.isAndroid ||
|
||||||
|
PlatformInfos.isDesktop);
|
||||||
|
|
||||||
|
Future<String> _getFileStoreDirectory() async {
|
||||||
|
try {
|
||||||
|
try {
|
||||||
|
return (await getApplicationSupportDirectory()).path;
|
||||||
|
} catch (_) {
|
||||||
|
return (await getApplicationDocumentsDirectory()).path;
|
||||||
|
}
|
||||||
|
} catch (_) {
|
||||||
|
return (await getDownloadsDirectory()).path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Uint8List> getFile(Uri mxcUri) async {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,7 @@ import package_info_plus_macos
|
|||||||
import path_provider_macos
|
import path_provider_macos
|
||||||
import shared_preferences_macos
|
import shared_preferences_macos
|
||||||
import sqflite
|
import sqflite
|
||||||
|
import sqflite_sqlcipher
|
||||||
import url_launcher_macos
|
import url_launcher_macos
|
||||||
import wakelock_macos
|
import wakelock_macos
|
||||||
|
|
||||||
@ -27,6 +28,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
|||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||||
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
|
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
|
||||||
|
SqfliteSqlCipherPlugin.register(with: registry.registrar(forPlugin: "SqfliteSqlCipherPlugin"))
|
||||||
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
|
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
|
||||||
WakelockMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockMacosPlugin"))
|
WakelockMacosPlugin.register(with: registry.registrar(forPlugin: "WakelockMacosPlugin"))
|
||||||
}
|
}
|
||||||
|
22
pubspec.lock
22
pubspec.lock
@ -651,6 +651,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.1"
|
version: "2.0.1"
|
||||||
|
idb_sqflite:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: idb_sqflite
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.1"
|
||||||
image:
|
image:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -773,9 +780,11 @@ packages:
|
|||||||
matrix:
|
matrix:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: matrix
|
path: "."
|
||||||
url: "https://pub.dartlang.org"
|
ref: "krille/idb"
|
||||||
source: hosted
|
resolved-ref: "5996f6bb01ba1110c1f8fb84c39dd6bcf0d2900a"
|
||||||
|
url: "https://gitlab.com/famedly/company/frontend/famedlysdk.git"
|
||||||
|
source: git
|
||||||
version: "0.7.0-nullsafety.6"
|
version: "0.7.0-nullsafety.6"
|
||||||
matrix_api_lite:
|
matrix_api_lite:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
@ -1309,6 +1318,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0+2"
|
version: "2.0.0+2"
|
||||||
|
sqflite_sqlcipher:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: sqflite_sqlcipher
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "2.1.0"
|
||||||
stack_trace:
|
stack_trace:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -42,6 +42,7 @@ dependencies:
|
|||||||
future_loading_dialog: ^0.2.1
|
future_loading_dialog: ^0.2.1
|
||||||
geolocator: ^7.6.2
|
geolocator: ^7.6.2
|
||||||
hive_flutter: ^1.1.0
|
hive_flutter: ^1.1.0
|
||||||
|
idb_sqflite: ^1.0.1
|
||||||
image_picker: ^0.8.4+2
|
image_picker: ^0.8.4+2
|
||||||
intl: any
|
intl: any
|
||||||
localstorage: ^4.0.0+1
|
localstorage: ^4.0.0+1
|
||||||
@ -67,6 +68,7 @@ dependencies:
|
|||||||
sentry: ^6.0.1
|
sentry: ^6.0.1
|
||||||
share: ^2.0.4
|
share: ^2.0.4
|
||||||
slugify: ^2.0.0
|
slugify: ^2.0.0
|
||||||
|
sqflite_sqlcipher: ^2.1.0
|
||||||
swipe_to_action: ^0.2.0
|
swipe_to_action: ^0.2.0
|
||||||
uni_links: ^0.5.1
|
uni_links: ^0.5.1
|
||||||
unifiedpush: ^1.0.6
|
unifiedpush: ^1.0.6
|
||||||
@ -115,4 +117,8 @@ dependency_overrides:
|
|||||||
hosted:
|
hosted:
|
||||||
name: geolocator_android
|
name: geolocator_android
|
||||||
url: https://hanntech-gmbh.gitlab.io/free2pass/flutter-geolocator-floss
|
url: https://hanntech-gmbh.gitlab.io/free2pass/flutter-geolocator-floss
|
||||||
|
matrix:
|
||||||
|
git:
|
||||||
|
url: https://gitlab.com/famedly/company/frontend/famedlysdk.git
|
||||||
|
ref: krille/idb
|
||||||
provider: 5.0.0
|
provider: 5.0.0
|
Loading…
x
Reference in New Issue
Block a user