Change directory management
This commit is contained in:
@@ -3,82 +3,65 @@ import 'dart:io';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:hiddify/domain/constants.dart';
|
||||
import 'package:hiddify/gen/assets.gen.dart';
|
||||
import 'package:hiddify/services/platform_services.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
typedef Directories = ({
|
||||
Directory baseDir,
|
||||
Directory workingDir,
|
||||
Directory tempDir
|
||||
});
|
||||
|
||||
class FilesEditorService with InfraLogger {
|
||||
FilesEditorService(this.platformServices);
|
||||
|
||||
late final _methodChannel = const MethodChannel("com.hiddify.app/files.method");
|
||||
final PlatformServices platformServices;
|
||||
|
||||
late final Directory baseDir;
|
||||
late final Directory workingDir;
|
||||
late final Directory tempDir;
|
||||
late final Directory logsDir;
|
||||
late final Directory _configsDir;
|
||||
late final Directories dirs;
|
||||
|
||||
Future<Map<String, String>?> getPaths() async {
|
||||
try {
|
||||
final Map<dynamic, dynamic>? directoryMap = await _methodChannel.invokeMethod('get_paths');
|
||||
return directoryMap?.cast<String, String>();
|
||||
} on PlatformException catch (e) {
|
||||
// print("Failed to get shared directory: '${e.message}'.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Directory get workingDir => dirs.workingDir;
|
||||
Directory get configsDir =>
|
||||
Directory(p.join(workingDir.path, Constants.configsFolderName));
|
||||
Directory get logsDir => dirs.workingDir;
|
||||
|
||||
File get appLogsFile => File(p.join(logsDir.path, "app.log"));
|
||||
File get coreLogsFile => File(p.join(logsDir.path, "box.log"));
|
||||
|
||||
Future<void> init() async {
|
||||
if (Platform.isIOS) {
|
||||
final paths = await getPaths();
|
||||
baseDir = Directory(paths!["base"]!);
|
||||
workingDir = Directory(paths["working"]!);
|
||||
tempDir = Directory(paths["temp"]!);
|
||||
dirs = await platformServices.getPaths().getOrElse(
|
||||
(error) {
|
||||
loggy.error("error getting paths", error, StackTrace.current);
|
||||
throw error;
|
||||
},
|
||||
).run();
|
||||
|
||||
loggy.info("directories: $dirs");
|
||||
|
||||
if (!await dirs.baseDir.exists()) {
|
||||
await dirs.baseDir.create(recursive: true);
|
||||
}
|
||||
if (!await dirs.workingDir.exists()) {
|
||||
await dirs.workingDir.create(recursive: true);
|
||||
}
|
||||
if (!await configsDir.exists()) {
|
||||
await configsDir.create(recursive: true);
|
||||
}
|
||||
|
||||
if (await appLogsFile.exists()) {
|
||||
await appLogsFile.writeAsString("");
|
||||
} else {
|
||||
baseDir = await getApplicationSupportDirectory();
|
||||
if (Platform.isAndroid) {
|
||||
final externalDir = await getExternalStorageDirectory();
|
||||
workingDir = externalDir!;
|
||||
} else if (Platform.isWindows || Platform.isLinux || Platform.isMacOS) {
|
||||
workingDir = baseDir;
|
||||
} else {
|
||||
workingDir = await getApplicationDocumentsDirectory();
|
||||
}
|
||||
tempDir = await getTemporaryDirectory();
|
||||
}
|
||||
logsDir = workingDir;
|
||||
|
||||
loggy.debug("base dir: ${baseDir.path}");
|
||||
loggy.debug("working dir: ${workingDir.path}");
|
||||
loggy.debug("temp dir: ${tempDir.path}");
|
||||
loggy.debug("logs dire: ${logsDir.path}");
|
||||
|
||||
_configsDir =
|
||||
Directory(p.join(workingDir.path, Constants.configsFolderName));
|
||||
if (!await baseDir.exists()) {
|
||||
await baseDir.create(recursive: true);
|
||||
}
|
||||
if (!await workingDir.exists()) {
|
||||
await workingDir.create(recursive: true);
|
||||
}
|
||||
if (!await _configsDir.exists()) {
|
||||
await _configsDir.create(recursive: true);
|
||||
await appLogsFile.create(recursive: true);
|
||||
}
|
||||
|
||||
final appLogFile = File(appLogsPath);
|
||||
if (await appLogFile.exists()) {
|
||||
await appLogFile.writeAsString("");
|
||||
if (await coreLogsFile.exists()) {
|
||||
await coreLogsFile.writeAsString("");
|
||||
} else {
|
||||
await appLogFile.create(recursive: true);
|
||||
await coreLogsFile.create(recursive: true);
|
||||
}
|
||||
|
||||
await _populateGeoAssets();
|
||||
|
||||
final coreLogFile = File(coreLogsPath);
|
||||
if (await coreLogFile.exists()) {
|
||||
await coreLogFile.writeAsString("");
|
||||
} else {
|
||||
await coreLogFile.create(recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
static Future<Directory> getDatabaseDirectory() async {
|
||||
@@ -90,11 +73,8 @@ class FilesEditorService with InfraLogger {
|
||||
return getApplicationDocumentsDirectory();
|
||||
}
|
||||
|
||||
String get appLogsPath => p.join(logsDir.path, "app.log");
|
||||
String get coreLogsPath => p.join(logsDir.path, "box.log");
|
||||
|
||||
String configPath(String fileName) {
|
||||
return p.join(_configsDir.path, "$fileName.json");
|
||||
return p.join(configsDir.path, "$fileName.json");
|
||||
}
|
||||
|
||||
String tempConfigPath(String fileName) => configPath("temp_$fileName");
|
||||
|
||||
@@ -1,16 +1,48 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hiddify/services/files_editor_service.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
part 'platform_settings.freezed.dart';
|
||||
part 'platform_settings.g.dart';
|
||||
part 'platform_services.freezed.dart';
|
||||
part 'platform_services.g.dart';
|
||||
|
||||
class PlatformSettings with InfraLogger {
|
||||
late final MethodChannel _methodChannel =
|
||||
const MethodChannel("com.hiddify.app/platform.settings");
|
||||
class PlatformServices with InfraLogger {
|
||||
final _methodChannel = const MethodChannel("app.hiddify.com/platform");
|
||||
|
||||
TaskEither<String, Directories> getPaths() {
|
||||
return TaskEither(
|
||||
() async {
|
||||
loggy.debug("getting paths");
|
||||
final Directories dirs;
|
||||
if (Platform.isIOS) {
|
||||
final paths = await _methodChannel.invokeMethod<Map>("get_paths");
|
||||
loggy.debug("paths: $paths");
|
||||
dirs = (
|
||||
baseDir: Directory(paths?["base"]! as String),
|
||||
workingDir: Directory(paths?["working"]! as String),
|
||||
tempDir: Directory(paths?["temp"]! as String),
|
||||
);
|
||||
} else {
|
||||
final baseDir = await getApplicationSupportDirectory();
|
||||
final workingDir = Platform.isAndroid
|
||||
? await getExternalStorageDirectory()
|
||||
: baseDir;
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
dirs = (
|
||||
baseDir: baseDir,
|
||||
workingDir: workingDir!,
|
||||
tempDir: tempDir,
|
||||
);
|
||||
}
|
||||
return right(dirs);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
TaskEither<String, bool> isIgnoringBatteryOptimizations() {
|
||||
return TaskEither(
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'package:hiddify/data/data_providers.dart';
|
||||
import 'package:hiddify/services/cron_service.dart';
|
||||
import 'package:hiddify/services/files_editor_service.dart';
|
||||
import 'package:hiddify/services/platform_settings.dart';
|
||||
import 'package:hiddify/services/platform_services.dart';
|
||||
import 'package:hiddify/services/singbox/singbox_service.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
@@ -9,14 +9,14 @@ part 'service_providers.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
FilesEditorService filesEditorService(FilesEditorServiceRef ref) =>
|
||||
FilesEditorService();
|
||||
FilesEditorService(ref.watch(platformServicesProvider));
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
SingboxService singboxService(SingboxServiceRef ref) => SingboxService();
|
||||
|
||||
@riverpod
|
||||
PlatformSettings platformSettings(PlatformSettingsRef ref) =>
|
||||
PlatformSettings();
|
||||
@Riverpod(keepAlive: true)
|
||||
PlatformServices platformServices(PlatformServicesRef ref) =>
|
||||
PlatformServices();
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
CronService cronService(CronServiceRef ref) {
|
||||
|
||||
Reference in New Issue
Block a user