fix: open files on linux (#765)

This commit is contained in:
Ayase authored and GitHub committed 2024-10-03 21:40:49 +08:00
1 parent 5ad15cc4ac
commit 1d86d14a20
1 file changed
+22 -18
+22 -18
View File
@@ -21,29 +21,33 @@ class FileExplorer {
} else if (Platform.isMacOS) { } else if (Platform.isMacOS) {
Process.run('open', ['-R', filePath]); Process.run('open', ['-R', filePath]);
} else if (Platform.isLinux) { } else if (Platform.isLinux) {
if (await _isUbuntuOrDebian()) { _linuxOpen(filePath);
Process.run('xdg-open', [filePath]); }
} else if (await _isCentOS()) { }
Process.run('nautilus', ['--select', filePath]);
static Future<void> _linuxOpen(String filePath) async {
if (await Process.run('which', ['xdg-open'])
.then((value) => value.exitCode == 0)) {
final result = await Process.run('xdg-open', [filePath]);
if (result.exitCode != 0) {
_openWithFileManager(filePath);
} }
} else {
_openWithFileManager(filePath);
} }
} }
static Future<bool> _isUbuntuOrDebian() async { static Future<void> _openWithFileManager(String filePath) async {
final result = await Process.run('lsb_release', ['-i']); final desktop = Platform.environment['XDG_CURRENT_DESKTOP'];
if (result.exitCode != 0) { if (desktop == null) {
return false; throw Exception('XDG_CURRENT_DESKTOP is not set');
} }
final output = result.stdout.toString().toLowerCase(); if (desktop == 'GNOME') {
return output.contains('ubuntu') || output.contains('debian'); await Process.run('nautilus', ['--select', filePath]);
} } else if (desktop == 'KDE') {
await Process.run('dolphin', ['--select', filePath]);
static Future<bool> _isCentOS() async { } else {
final result = await Process.run('cat', ['/etc/os-release']); throw Exception('Unsupported desktop environment');
if (result.exitCode != 0) {
return false;
} }
final output = result.stdout.toString().toLowerCase();
return output.contains('centos');
} }
} }