-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathstack_file_system.dart
More file actions
250 lines (224 loc) · 7.25 KB
/
Copy pathstack_file_system.dart
File metadata and controls
250 lines (224 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* 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
*
*/
import 'dart:io';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import '../app_config.dart';
import 'prefs.dart';
import 'util.dart';
abstract class StackFileSystem {
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;
}
static bool get _createSubDirs =>
Util.isDesktop || AppConfig.appName == "Campfire";
static Future<Directory> applicationRootDirectory() async {
Directory appDirectory;
// todo: can merge and do same as regular linux home dir?
if (Util.isArmLinux) {
appDirectory = await getApplicationDocumentsDirectory();
appDirectory = Directory(
path.join(appDirectory.path, ".${AppConfig.appDefaultDataDirName}"),
);
} else if (Platform.isLinux) {
if (_overrideDesktopDirPath != null) {
appDirectory = Directory(_overrideDesktopDirPath!);
} else {
appDirectory = Directory(
path.join(
Platform.environment['HOME']!,
".${AppConfig.appDefaultDataDirName}",
),
);
}
} else if (Platform.isWindows) {
if (_overrideDesktopDirPath != null) {
appDirectory = Directory(_overrideDesktopDirPath!);
} else {
appDirectory = await getApplicationSupportDirectory();
}
} else if (Platform.isMacOS) {
if (_overrideDesktopDirPath != null) {
appDirectory = Directory(_overrideDesktopDirPath!);
} else {
appDirectory = await getLibraryDirectory();
appDirectory = Directory(
path.join(appDirectory.path, AppConfig.appDefaultDataDirName),
);
}
} 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 (_createSubDirs) {
final dir = Directory(path.join(root.path, "isar"));
if (!dir.existsSync()) {
await dir.create();
}
return dir;
} else {
return root;
}
}
static Future<Directory> applicationDriftDirectory() async {
final root = await applicationRootDirectory();
if (_createSubDirs) {
final dir = Directory(path.join(root.path, "drift"));
if (!dir.existsSync()) {
await dir.create();
}
return dir;
} else {
return root;
}
}
// Not used in general now. See applicationFiroCacheSQLiteDirectory()
// static Future<Directory> applicationSQLiteDirectory() async {
// final root = await applicationRootDirectory();
// if (_createSubDirs) {
// final dir = Directory("${root.path}/sqlite");
// if (!dir.existsSync()) {
// await dir.create();
// }
// return dir;
// } else {
// return root;
// }
// }
static Future<Directory> applicationTorDirectory() async {
final root = await applicationRootDirectory();
if (_createSubDirs) {
final dir = Directory(path.join(root.path, "tor"));
if (!dir.existsSync()) {
await dir.create();
}
return dir;
} else {
return root;
}
}
static Future<Directory> applicationMwebdDirectory(String network) async {
final root = await applicationRootDirectory();
final dir = Directory(path.join(root.path, "mwebd", network));
if (!dir.existsSync()) {
await dir.create(recursive: true);
}
return dir;
}
static Future<Directory> applicationFiroCacheSQLiteDirectory() async {
final root = await applicationRootDirectory();
if (_createSubDirs) {
final dir = Directory(path.join(root.path, "sqlite", "firo_cache"));
if (!dir.existsSync()) {
await dir.create(recursive: true);
}
return dir;
} else {
return root;
}
}
static Future<Directory> applicationHiveDirectory() async {
final root = await applicationRootDirectory();
if (_createSubDirs) {
final dir = Directory(path.join(root.path, "hive"));
if (!dir.existsSync()) {
await dir.create();
}
return dir;
} else {
return root;
}
}
static Future<Directory> applicationXelisDirectory() async {
final root = await applicationRootDirectory();
final dir = Directory(path.join(root.path, "xelis"));
if (!dir.existsSync()) {
await dir.create();
}
return dir;
}
static Future<Directory> applicationXelisTableDirectory() async {
final xelis = await applicationXelisDirectory();
final dir = Directory(path.join(xelis.path, "table"));
if (!dir.existsSync()) {
await dir.create();
}
return dir;
}
static Future<void> initThemesDir() async {
final root = await applicationRootDirectory();
final dir = Directory(path.join(root.path, "themes"));
if (!dir.existsSync()) {
await dir.create();
}
themesDir = dir;
}
static Directory? themesDir;
static Future<Directory> applicationLogsDirectory(Prefs prefs) async {
// if prefs logs path is set, use that
if (prefs.logsPath != null) {
final dir = Directory(prefs.logsPath!);
if (await dir.exists()) {
return dir;
}
}
final Directory logsDir;
// When a desktop override dir is set (e.g. the Flatpak XDG_DATA_HOME
// location or the -d launch flag) keep logs under that persistent root
// so they follow the data directory rather than landing in the
// application documents directory.
if (_overrideDesktopDirPath != null) {
logsDir = Directory(path.join(_overrideDesktopDirPath!, "logs"));
} else {
final appDocsDir = await getApplicationDocumentsDirectory();
const logsDirName = "${AppConfig.prefix}_Logs";
if (Platform.isIOS) {
logsDir = Directory(path.join(appDocsDir.path, "logs"));
} else if (Platform.isMacOS || Platform.isLinux || Platform.isWindows) {
// TODO check this is correct for macos
logsDir = Directory(path.join(appDocsDir.path, logsDirName));
} else if (Platform.isAndroid) {
// final dir = await wtfAndroidDocumentsPath();
// final logsDirPath = path.join(dir.path, logsDirName);
// logsDir = Directory(logsDirPath);
logsDir = Directory(path.join(appDocsDir.path, "logs"));
} else {
throw Exception("Unsupported Platform");
}
}
if (!logsDir.existsSync()) {
await logsDir.create(recursive: true);
}
return logsDir;
}
}