From 68ed7767856437ddee04f5fd6286a71fd4a86ca3 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 20 Jul 2023 17:06:17 -0300 Subject: [PATCH 1/5] feat(platform): Implement custom platform-dependent storage path definition - Create new PlatformAdapter util and hide i/o there - Move other usages of Platform class in PlatformAdapter --- lib/config/hive_config.dart | 3 +- .../server_installation_repository.dart | 49 ++++----------- lib/utils/platform_adapter.dart | 59 +++++++++++++++++++ 3 files changed, 71 insertions(+), 40 deletions(-) create mode 100644 lib/utils/platform_adapter.dart diff --git a/lib/config/hive_config.dart b/lib/config/hive_config.dart index 25dee2c8..5d6e3eca 100644 --- a/lib/config/hive_config.dart +++ b/lib/config/hive_config.dart @@ -8,10 +8,11 @@ import 'package:selfprivacy/logic/models/hive/backups_credential.dart'; import 'package:selfprivacy/logic/models/hive/server_details.dart'; import 'package:selfprivacy/logic/models/hive/server_domain.dart'; import 'package:selfprivacy/logic/models/hive/user.dart'; +import 'package:selfprivacy/utils/platform_adapter.dart'; class HiveConfig { static Future init() async { - await Hive.initFlutter(); + await Hive.initFlutter(PlatformAdapter.storagePath); Hive.registerAdapter(UserAdapter()); Hive.registerAdapter(ServerHostingDetailsAdapter()); Hive.registerAdapter(ServerDomainAdapter()); diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index 717b7535..45b19b21 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -1,9 +1,7 @@ import 'dart:async'; import 'dart:io'; -import 'package:device_info_plus/device_info_plus.dart'; import 'package:easy_localization/easy_localization.dart'; -import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; import 'package:pub_semver/pub_semver.dart'; import 'package:selfprivacy/config/get_it_config.dart'; @@ -22,6 +20,7 @@ import 'package:selfprivacy/logic/models/server_basic_info.dart'; import 'package:selfprivacy/logic/models/server_type.dart'; import 'package:selfprivacy/logic/providers/providers_controller.dart'; import 'package:selfprivacy/utils/network_utils.dart'; +import 'package:selfprivacy/utils/platform_adapter.dart'; class IpNotFoundException implements Exception { IpNotFoundException(this.message); @@ -285,40 +284,6 @@ class ServerInstallationRepository { return domain!; } - Future getDeviceName() async { - final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); - if (kIsWeb) { - return deviceInfo.webBrowserInfo.then( - (final WebBrowserInfo value) => - '${value.browserName} ${value.platform}', - ); - } else { - if (Platform.isAndroid) { - return deviceInfo.androidInfo.then( - (final AndroidDeviceInfo value) => - '${value.model} ${value.version.release}', - ); - } else if (Platform.isIOS) { - return deviceInfo.iosInfo.then( - (final IosDeviceInfo value) => - '${value.utsname.machine} ${value.systemName} ${value.systemVersion}', - ); - } else if (Platform.isLinux) { - return deviceInfo.linuxInfo - .then((final LinuxDeviceInfo value) => value.prettyName); - } else if (Platform.isMacOS) { - return deviceInfo.macOsInfo.then( - (final MacOsDeviceInfo value) => - '${value.hostName} ${value.computerName}', - ); - } else if (Platform.isWindows) { - return deviceInfo.windowsInfo - .then((final WindowsDeviceInfo value) => value.computerName); - } - } - return 'Unidentified'; - } - Future authorizeByNewDeviceKey( final ServerDomain serverDomain, final String newDeviceKey, @@ -330,7 +295,10 @@ class ServerInstallationRepository { ); final String serverIp = await getServerIpFromDomain(serverDomain); final GenericResult result = await serverApi.authorizeDevice( - DeviceToken(device: await getDeviceName(), token: newDeviceKey), + DeviceToken( + device: await PlatformAdapter.deviceName, + token: newDeviceKey, + ), ); if (result.success) { @@ -367,7 +335,7 @@ class ServerInstallationRepository { ); final String serverIp = await getServerIpFromDomain(serverDomain); final GenericResult result = await serverApi.useRecoveryToken( - DeviceToken(device: await getDeviceName(), token: recoveryKey), + DeviceToken(device: await PlatformAdapter.deviceName, token: recoveryKey), ); if (result.success) { @@ -430,7 +398,10 @@ class ServerInstallationRepository { final GenericResult deviceAuthKey = await serverApi.createDeviceToken(); final GenericResult result = await serverApi.authorizeDevice( - DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data), + DeviceToken( + device: await PlatformAdapter.deviceName, + token: deviceAuthKey.data, + ), ); if (result.success) { diff --git a/lib/utils/platform_adapter.dart b/lib/utils/platform_adapter.dart new file mode 100644 index 00000000..6a37d035 --- /dev/null +++ b/lib/utils/platform_adapter.dart @@ -0,0 +1,59 @@ +import 'dart:io'; + +import 'package:device_info_plus/device_info_plus.dart'; +import 'package:flutter/foundation.dart'; + +/// SelfPrivacy wrapper for Platform information provider. +class PlatformAdapter { + /// Persistent storage directory for data files. + static String get storagePath { + String path = '.'; + if (Platform.isLinux) { + // https://wiki.archlinux.org/title/XDG_Base_Directory + path = Platform.environment['XDG_DATA_HOME'] ?? '.'; + if (path == '.') { + final String home = Platform.environment['HOME'] ?? '.'; + path += '$home/.local/share'; + } + path += '/selfprivacy'; + } + + return path; + } + + /// Running operation environment. + static Future get deviceName async { + final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); + if (kIsWeb) { + return deviceInfo.webBrowserInfo.then( + (final WebBrowserInfo value) => + '${value.browserName} ${value.platform}', + ); + } else { + if (Platform.isAndroid) { + return deviceInfo.androidInfo.then( + (final AndroidDeviceInfo value) => + '${value.model} ${value.version.release}', + ); + } else if (Platform.isIOS) { + return deviceInfo.iosInfo.then( + (final IosDeviceInfo value) => + '${value.utsname.machine} ${value.systemName} ${value.systemVersion}', + ); + } else if (Platform.isLinux) { + return deviceInfo.linuxInfo + .then((final LinuxDeviceInfo value) => value.prettyName); + } else if (Platform.isMacOS) { + return deviceInfo.macOsInfo.then( + (final MacOsDeviceInfo value) => + '${value.hostName} ${value.computerName}', + ); + } else if (Platform.isWindows) { + return deviceInfo.windowsInfo + .then((final WindowsDeviceInfo value) => value.computerName); + } + } + + return 'Unidentified'; + } +} From 25d68818379d8bf3605b2ad02112e98a7bf15d24 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 20 Jul 2023 17:07:08 -0300 Subject: [PATCH 2/5] chore: Remove misleading commentary --- lib/ui/pages/providers/providers.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ui/pages/providers/providers.dart b/lib/ui/pages/providers/providers.dart index bdae6e07..64b87d34 100644 --- a/lib/ui/pages/providers/providers.dart +++ b/lib/ui/pages/providers/providers.dart @@ -96,7 +96,6 @@ class _ProvidersPageState extends State { onTap: () => context.pushRoute(const DnsDetailsRoute()), ), const SizedBox(height: 16), - // TODO: When backups are fixed, show this card _Card( state: isBackupInitialized ? StateType.stable From 3b1eee5a9478dca6e9795de1071308f8d704df26 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 20 Jul 2023 17:14:17 -0300 Subject: [PATCH 3/5] fix(platform): Make platform storage path nullable for compability with previous behaviour --- lib/utils/platform_adapter.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/utils/platform_adapter.dart b/lib/utils/platform_adapter.dart index 6a37d035..c1bdcbde 100644 --- a/lib/utils/platform_adapter.dart +++ b/lib/utils/platform_adapter.dart @@ -6,14 +6,14 @@ import 'package:flutter/foundation.dart'; /// SelfPrivacy wrapper for Platform information provider. class PlatformAdapter { /// Persistent storage directory for data files. - static String get storagePath { - String path = '.'; + static String? get storagePath { + String? path; if (Platform.isLinux) { // https://wiki.archlinux.org/title/XDG_Base_Directory - path = Platform.environment['XDG_DATA_HOME'] ?? '.'; - if (path == '.') { + path = Platform.environment['XDG_DATA_HOME']; + if (path == null) { final String home = Platform.environment['HOME'] ?? '.'; - path += '$home/.local/share'; + path = '$home/.local/share'; } path += '/selfprivacy'; } From 06a857aa8c03dd7d1caa7c244872f28713c707b2 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 20 Jul 2023 17:19:29 -0300 Subject: [PATCH 4/5] chore(platform): Fix comment typo --- lib/utils/platform_adapter.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/platform_adapter.dart b/lib/utils/platform_adapter.dart index c1bdcbde..fa40d7d9 100644 --- a/lib/utils/platform_adapter.dart +++ b/lib/utils/platform_adapter.dart @@ -21,7 +21,7 @@ class PlatformAdapter { return path; } - /// Running operation environment. + /// Running operating environment. static Future get deviceName async { final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); if (kIsWeb) { From d8660b9f3aa7d112c434093cab412f9070653682 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Fri, 21 Jul 2023 14:44:01 -0300 Subject: [PATCH 5/5] feat(platform): Print storage path before Hive initialization --- lib/config/hive_config.dart | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/config/hive_config.dart b/lib/config/hive_config.dart index 5d6e3eca..01118bba 100644 --- a/lib/config/hive_config.dart +++ b/lib/config/hive_config.dart @@ -12,7 +12,9 @@ import 'package:selfprivacy/utils/platform_adapter.dart'; class HiveConfig { static Future init() async { - await Hive.initFlutter(PlatformAdapter.storagePath); + final String? storagePath = PlatformAdapter.storagePath; + print('HiveConfig: Custom storage path: $storagePath'); + await Hive.initFlutter(storagePath); Hive.registerAdapter(UserAdapter()); Hive.registerAdapter(ServerHostingDetailsAdapter()); Hive.registerAdapter(ServerDomainAdapter());