fix: file explorer and support select file (#111)

This commit is contained in:
Li Wei authored and GitHub committed 2023-05-04 14:32:37 +08:00
1 parent 34bcbd9571
commit 1e08ecd8db
3 files changed
+67 -9

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import 'dart:io';
class FileExplorer {
static Future<void> openAndSelectFile(String filePath) async {
if (!await FileSystemEntity.isDirectory(filePath) &&
!await FileSystemEntity.isFile(filePath)) {
return;
}
if (await FileSystemEntity.isDirectory(filePath)) {
if (Platform.isWindows) {
Process.run('explorer.exe', [filePath]);
} else if (Platform.isMacOS || Platform.isLinux) {
Process.run('xdg-open', [filePath]);
}
} else if (await FileSystemEntity.isFile(filePath)) {
if (Platform.isWindows) {
Process.run('explorer.exe', ['/select,', filePath]);
} else if (Platform.isMacOS) {
Process.run('open', ['-R', filePath]);
} else if (Platform.isLinux) {
if (await _isUbuntuOrDebian()) {
Process.run('xdg-open', [filePath]);
} else if (await _isCentOS()) {
Process.run('nautilus', ['--select', filePath]);
}
}
}
}
static Future<bool> _isUbuntuOrDebian() async {
final result = await Process.run('lsb_release', ['-i']);
if (result.exitCode != 0) {
return false;
}
final output = result.stdout.toString().toLowerCase();
return output.contains('ubuntu') || output.contains('debian');
}
static Future<bool> _isCentOS() async {
final result = await Process.run('cat', ['/etc/os-release']);
if (result.exitCode != 0) {
return false;
}
final output = result.stdout.toString().toLowerCase();
return output.contains('centos');
}
}
+5 -6
View File
@@ -3,12 +3,11 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
class Util {
static String buildAbsPath(String dir, String path, String name) {
return (dir == "" ? "" : "$dir/") + buildPath(path, name);
}
static String buildPath(String path, String name) {
return (path == "" ? "" : "$path/") + name;
static String safeDir(String path) {
if (path == "." || path == "./" || path == ".\\") {
return "";
}
return path;
}
static String fmtByte(int byte) {