handle invalid local path

This commit is contained in:
2024-11-24 12:03:12 +08:00
parent 4b32165aae
commit e2aceb857d
2 changed files with 42 additions and 30 deletions

View File

@@ -175,6 +175,27 @@ class LocalManager with ChangeNotifier {
return null;
}
Future<String> findDefaultPath() async {
if (App.isAndroid) {
var external = await getExternalStorageDirectories();
if (external != null && external.isNotEmpty) {
return FilePath.join(external.first.path, 'local');
} else {
return FilePath.join(App.dataPath, 'local');
}
} else if (App.isIOS) {
var oldPath = FilePath.join(App.dataPath, 'local');
if (Directory(oldPath).existsSync() && Directory(oldPath).listSync().isNotEmpty) {
return oldPath;
} else {
var directory = await getApplicationDocumentsDirectory();
return FilePath.join(directory.path, 'local');
}
} else {
return FilePath.join(App.dataPath, 'local');
}
}
Future<void> init() async {
_db = sqlite3.open(
'${App.dataPath}/local.db',
@@ -196,29 +217,20 @@ class LocalManager with ChangeNotifier {
''');
if (File(FilePath.join(App.dataPath, 'local_path')).existsSync()) {
path = File(FilePath.join(App.dataPath, 'local_path')).readAsStringSync();
} else {
if (App.isAndroid) {
var external = await getExternalStorageDirectories();
if (external != null && external.isNotEmpty) {
path = FilePath.join(external.first.path, 'local');
} else {
path = FilePath.join(App.dataPath, 'local');
}
} else if (App.isIOS) {
var oldPath = FilePath.join(App.dataPath, 'local');
if (Directory(oldPath).existsSync() && Directory(oldPath).listSync().isNotEmpty) {
path = oldPath;
} else {
var directory = await getApplicationDocumentsDirectory();
path = FilePath.join(directory.path, 'local');
if (!Directory(path).existsSync()) {
path = await findDefaultPath();
}
} else {
path = FilePath.join(App.dataPath, 'local');
}
path = await findDefaultPath();
}
try {
if (!Directory(path).existsSync()) {
await Directory(path).create();
}
}
catch(e, s) {
Log.error("IO", "Failed to create local folder: $e", s);
}
restoreDownloadingTasks();
}

View File

@@ -32,11 +32,11 @@ class Log {
static const String? logFile = null;
static void printWarning(String text) {
print('\x1B[33m$text\x1B[0m');
debugPrint('\x1B[33m$text\x1B[0m');
}
static void printError(String text) {
print('\x1B[31m$text\x1B[0m');
debugPrint('\x1B[31m$text\x1B[0m');
}
static void addLog(LogLevel level, String title, String content) {
@@ -44,14 +44,14 @@ class Log {
content = "${content.substring(0, maxLogLength)}...";
}
if (kDebugMode) {
switch (level) {
case LogLevel.error:
printError(content);
case LogLevel.warning:
printWarning(content);
case LogLevel.info:
print(content);
if(kDebugMode) {
debugPrint(content);
}
}