-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathpath.dart
More file actions
171 lines (141 loc) · 4.58 KB
/
path.dart
File metadata and controls
171 lines (141 loc) · 4.58 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
import 'dart:async';
import 'dart:io';
import 'package:fl_clash/common/common.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
class AppPath {
static AppPath? _instance;
Completer<Directory> dataDir = Completer();
Completer<Directory> downloadDir = Completer();
Completer<Directory> tempDir = Completer();
Completer<Directory> cacheDir = Completer();
late String appDirPath;
bool isPortable = false;
AppPath._internal() {
appDirPath = join(dirname(Platform.resolvedExecutable));
_initDataDir();
getTemporaryDirectory().then((value) {
tempDir.complete(value);
});
getDownloadsDirectory().then((value) {
downloadDir.complete(value);
});
getApplicationCacheDirectory().then((value) {
cacheDir.complete(value);
});
}
bool get _isDesktop =>
Platform.isWindows || Platform.isMacOS || Platform.isLinux;
Future<void> _initDataDir() async {
// Portable mode: if a 'config' directory exists next to the executable on
// desktop platforms, use it as the data directory instead of the
// platform-specific application support directory.
if (_isDesktop) {
final portableConfigDir = Directory(join(appDirPath, 'config'));
if (await portableConfigDir.exists()) {
isPortable = true;
dataDir.complete(portableConfigDir);
return;
}
}
final dir = await getApplicationSupportDirectory();
dataDir.complete(dir);
}
factory AppPath() {
_instance ??= AppPath._internal();
return _instance!;
}
String get executableExtension {
return system.isWindows ? '.exe' : '';
}
String get executableDirPath {
final currentExecutablePath = Platform.resolvedExecutable;
return dirname(currentExecutablePath);
}
String get corePath {
return join(executableDirPath, 'FlClashCore$executableExtension');
}
String get helperPath {
return join(executableDirPath, '$appHelperService$executableExtension');
}
Future<String> get downloadDirPath async {
final directory = await downloadDir.future;
return directory.path;
}
Future<String> get homeDirPath async {
final directory = await dataDir.future;
return directory.path;
}
Future<String> get databasePath async {
final mHomeDirPath = await homeDirPath;
return join(mHomeDirPath, 'database.sqlite');
}
Future<String> get backupFilePath async {
final mHomeDirPath = await homeDirPath;
return join(mHomeDirPath, 'backup.zip');
}
Future<String> get restoreDirPath async {
final mHomeDirPath = await homeDirPath;
return join(mHomeDirPath, 'restore');
}
Future<String> get tempFilePath async {
final mTempDir = await tempDir.future;
return join(mTempDir.path, 'temp${utils.id}');
}
Future<String> get lockFilePath async {
final homeDirPath = await appPath.homeDirPath;
return join(homeDirPath, 'FlClash.lock');
}
Future<String> get configFilePath async {
final mHomeDirPath = await homeDirPath;
return join(mHomeDirPath, 'config.yaml');
}
Future<String> get sharedFilePath async {
final mHomeDirPath = await homeDirPath;
return join(mHomeDirPath, 'shared.json');
}
Future<String> get sharedPreferencesPath async {
final directory = await dataDir.future;
return join(directory.path, 'shared_preferences.json');
}
Future<String> get profilesPath async {
final directory = await dataDir.future;
return join(directory.path, profilesDirectoryName);
}
Future<String> getProfilePath(String fileName) async {
return join(await profilesPath, '$fileName.yaml');
}
Future<String> get scriptsDirPath async {
final path = await homeDirPath;
return join(path, 'scripts');
}
Future<String> getScriptPath(String fileName) async {
final path = await scriptsDirPath;
return join(path, '$fileName.js');
}
Future<String> getIconsCacheDir() async {
final directory = await cacheDir.future;
return join(directory.path, 'icons');
}
Future<String> getProvidersRootPath() async {
final directory = await profilesPath;
return join(directory, 'providers');
}
Future<String> getProvidersDirPath(String id) async {
final directory = await profilesPath;
return join(directory, 'providers', id);
}
Future<String> getProvidersFilePath(
String id,
String type,
String url,
) async {
final directory = await profilesPath;
return join(directory, 'providers', id, type, url.toMd5());
}
Future<String> get tempPath async {
final directory = await tempDir.future;
return directory.path;
}
}
final appPath = AppPath();