Add ability to set custom data application directory

This commit is contained in:
M 2023-04-13 19:48:51 -04:00
parent f52c45b167
commit 8efedbccf6
6 changed files with 46 additions and 15 deletions

View file

@ -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<String> 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<String> pathForWallet({required String name, required WalletType type}) a
.then((path) => path + '/$name');
Future<String> outdatedAndroidPathForWalletDir({required String name}) async {
final directory = await getApplicationDocumentsDirectory();
final directory = await getAppDir();
final pathDir = directory.path + '/$name';
return pathDir;

26
cw_core/lib/root_dir.dart Normal file
View file

@ -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<Directory> 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;
}

View file

@ -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<Uint8List> _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<void> _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<void> _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<Box<WalletInfo>> _reloadHiveWalletInfoBox() async {
final appDir = await getApplicationDocumentsDirectory();
final appDir = await getAppDir();
await Hive.close();
Hive.init(appDir.path);
@ -184,7 +185,7 @@ class BackupService {
}
Future<void> _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<void> _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<void> _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');

View file

@ -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<void> main() async {
return true;
};
final appDir = await getApplicationDocumentsDirectory();
setRootDirFromEnv();
final appDir = await getAppDir();
await Hive.close();
Hive.init(appDir.path);

View file

@ -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');

View file

@ -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<String> 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<void> 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();