mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-15 21:03:41 +00:00
fix: file explorer and support select file (#111)
This commit is contained in:
1 parent
34bcbd9571
commit
1e08ecd8db
3 files changed
+67
-9
No files matched your search
@@ -3,9 +3,11 @@ import 'dart:io';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:url_launcher/url_launcher.dart';
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
import 'package:path/path.dart' as path;
|
||||||
|
|
||||||
import '../../../api/api.dart';
|
import '../../../api/api.dart';
|
||||||
import '../../../api/model/task.dart';
|
import '../../../api/model/task.dart';
|
||||||
|
import '../../../util/file_explorer.dart';
|
||||||
import '../../../util/file_icon.dart';
|
import '../../../util/file_icon.dart';
|
||||||
import '../../../util/icons.dart';
|
import '../../../util/icons.dart';
|
||||||
import '../../../util/message.dart';
|
import '../../../util/message.dart';
|
||||||
@@ -53,6 +55,18 @@ class BuildTaskListView extends GetView {
|
|||||||
return task.status == Status.running;
|
return task.status == Status.running;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String buildExplorerUrl(Task task) {
|
||||||
|
if (task.meta.res.rootDir.trim().isEmpty) {
|
||||||
|
return path.join(
|
||||||
|
Util.safeDir(task.meta.opts.path),
|
||||||
|
Util.safeDir(task.meta.res.files[0].path),
|
||||||
|
task.meta.res.files[0].name);
|
||||||
|
} else {
|
||||||
|
return path.join(Util.safeDir(task.meta.opts.path),
|
||||||
|
Util.safeDir(task.meta.res.rootDir));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> showDeleteDialog(String id) {
|
Future<void> showDeleteDialog(String id) {
|
||||||
final keep = true.obs;
|
final keep = true.obs;
|
||||||
|
|
||||||
@@ -100,9 +114,7 @@ class BuildTaskListView extends GetView {
|
|||||||
list.add(IconButton(
|
list.add(IconButton(
|
||||||
icon: const Icon(Icons.folder_open),
|
icon: const Icon(Icons.folder_open),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
final file = File(Util.buildAbsPath(task.meta.opts.path,
|
await FileExplorer.openAndSelectFile(buildExplorerUrl(task));
|
||||||
task.meta.res.files[0].path, task.meta.res.files[0].name));
|
|
||||||
await launchUrl(file.parent.uri);
|
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,12 +3,11 @@ import 'dart:io';
|
|||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
class Util {
|
class Util {
|
||||||
static String buildAbsPath(String dir, String path, String name) {
|
static String safeDir(String path) {
|
||||||
return (dir == "" ? "" : "$dir/") + buildPath(path, name);
|
if (path == "." || path == "./" || path == ".\\") {
|
||||||
}
|
return "";
|
||||||
|
}
|
||||||
static String buildPath(String path, String name) {
|
return path;
|
||||||
return (path == "" ? "" : "$path/") + name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static String fmtByte(int byte) {
|
static String fmtByte(int byte) {
|
||||||
|
|||||||
Reference in new issue
Block a user