stack_wallet/lib/utilities/stack_file_system.dart

132 lines
3.6 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-11-12 22:04:16 +00:00
import 'dart:io';
import 'package:path_provider/path_provider.dart';
2024-05-23 15:20:11 +00:00
import '../app_config.dart';
import 'logger.dart';
import 'util.dart';
2022-11-12 22:04:16 +00:00
abstract class StackFileSystem {
2024-05-23 15:20:11 +00:00
static String? _overrideDesktopDirPath;
static bool _overrideDirSet = false;
static void setDesktopOverrideDir(String dirPath) {
if (_overrideDirSet) {
throw Exception(
"Attempted to change StackFileSystem._overrideDir unexpectedly",
);
}
_overrideDesktopDirPath = dirPath;
_overrideDirSet = true;
}
2022-11-12 22:04:16 +00:00
static Future<Directory> applicationRootDirectory() async {
Directory appDirectory;
// todo: can merge and do same as regular linux home dir?
if (Logging.isArmLinux) {
appDirectory = await getApplicationDocumentsDirectory();
2024-05-23 15:20:11 +00:00
appDirectory =
Directory("${appDirectory.path}/.${AppConfig.appDefaultDataDirName}");
2022-11-12 22:04:16 +00:00
} else if (Platform.isLinux) {
2024-05-23 15:20:11 +00:00
if (_overrideDesktopDirPath != null) {
appDirectory = Directory(_overrideDesktopDirPath!);
} else {
2024-05-23 15:20:11 +00:00
appDirectory = Directory(
2024-05-27 23:56:22 +00:00
"${Platform.environment['HOME']}/.${AppConfig.appDefaultDataDirName}",
);
}
2022-11-12 22:04:16 +00:00
} else if (Platform.isWindows) {
2024-05-23 15:20:11 +00:00
if (_overrideDesktopDirPath != null) {
appDirectory = Directory(_overrideDesktopDirPath!);
} else {
appDirectory = await getApplicationSupportDirectory();
}
2022-11-12 22:04:16 +00:00
} else if (Platform.isMacOS) {
2024-05-23 15:20:11 +00:00
if (_overrideDesktopDirPath != null) {
appDirectory = Directory(_overrideDesktopDirPath!);
} else {
appDirectory = await getLibraryDirectory();
2024-05-23 15:20:11 +00:00
appDirectory = Directory(
2024-05-27 23:56:22 +00:00
"${appDirectory.path}/${AppConfig.appDefaultDataDirName}",
);
}
2022-11-12 22:04:16 +00:00
} 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;
}
}
2023-08-07 16:39:04 +00:00
static Future<Directory> applicationTorDirectory() async {
final root = await applicationRootDirectory();
if (Util.isDesktop) {
final dir = Directory("${root.path}/tor");
if (!dir.existsSync()) {
await dir.create();
}
return dir;
} else {
return root;
}
}
2022-11-12 22:04:16 +00:00
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;
}
}
2023-07-04 00:25:18 +00:00
static Future<void> initThemesDir() async {
final root = await applicationRootDirectory();
2023-07-04 00:25:18 +00:00
final dir = Directory("${root.path}/themes");
if (!dir.existsSync()) {
await dir.create();
}
2023-07-04 00:25:18 +00:00
themesDir = dir;
}
2023-07-04 00:25:18 +00:00
static Directory? themesDir;
2022-11-12 22:04:16 +00:00
}