From 68ed7767856437ddee04f5fd6286a71fd4a86ca3 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 20 Jul 2023 17:06:17 -0300 Subject: [PATCH] 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'; + } +}