fix: incorrect storage path in linux (#270)

This commit is contained in:
Levi authored and GitHub committed 2023-11-15 16:26:29 +08:00
1 parent 485656d1d4
commit 0a05bf41e7
9 files changed
+164 -85

No files matched your search

+13 -7
View File
@@ -1,18 +1,24 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';
import 'package:path/path.dart' as path;
import 'util.dart';
final logger = Logger(
filter: ProductionFilter(),
printer: SimplePrinter(printTime: true, colors: false),
output: buildOutput(),
);
late final Logger logger;
buildOutput() {
initLogger() {
// if is debug mode, don't log to file
logger = Logger(
filter: ProductionFilter(),
printer: SimplePrinter(printTime: true, colors: false),
output: _buildOutput(),
);
}
_buildOutput() {
// if is debug mode, don't log to file
if (!kDebugMode && Util.isDesktop()) {
const logDirPath = 'logs';
final logDirPath = path.join(Util.getStorageDir(), 'logs');
var logDir = Directory(logDirPath);
if (!logDir.existsSync()) {
logDir.createSync();
+12 -4
View File
@@ -6,6 +6,8 @@ import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as path;
import 'log_util.dart';
class Util {
static String? _storageDir;
@@ -55,6 +57,16 @@ class Util {
storageDir = (await getExternalStorageDirectory())?.path ?? storageDir;
} else if (Platform.isIOS) {
storageDir = (await getLibraryDirectory()).path;
} else if (Util.isLinux()) {
storageDir = File(Platform.resolvedExecutable).parent.path;
// check has write permission, if not, fallback to application support dir
try {
final testFile = File(path.join(storageDir, ".test"));
await testFile.writeAsString("test");
await testFile.delete();
} catch (e) {
storageDir = (await getApplicationSupportDirectory()).path;
}
}
_storageDir = storageDir;
}
@@ -63,10 +75,6 @@ class Util {
return _storageDir!;
}
static String getStorageAsset(String asset) {
return path.join(getStorageDir(), asset);
}
static isAndroid() {
return !kIsWeb && Platform.isAndroid;
}