File system path clean up

This commit is contained in:
julian 2022-11-12 16:04:16 -06:00
parent d8b71579e3
commit 0164679cce
13 changed files with 119 additions and 93 deletions

View file

@ -53,6 +53,7 @@ import 'package:stackwallet/utilities/db_version_migration.dart';
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
import 'package:stackwallet/utilities/theme/color_theme.dart';
import 'package:stackwallet/utilities/theme/dark_colors.dart';
import 'package:stackwallet/utilities/theme/light_colors.dart';
@ -79,29 +80,11 @@ void main() async {
setWindowMaxSize(Size.infinite);
}
Directory appDirectory = (await getApplicationDocumentsDirectory());
if (Platform.isIOS) {
appDirectory = (await getLibraryDirectory());
}
if (Logging.isArmLinux) {
appDirectory = Directory("${appDirectory.path}/.stackwallet");
await appDirectory.create();
}
if (Platform.isLinux) {
appDirectory = Directory("${Platform.environment['HOME']}/.stackwallet");
if (!appDirectory.existsSync()) {
await appDirectory.create();
}
}
// FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
if (!(Logging.isArmLinux || Logging.isTestEnv)) {
final isar = await Isar.open(
[LogSchema],
directory: appDirectory.path,
directory: (await StackFileSystem.applicationIsarDirectory()).path,
inspector: false,
);
await Logging.instance.init(isar);
@ -150,7 +133,8 @@ void main() async {
Hive.registerAdapter(WalletTypeAdapter());
Hive.registerAdapter(UnspentCoinsInfoAdapter());
await Hive.initFlutter(appDirectory.path);
await Hive.initFlutter(
(await StackFileSystem.applicationHiveDirectory()).path);
await Hive.openBox<dynamic>(DB.boxNameDBInfo);

View file

@ -7,19 +7,25 @@ import 'package:event_bus/event_bus.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_libepiccash/git_versions.dart' as EPIC_VERSIONS;
import 'package:flutter_libmonero/git_versions.dart' as MONERO_VERSIONS;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:lelantus/git_versions.dart' as FIRO_VERSIONS;
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stackwallet/models/isar/models/log.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart';
import 'package:stackwallet/providers/global/debug_service_provider.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/clipboard_interface.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/flush_bar_type.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/theme/stack_colors.dart';
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart';
import 'package:stackwallet/widgets/custom_loading_overlay.dart';
@ -28,15 +34,6 @@ import 'package:stackwallet/widgets/rounded_container.dart';
import 'package:stackwallet/widgets/stack_dialog.dart';
import 'package:stackwallet/widgets/stack_text_field.dart';
import 'package:stackwallet/widgets/textfield_icon_button.dart';
import 'package:flutter_libepiccash/git_versions.dart' as EPIC_VERSIONS;
import 'package:flutter_libmonero/git_versions.dart' as MONERO_VERSIONS;
import 'package:lelantus/git_versions.dart' as FIRO_VERSIONS;
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart';
import 'package:stackwallet/utilities/clipboard_interface.dart';
import 'package:stackwallet/utilities/util.dart';
class DebugView extends ConsumerStatefulWidget {
const DebugView({Key? key}) : super(key: key);
@ -352,10 +349,10 @@ class _DebugViewState extends ConsumerState<DebugView> {
BlueTextButton(
text: "Save logs to file",
onTap: () async {
final systemfile = StackFileSystem();
final systemfile = SWBFileSystem();
await systemfile.prepareStorage();
Directory rootPath =
(await getApplicationDocumentsDirectory());
Directory rootPath = await StackFileSystem
.applicationRootDirectory();
if (Platform.isAndroid) {
rootPath = Directory("/storage/emulated/0/");

View file

@ -9,7 +9,7 @@ import 'package:stack_wallet_backup/stack_wallet_backup.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/auto_backup_view.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/sub_views/backup_frequency_type_select_sheet.dart';
import 'package:stackwallet/providers/global/prefs_provider.dart';
import 'package:stackwallet/providers/global/secure_store_provider.dart';
@ -49,7 +49,7 @@ class _EnableAutoBackupViewState extends ConsumerState<CreateAutoBackupView> {
late final FocusNode passwordFocusNode;
late final FocusNode passwordRepeatFocusNode;
late final StackFileSystem stackFileSystem;
late final SWBFileSystem stackFileSystem;
final zxcvbn = Zxcvbn();
String passwordFeedback =
@ -70,7 +70,7 @@ class _EnableAutoBackupViewState extends ConsumerState<CreateAutoBackupView> {
@override
void initState() {
secureStore = ref.read(secureStoreProvider);
stackFileSystem = StackFileSystem();
stackFileSystem = SWBFileSystem();
fileLocationController = TextEditingController();
passwordController = TextEditingController();
passwordRepeatController = TextEditingController();

View file

@ -7,7 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart';
import 'package:stackwallet/providers/global/secure_store_provider.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
@ -41,7 +41,7 @@ class _RestoreFromFileViewState extends State<CreateBackupView> {
late final FocusNode passwordFocusNode;
late final FocusNode passwordRepeatFocusNode;
late final StackFileSystem stackFileSystem;
late final SWBFileSystem stackFileSystem;
final zxcvbn = Zxcvbn();
String passwordFeedback =
@ -61,7 +61,7 @@ class _RestoreFromFileViewState extends State<CreateBackupView> {
@override
void initState() {
stackFileSystem = StackFileSystem();
stackFileSystem = SWBFileSystem();
fileLocationController = TextEditingController();
passwordController = TextEditingController();
passwordRepeatController = TextEditingController();

View file

@ -9,7 +9,7 @@ import 'package:stack_wallet_backup/stack_wallet_backup.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/auto_backup_view.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/sub_views/backup_frequency_type_select_sheet.dart';
import 'package:stackwallet/providers/global/prefs_provider.dart';
import 'package:stackwallet/providers/global/secure_store_provider.dart';
@ -48,7 +48,7 @@ class _EditAutoBackupViewState extends ConsumerState<EditAutoBackupView> {
late final FocusNode passwordFocusNode;
late final FocusNode passwordRepeatFocusNode;
late final StackFileSystem stackFileSystem;
late final SWBFileSystem stackFileSystem;
final zxcvbn = Zxcvbn();
String passwordFeedback =
@ -69,7 +69,7 @@ class _EditAutoBackupViewState extends ConsumerState<EditAutoBackupView> {
@override
void initState() {
secureStore = ref.read(secureStoreProvider);
stackFileSystem = StackFileSystem();
stackFileSystem = SWBFileSystem();
fileLocationController = TextEditingController();
passwordController = TextEditingController();
passwordRepeatController = TextEditingController();

View file

@ -4,15 +4,16 @@ import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:stackwallet/utilities/util.dart';
class StackFileSystem {
class SWBFileSystem {
Directory? rootPath;
Directory? startPath;
String? filePath;
String? dirPath;
final bool isDesktop = !(Platform.isAndroid || Platform.isIOS);
final bool isDesktop = Util.isDesktop;
Future<Directory> prepareStorage() async {
if (Platform.isAndroid) {

View file

@ -7,7 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/sub_views/stack_restore_progress_view.dart';
import 'package:stackwallet/route_generator.dart';
import 'package:stackwallet/utilities/assets.dart';
@ -20,14 +20,13 @@ import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
import 'package:stackwallet/widgets/desktop/desktop_dialog.dart';
import 'package:stackwallet/widgets/desktop/desktop_dialog_close_button.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/desktop/secondary_button.dart';
import 'package:stackwallet/widgets/loading_indicator.dart';
import 'package:stackwallet/widgets/stack_text_field.dart';
import 'package:tuple/tuple.dart';
import '../../../../widgets/desktop/desktop_dialog_close_button.dart';
class RestoreFromFileView extends ConsumerStatefulWidget {
const RestoreFromFileView({Key? key}) : super(key: key);
@ -44,13 +43,13 @@ class _RestoreFromFileViewState extends ConsumerState<RestoreFromFileView> {
late final FocusNode passwordFocusNode;
late final StackFileSystem stackFileSystem;
late final SWBFileSystem stackFileSystem;
bool hidePassword = true;
@override
void initState() {
stackFileSystem = StackFileSystem();
stackFileSystem = SWBFileSystem();
fileLocationController = TextEditingController();
passwordController = TextEditingController();

View file

@ -9,14 +9,13 @@ import 'package:flutter_svg/svg.dart';
import 'package:stack_wallet_backup/stack_wallet_backup.dart';
import 'package:stackwallet/notifications/show_flush_bar.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/stack_file_system.dart';
import 'package:stackwallet/pages/settings_views/global_settings_view/stack_backup_views/helpers/swb_file_system.dart';
import 'package:stackwallet/providers/global/prefs_provider.dart';
import 'package:stackwallet/providers/global/secure_store_provider.dart';
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/constants.dart';
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
import 'package:stackwallet/utilities/enums/flush_bar_type.dart';
import 'package:stackwallet/utilities/enums/log_level_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/format.dart';
import 'package:stackwallet/utilities/logger.dart';
@ -48,7 +47,7 @@ class _CreateAutoBackup extends ConsumerState<CreateAutoBackup> {
late final SecureStorageInterface secureStore;
late final StackFileSystem stackFileSystem;
late final SWBFileSystem stackFileSystem;
late final FocusNode passphraseFocusNode;
late final FocusNode passphraseRepeatFocusNode;
final zxcvbn = Zxcvbn();
@ -81,7 +80,7 @@ class _CreateAutoBackup extends ConsumerState<CreateAutoBackup> {
@override
void initState() {
secureStore = ref.read(secureStoreProvider);
stackFileSystem = StackFileSystem();
stackFileSystem = SWBFileSystem();
fileLocationController = TextEditingController();
passphraseController = TextEditingController();

View file

@ -9,7 +9,6 @@ import 'package:flutter_libepiccash/epic_cash.dart';
import 'package:hive/hive.dart';
import 'package:http/http.dart';
import 'package:mutex/mutex.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stack_wallet_backup/generate_password.dart';
import 'package:stackwallet/hive/db.dart';
import 'package:stackwallet/models/node_model.dart';
@ -31,6 +30,7 @@ import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
import 'package:stackwallet/utilities/test_epic_box_connection.dart';
import 'package:tuple/tuple.dart';
@ -253,14 +253,16 @@ Future<String> deleteEpicWallet({
required SecureStorageInterface secureStore,
}) async {
String? config = await secureStore.read(key: '${walletId}_config');
// TODO: why double check for iOS?
if (Platform.isIOS) {
Directory appDir = (await getApplicationDocumentsDirectory());
if (Platform.isIOS) {
appDir = (await getLibraryDirectory());
}
if (Platform.isLinux) {
appDir = Directory("${appDir.path}/.stackwallet");
}
Directory appDir = await StackFileSystem.applicationRootDirectory();
// todo why double check for ios?
// if (Platform.isIOS) {
// appDir = (await getLibraryDirectory());
// }
// if (Platform.isLinux) {
// appDir = Directory("${appDir.path}/.stackwallet");
// }
final path = "${appDir.path}/epiccash";
final String name = walletId;
@ -1237,13 +1239,8 @@ class EpicCashWallet extends CoinServiceAPI {
}
Future<String> currentWalletDirPath() async {
Directory appDir = (await getApplicationDocumentsDirectory());
if (Platform.isIOS) {
appDir = (await getLibraryDirectory());
}
if (Platform.isLinux) {
appDir = Directory("${appDir.path}/.stackwallet");
}
Directory appDir = await StackFileSystem.applicationRootDirectory();
final path = "${appDir.path}/epiccash";
final String name = _walletId.trim();
return '$path/$name';

View file

@ -25,7 +25,6 @@ import 'package:flutter_libmonero/monero/monero.dart';
import 'package:flutter_libmonero/view_model/send/output.dart' as monero_output;
import 'package:http/http.dart';
import 'package:mutex/mutex.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stackwallet/hive/db.dart';
import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/models/paymint/fee_object_model.dart';
@ -47,6 +46,7 @@ import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
const int MINIMUM_CONFIRMATIONS = 10;
@ -897,14 +897,8 @@ class MoneroWallet extends CoinServiceAPI {
required String name,
required WalletType type,
}) async {
Directory root = (await getApplicationDocumentsDirectory());
if (Platform.isIOS) {
root = (await getLibraryDirectory());
}
//
if (Platform.isLinux) {
root = Directory("${root.path}/.stackwallet");
}
Directory root = await StackFileSystem.applicationRootDirectory();
final prefix = walletTypeToString(type).toLowerCase();
final walletsDir = Directory('${root.path}/wallets');
final walletDire = Directory('${walletsDir.path}/$prefix/$name');

View file

@ -26,7 +26,6 @@ import 'package:flutter_libmonero/view_model/send/output.dart'
import 'package:flutter_libmonero/wownero/wownero.dart';
import 'package:http/http.dart';
import 'package:mutex/mutex.dart';
import 'package:path_provider/path_provider.dart';
import 'package:stackwallet/hive/db.dart';
import 'package:stackwallet/models/node_model.dart';
import 'package:stackwallet/models/paymint/fee_object_model.dart';
@ -48,6 +47,7 @@ import 'package:stackwallet/utilities/enums/fee_rate_type_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
const int MINIMUM_CONFIRMATIONS = 10;
@ -901,13 +901,8 @@ class WowneroWallet extends CoinServiceAPI {
required String name,
required WalletType type,
}) async {
Directory root = (await getApplicationDocumentsDirectory());
if (Platform.isIOS) {
root = (await getLibraryDirectory());
}
if (Platform.isLinux) {
root = Directory("${root.path}/.stackwallet");
}
Directory root = await StackFileSystem.applicationRootDirectory();
final prefix = walletTypeToString(type).toLowerCase();
final walletsDir = Directory('${root.path}/wallets');
final walletDire = Directory('${walletsDir.path}/$prefix/$name');

View file

@ -1,9 +1,8 @@
import 'dart:io';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:isar/isar.dart';
import 'package:stack_wallet_backup/secure_storage.dart';
import 'package:stackwallet/models/isar/models/encrypted_string_value.dart';
import 'package:stackwallet/utilities/stack_file_system.dart';
abstract class SecureStorageInterface {
dynamic get store;
@ -47,14 +46,9 @@ class DesktopSecureStore {
DesktopSecureStore(this.handler);
Future<void> init() async {
Directory? appDirectory;
if (Platform.isLinux) {
appDirectory = Directory("${Platform.environment['HOME']}/.stackwallet");
await appDirectory.create();
}
isar = await Isar.open(
[EncryptedStringValueSchema],
directory: appDirectory!.path,
directory: (await StackFileSystem.applicationIsarDirectory()).path,
inspector: false,
name: "desktopStore",
);

View file

@ -0,0 +1,66 @@
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/util.dart';
abstract class StackFileSystem {
static Future<Directory> applicationRootDirectory() async {
Directory appDirectory;
// todo: can merge and do same as regular linux home dir?
if (Logging.isArmLinux) {
appDirectory = await getApplicationDocumentsDirectory();
appDirectory = Directory("${appDirectory.path}/.stackwallet");
} else if (Platform.isLinux) {
appDirectory = Directory("${Platform.environment['HOME']}/.stackwallet");
} else if (Platform.isWindows) {
// TODO: windows root .stackwallet dir location
throw Exception("Unsupported platform");
} else if (Platform.isMacOS) {
// currently run in ipad mode??
throw Exception("Unsupported platform");
} else if (Platform.isIOS) {
// todo: check if we need different behaviour here
if (Util.isDesktop) {
appDirectory = await getLibraryDirectory();
} else {
appDirectory = await getLibraryDirectory();
}
} else if (Platform.isAndroid) {
appDirectory = await getApplicationDocumentsDirectory();
} else {
throw Exception("Unsupported platform");
}
if (!appDirectory.existsSync()) {
await appDirectory.create(recursive: true);
}
return appDirectory;
}
static Future<Directory> applicationIsarDirectory() async {
final root = await applicationRootDirectory();
if (Util.isDesktop) {
final dir = Directory("${root.path}/isar");
if (!dir.existsSync()) {
await dir.create();
}
return dir;
} else {
return root;
}
}
static Future<Directory> applicationHiveDirectory() async {
final root = await applicationRootDirectory();
if (Util.isDesktop) {
final dir = Directory("${root.path}/hive");
if (!dir.existsSync()) {
await dir.create();
}
return dir;
} else {
return root;
}
}
}