From 8efedbccf6380fe7f2d1993293b35d0a2583d979 Mon Sep 17 00:00:00 2001 From: M Date: Thu, 13 Apr 2023 19:48:51 -0400 Subject: [PATCH] Add ability to set custom data application directory --- cw_core/lib/pathForWallet.dart | 5 +++-- cw_core/lib/root_dir.dart | 26 ++++++++++++++++++++++++++ lib/core/backup_service.dart | 15 ++++++++------- lib/main.dart | 5 +++-- lib/utils/exception_handler.dart | 5 +++-- lib/view_model/backup_view_model.dart | 5 +++-- 6 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 cw_core/lib/root_dir.dart diff --git a/cw_core/lib/pathForWallet.dart b/cw_core/lib/pathForWallet.dart index af4838ffa..5b27a4729 100644 --- a/cw_core/lib/pathForWallet.dart +++ b/cw_core/lib/pathForWallet.dart @@ -1,10 +1,11 @@ import 'dart:io'; +import 'package:cw_core/root_dir.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:flutter/foundation.dart'; import 'package:path_provider/path_provider.dart'; Future pathForWalletDir({required String name, required WalletType type}) async { - final root = await getApplicationDocumentsDirectory(); + final root = await getAppDir(); final prefix = walletTypeToString(type).toLowerCase(); final walletsDir = Directory('${root.path}/wallets'); final walletDire = Directory('${walletsDir.path}/$prefix/$name'); @@ -21,7 +22,7 @@ Future pathForWallet({required String name, required WalletType type}) a .then((path) => path + '/$name'); Future outdatedAndroidPathForWalletDir({required String name}) async { - final directory = await getApplicationDocumentsDirectory(); + final directory = await getAppDir(); final pathDir = directory.path + '/$name'; return pathDir; diff --git a/cw_core/lib/root_dir.dart b/cw_core/lib/root_dir.dart new file mode 100644 index 000000000..efbe59ce9 --- /dev/null +++ b/cw_core/lib/root_dir.dart @@ -0,0 +1,26 @@ +import 'dart:io'; +import 'package:path_provider/path_provider.dart'; + +String? _rootDirPath; + +void setRootDirFromEnv() + => _rootDirPath = Platform.environment['CAKE_WALLET_DIR']; + +Future getAppDir({String appName = 'cake_wallet'}) async { + Directory dir; + + if (_rootDirPath != null && _rootDirPath!.isNotEmpty) { + dir = Directory.fromUri(Uri.file(_rootDirPath!)); + dir.create(recursive: true); + } else { + dir = await getApplicationDocumentsDirectory(); + + if (Platform.isLinux) { + final appDirPath = '${dir.path}/$appName'; + dir = Directory.fromUri(Uri.file(appDirPath)); + await dir.create(recursive: true); + } + } + + return dir; +} \ No newline at end of file diff --git a/lib/core/backup_service.dart b/lib/core/backup_service.dart index c6d85947f..204266887 100644 --- a/lib/core/backup_service.dart +++ b/lib/core/backup_service.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; +import 'package:cw_core/root_dir.dart'; import 'package:cw_core/wallet_type.dart'; import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; @@ -73,7 +74,7 @@ class BackupService { Future _exportBackupV2(String password) async { final zipEncoder = ZipFileEncoder(); - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final now = DateTime.now(); final tmpDir = Directory('${appDir.path}/~_BACKUP_TMP'); final archivePath = '${tmpDir.path}/backup_${now.toString()}.zip'; @@ -114,7 +115,7 @@ class BackupService { Future _importBackupV1(Uint8List data, String password, {required String nonce}) async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final decryptedData = await _decryptV1(data, password, nonce); final zip = ZipDecoder().decodeBytes(decryptedData); @@ -137,7 +138,7 @@ class BackupService { } Future _importBackupV2(Uint8List data, String password) async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final decryptedData = await _decryptV2(data, password); final zip = ZipDecoder().decodeBytes(decryptedData); @@ -172,7 +173,7 @@ class BackupService { } Future> _reloadHiveWalletInfoBox() async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); await Hive.close(); Hive.init(appDir.path); @@ -184,7 +185,7 @@ class BackupService { } Future _importPreferencesDump() async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final preferencesFile = File('${appDir.path}/~_preferences_dump'); if (!preferencesFile.existsSync()) { @@ -303,7 +304,7 @@ class BackupService { Future _importKeychainDumpV1(String password, {required String nonce, String keychainSalt = secrets.backupKeychainSalt}) async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final keychainDumpFile = File('${appDir.path}/~_keychain_dump'); final decryptedKeychainDumpFileData = await _decryptV1( keychainDumpFile.readAsBytesSync(), '$keychainSalt$password', nonce); @@ -332,7 +333,7 @@ class BackupService { Future _importKeychainDumpV2(String password, {String keychainSalt = secrets.backupKeychainSalt}) async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final keychainDumpFile = File('${appDir.path}/~_keychain_dump'); final decryptedKeychainDumpFileData = await _decryptV2( keychainDumpFile.readAsBytesSync(), '$keychainSalt$password'); diff --git a/lib/main.dart b/lib/main.dart index 2d8e97d1e..8a97b3cbc 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -4,6 +4,7 @@ import 'package:cake_wallet/entities/language_service.dart'; import 'package:cake_wallet/buy/order.dart'; import 'package:cake_wallet/store/yat/yat_store.dart'; import 'package:cake_wallet/utils/exception_handler.dart'; +import 'package:cw_core/root_dir.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -55,8 +56,8 @@ Future main() async { return true; }; - - final appDir = await getApplicationDocumentsDirectory(); + setRootDirFromEnv(); + final appDir = await getAppDir(); await Hive.close(); Hive.init(appDir.path); diff --git a/lib/utils/exception_handler.dart b/lib/utils/exception_handler.dart index 4cbebdb5d..a14962371 100644 --- a/lib/utils/exception_handler.dart +++ b/lib/utils/exception_handler.dart @@ -6,6 +6,7 @@ import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/main.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; +import 'package:cw_core/root_dir.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mailer/flutter_mailer.dart'; @@ -17,7 +18,7 @@ class ExceptionHandler { static const _coolDownDurationInDays = 7; static void _saveException(String? error, StackTrace? stackTrace) async { - final appDocDir = await getApplicationDocumentsDirectory(); + final appDocDir = await getAppDir(); final file = File('${appDocDir.path}/error.txt'); final exception = { @@ -38,7 +39,7 @@ class ExceptionHandler { static void _sendExceptionFile() async { try { - final appDocDir = await getApplicationDocumentsDirectory(); + final appDocDir = await getAppDir(); final file = File('${appDocDir.path}/error.txt'); diff --git a/lib/view_model/backup_view_model.dart b/lib/view_model/backup_view_model.dart index ae797d3e2..41f8f8dc5 100644 --- a/lib/view_model/backup_view_model.dart +++ b/lib/view_model/backup_view_model.dart @@ -3,6 +3,7 @@ import 'package:cake_wallet/core/backup_service.dart'; import 'package:cake_wallet/core/execution_state.dart'; import 'package:cake_wallet/entities/secret_store_key.dart'; import 'package:cake_wallet/store/secret_store.dart'; +import 'package:cw_core/root_dir.dart'; import 'package:flutter/foundation.dart'; import 'package:cake_wallet/core/secure_storage.dart'; import 'package:mobx/mobx.dart'; @@ -73,7 +74,7 @@ abstract class BackupViewModelBase with Store { } Future saveBackupFileLocally(BackupExportFile backup) async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final path = '${appDir.path}/${backup.name}'; final backupFile = File(path); await backupFile.writeAsBytes(backup.content); @@ -81,7 +82,7 @@ abstract class BackupViewModelBase with Store { } Future removeBackupFileLocally(BackupExportFile backup) async { - final appDir = await getApplicationDocumentsDirectory(); + final appDir = await getAppDir(); final path = '${appDir.path}/${backup.name}'; final backupFile = File(path); await backupFile.delete();