mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-17 13:53:41 +00:00
feat: support browse files on web (#283)
This commit is contained in:
14 files changed
+145
-71
No files matched your search
+4
-3
@@ -8,9 +8,10 @@ import (
|
|||||||
// only for local development
|
// only for local development
|
||||||
func main() {
|
func main() {
|
||||||
cfg := &model.StartConfig{
|
cfg := &model.StartConfig{
|
||||||
Network: "tcp",
|
Network: "tcp",
|
||||||
Address: "127.0.0.1:9999",
|
Address: "127.0.0.1:9999",
|
||||||
Storage: model.StorageBolt,
|
Storage: model.StorageBolt,
|
||||||
|
WebEnable: true,
|
||||||
}
|
}
|
||||||
cmd.Start(cfg)
|
cmd.Start(cfg)
|
||||||
}
|
}
|
||||||
@@ -57,6 +57,15 @@ func (m *FetcherMeta) SingleFilepath() string {
|
|||||||
return path.Join(m.Opts.Path, file.Path, fileName)
|
return path.Join(m.Opts.Path, file.Path, fileName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RootDirPath return the root dir path of the task file.
|
||||||
|
func (m *FetcherMeta) RootDirPath() string {
|
||||||
|
if m.Res.Name != "" {
|
||||||
|
return m.FolderPath()
|
||||||
|
} else {
|
||||||
|
return m.Opts.Path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// FetcherBuilder defines the interface for a fetcher builder.
|
// FetcherBuilder defines the interface for a fetcher builder.
|
||||||
type FetcherBuilder interface {
|
type FetcherBuilder interface {
|
||||||
// Schemes returns the schemes supported by the fetcher.
|
// Schemes returns the schemes supported by the fetcher.
|
||||||
|
|||||||
@@ -540,18 +540,7 @@ func (d *Downloader) GetTask(id string) *Task {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) GetTasks() []*Task {
|
func (d *Downloader) GetTasks() []*Task {
|
||||||
tasks := make([]*Task, len(d.tasks))
|
return d.tasks
|
||||||
for i := 0; i < len(d.tasks); i++ {
|
|
||||||
tasks[i] = d.tasks[i]
|
|
||||||
}
|
|
||||||
// sort tasks by status, order by Status(if(running,1,0)) desc, CreatedAt desc
|
|
||||||
sort.Slice(tasks, func(i, j int) bool {
|
|
||||||
if tasks[i].Status != tasks[j].Status {
|
|
||||||
return tasks[i].Status == base.DownloadStatusRunning
|
|
||||||
}
|
|
||||||
return tasks[i].CreatedAt.After(tasks[j].CreatedAt)
|
|
||||||
})
|
|
||||||
return tasks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Downloader) GetConfig() (*DownloaderStoreConfig, error) {
|
func (d *Downloader) GetConfig() (*DownloaderStoreConfig, error) {
|
||||||
|
|||||||
+41
-14
@@ -105,9 +105,10 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
r.Methods(http.MethodDelete).Path("/api/v1/extensions/{identity}").HandlerFunc(DeleteExtension)
|
r.Methods(http.MethodDelete).Path("/api/v1/extensions/{identity}").HandlerFunc(DeleteExtension)
|
||||||
r.Methods(http.MethodGet).Path("/api/v1/extensions/{identity}/update").HandlerFunc(UpdateCheckExtension)
|
r.Methods(http.MethodGet).Path("/api/v1/extensions/{identity}/update").HandlerFunc(UpdateCheckExtension)
|
||||||
r.Methods(http.MethodPost).Path("/api/v1/extensions/{identity}/update").HandlerFunc(UpdateExtension)
|
r.Methods(http.MethodPost).Path("/api/v1/extensions/{identity}/update").HandlerFunc(UpdateExtension)
|
||||||
r.PathPrefix("/fs/extensions").Handler(http.FileServer(new(extensionFileSystem)))
|
|
||||||
r.Path("/api/v1/proxy").HandlerFunc(DoProxy)
|
r.Path("/api/v1/proxy").HandlerFunc(DoProxy)
|
||||||
if startCfg.WebEnable {
|
if startCfg.WebEnable {
|
||||||
|
r.PathPrefix("/fs/tasks").Handler(http.FileServer(new(taskFileSystem)))
|
||||||
|
r.PathPrefix("/fs/extensions").Handler(http.FileServer(new(extensionFileSystem)))
|
||||||
r.PathPrefix("/").Handler(http.FileServer(http.FS(startCfg.WebFS)))
|
r.PathPrefix("/").Handler(http.FileServer(http.FS(startCfg.WebFS)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,30 +139,56 @@ func BuildServer(startCfg *model.StartConfig) (*http.Server, net.Listener, error
|
|||||||
return srv, listener, nil
|
return srv, listener, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func resolvePath(urlPath string, prefix string) (identity string, path string, err error) {
|
||||||
|
// remove prefix
|
||||||
|
clearPath := strings.TrimPrefix(urlPath, prefix)
|
||||||
|
// match extension identity, eg: /fs/extensions/identity/xxx
|
||||||
|
reg := regexp.MustCompile(`^/([^/]+)/(.*)$`)
|
||||||
|
if !reg.MatchString(clearPath) {
|
||||||
|
err = os.ErrNotExist
|
||||||
|
return
|
||||||
|
}
|
||||||
|
matched := reg.FindStringSubmatch(clearPath)
|
||||||
|
if len(matched) != 3 {
|
||||||
|
err = os.ErrNotExist
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return matched[1], matched[2], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle task file resource
|
||||||
|
type taskFileSystem struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *taskFileSystem) Open(name string) (http.File, error) {
|
||||||
|
// get extension identity
|
||||||
|
identity, path, err := resolvePath(name, "/fs/tasks")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
task := Downloader.GetTask(identity)
|
||||||
|
if task == nil {
|
||||||
|
return nil, os.ErrNotExist
|
||||||
|
}
|
||||||
|
return os.Open(filepath.Join(task.Meta.RootDirPath(), path))
|
||||||
|
}
|
||||||
|
|
||||||
// handle extension file resource
|
// handle extension file resource
|
||||||
type extensionFileSystem struct {
|
type extensionFileSystem struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *extensionFileSystem) Open(name string) (http.File, error) {
|
func (e *extensionFileSystem) Open(name string) (http.File, error) {
|
||||||
// remove prefix
|
|
||||||
path := strings.TrimPrefix(name, "/fs/extensions")
|
|
||||||
// match extension identity, eg: /fs/extensions/identity/xxx
|
|
||||||
reg := regexp.MustCompile(`^/([^/]+)/(.*)$`)
|
|
||||||
if !reg.MatchString(path) {
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
|
||||||
matched := reg.FindStringSubmatch(path)
|
|
||||||
if len(matched) != 3 {
|
|
||||||
return nil, os.ErrNotExist
|
|
||||||
}
|
|
||||||
// get extension identity
|
// get extension identity
|
||||||
identity := matched[1]
|
identity, path, err := resolvePath(name, "/fs/extensions")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
extension, err := Downloader.GetExtension(identity)
|
extension, err := Downloader.GetExtension(identity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, os.ErrNotExist
|
return nil, os.ErrNotExist
|
||||||
}
|
}
|
||||||
extensionPath := Downloader.ExtensionPath(extension)
|
extensionPath := Downloader.ExtensionPath(extension)
|
||||||
return os.Open(filepath.Join(extensionPath, matched[2]))
|
return os.Open(filepath.Join(extensionPath, path))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadJson(r *http.Request, w http.ResponseWriter, v any) bool {
|
func ReadJson(r *http.Request, w http.ResponseWriter, v any) bool {
|
||||||
|
|||||||
@@ -491,6 +491,7 @@ func doTest(handler func()) {
|
|||||||
cfg.Init()
|
cfg.Init()
|
||||||
cfg.Storage = storage
|
cfg.Storage = storage
|
||||||
cfg.StorageDir = ".test_storage"
|
cfg.StorageDir = ".test_storage"
|
||||||
|
cfg.WebEnable = true
|
||||||
fileListener := doStart(cfg)
|
fileListener := doStart(cfg)
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := fileListener.Close(); err != nil {
|
if err := fileListener.Close(); err != nil {
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
|
|
||||||
import 'package:gopeed/app/modules/task/controllers/task_list_controller.dart';
|
import 'package:gopeed/app/modules/task/controllers/task_list_controller.dart';
|
||||||
|
|
||||||
import '../../../../api/model/task.dart';
|
import '../../../../api/model/task.dart';
|
||||||
|
|
||||||
class TaskDownloadedController extends TaskListController {
|
class TaskDownloadedController extends TaskListController {
|
||||||
TaskDownloadedController() : super([Status.done]);
|
TaskDownloadedController() : super([Status.done], SortDirection.asc);
|
||||||
}
|
}
|
||||||
@@ -9,5 +9,5 @@ class TaskDownloadingController extends TaskListController {
|
|||||||
Status.pause,
|
Status.pause,
|
||||||
Status.wait,
|
Status.wait,
|
||||||
Status.error
|
Status.error
|
||||||
]);
|
], SortDirection.desc);
|
||||||
}
|
}
|
||||||
@@ -5,10 +5,13 @@ import 'package:get/get.dart';
|
|||||||
import '../../../../api/api.dart';
|
import '../../../../api/api.dart';
|
||||||
import '../../../../api/model/task.dart';
|
import '../../../../api/model/task.dart';
|
||||||
|
|
||||||
|
enum SortDirection { asc, desc }
|
||||||
|
|
||||||
abstract class TaskListController extends GetxController {
|
abstract class TaskListController extends GetxController {
|
||||||
List<Status> statuses;
|
List<Status> statuses;
|
||||||
|
SortDirection sortDirection;
|
||||||
|
|
||||||
TaskListController(this.statuses);
|
TaskListController(this.statuses, this.sortDirection);
|
||||||
|
|
||||||
final tasks = <Task>[].obs;
|
final tasks = <Task>[].obs;
|
||||||
final isRunning = false.obs;
|
final isRunning = false.obs;
|
||||||
@@ -43,6 +46,15 @@ abstract class TaskListController extends GetxController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getTasksState() async {
|
getTasksState() async {
|
||||||
tasks.value = await getTasks(statuses);
|
final tasks = await getTasks(statuses);
|
||||||
|
// sort tasks by create time
|
||||||
|
tasks.sort((a, b) {
|
||||||
|
if (sortDirection == SortDirection.asc) {
|
||||||
|
return a.createdAt.compareTo(b.createdAt);
|
||||||
|
} else {
|
||||||
|
return b.createdAt.compareTo(a.createdAt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.tasks.value = tasks;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,10 @@ import 'package:get/get.dart';
|
|||||||
import 'package:open_file/open_file.dart';
|
import 'package:open_file/open_file.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart';
|
||||||
import 'package:share_plus/share_plus.dart';
|
import 'package:share_plus/share_plus.dart';
|
||||||
|
import 'package:url_launcher/url_launcher.dart';
|
||||||
|
|
||||||
|
import '../../../../api/api.dart' as api;
|
||||||
|
import '../../../../util/browser_download/browser_download.dart';
|
||||||
import '../../../../util/file_icon.dart';
|
import '../../../../util/file_icon.dart';
|
||||||
import '../../../../util/icons.dart';
|
import '../../../../util/icons.dart';
|
||||||
import '../../../../util/util.dart';
|
import '../../../../util/util.dart';
|
||||||
@@ -21,7 +24,7 @@ class TaskFilesView extends GetView<TaskFilesController> {
|
|||||||
icon: const Icon(Icons.arrow_back),
|
icon: const Icon(Icons.arrow_back),
|
||||||
onPressed: () => Get.rootDelegate.popRoute()),
|
onPressed: () => Get.rootDelegate.popRoute()),
|
||||||
// actions: [],
|
// actions: [],
|
||||||
title: Obx(() => Text(controller.task.value!.meta.res!.name)),
|
title: Obx(() => Text(controller.task.value?.meta.res?.name ?? "")),
|
||||||
),
|
),
|
||||||
body: Obx(() {
|
body: Obx(() {
|
||||||
final fileList = controller.fileList;
|
final fileList = controller.fileList;
|
||||||
@@ -53,11 +56,12 @@ class TaskFilesView extends GetView<TaskFilesController> {
|
|||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final meta = controller.task.value!.meta;
|
final meta = controller.task.value!.meta;
|
||||||
final file = fileList[index];
|
final file = fileList[index];
|
||||||
final filePath = Util.safePathJoin([
|
// if resource is single file, use opts.name as file name
|
||||||
meta.opts.path,
|
final realFileName =
|
||||||
meta.res!.name,
|
meta.res!.name.isEmpty ? file.name : "";
|
||||||
file.filePath(meta.opts.name)
|
final fileRelativePath = file.filePath(realFileName);
|
||||||
]);
|
final filePath = Util.safePathJoin(
|
||||||
|
[meta.opts.path, meta.res!.name, fileRelativePath]);
|
||||||
final fileName = basename(filePath);
|
final fileName = basename(filePath);
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: file.isDirectory
|
leading: file.isDirectory
|
||||||
@@ -77,19 +81,39 @@ class TaskFilesView extends GetView<TaskFilesController> {
|
|||||||
width: 100,
|
width: 100,
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.end,
|
mainAxisAlignment: MainAxisAlignment.end,
|
||||||
children: [
|
children: Util.isWeb()
|
||||||
IconButton(
|
? () {
|
||||||
icon: const Icon(Icons.play_circle),
|
final accessUrl = api.join(
|
||||||
onPressed: () {
|
"/fs/tasks/${controller.task.value!.id}$fileRelativePath");
|
||||||
OpenFile.open(filePath);
|
return [
|
||||||
}),
|
IconButton(
|
||||||
IconButton(
|
icon:
|
||||||
icon: const Icon(Icons.share),
|
const Icon(Icons.play_circle),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
final xfile = XFile(filePath);
|
launchUrl(Uri.parse(accessUrl),
|
||||||
Share.shareXFiles([xfile]);
|
webOnlyWindowName:
|
||||||
})
|
"_blank");
|
||||||
],
|
}),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.download),
|
||||||
|
onPressed: () {
|
||||||
|
download(accessUrl, fileName);
|
||||||
|
})
|
||||||
|
];
|
||||||
|
}()
|
||||||
|
: [
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.play_circle),
|
||||||
|
onPressed: () {
|
||||||
|
OpenFile.open(filePath);
|
||||||
|
}),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.share),
|
||||||
|
onPressed: () {
|
||||||
|
final xfile = XFile(filePath);
|
||||||
|
Share.shareXFiles([xfile]);
|
||||||
|
})
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:gopeed/app/modules/task/controllers/task_downloading_controller.dart';
|
|
||||||
import '../controllers/task_downloaded_controller.dart';
|
|
||||||
import 'task_downloading_view.dart';
|
|
||||||
|
|
||||||
import 'task_downloaded_view.dart';
|
|
||||||
import '../controllers/task_controller.dart';
|
import '../controllers/task_controller.dart';
|
||||||
|
import '../controllers/task_downloaded_controller.dart';
|
||||||
|
import '../controllers/task_downloading_controller.dart';
|
||||||
|
import 'task_downloaded_view.dart';
|
||||||
|
import 'task_downloading_view.dart';
|
||||||
|
|
||||||
class TaskView extends GetView<TaskController> {
|
class TaskView extends GetView<TaskController> {
|
||||||
const TaskView({Key? key}) : super(key: key);
|
const TaskView({Key? key}) : super(key: key);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:gopeed/api/model/meta.dart';
|
|
||||||
import 'package:path/path.dart' as path;
|
import 'package:path/path.dart' as path;
|
||||||
import 'package:styled_widget/styled_widget.dart';
|
import 'package:styled_widget/styled_widget.dart';
|
||||||
|
|
||||||
import '../../api/api.dart';
|
import '../../api/api.dart';
|
||||||
|
import '../../api/model/meta.dart';
|
||||||
import '../../api/model/task.dart';
|
import '../../api/model/task.dart';
|
||||||
import '../../util/file_explorer.dart';
|
import '../../util/file_explorer.dart';
|
||||||
import '../../util/file_icon.dart';
|
import '../../util/file_icon.dart';
|
||||||
@@ -110,19 +110,17 @@ class BuildTaskListView extends GetView {
|
|||||||
List<Widget> buildActions() {
|
List<Widget> buildActions() {
|
||||||
final list = <Widget>[];
|
final list = <Widget>[];
|
||||||
if (isDone()) {
|
if (isDone()) {
|
||||||
if (Util.isDesktop() || Util.isMobile()) {
|
list.add(IconButton(
|
||||||
list.add(IconButton(
|
icon: const Icon(Icons.folder_open),
|
||||||
icon: const Icon(Icons.folder_open),
|
onPressed: () {
|
||||||
onPressed: () {
|
if (Util.isDesktop()) {
|
||||||
if (Util.isDesktop()) {
|
FileExplorer.openAndSelectFile(buildExplorerUrl(task));
|
||||||
FileExplorer.openAndSelectFile(buildExplorerUrl(task));
|
} else {
|
||||||
} else {
|
Get.rootDelegate
|
||||||
Get.rootDelegate
|
.toNamed(Routes.TASK_FILES, parameters: {'id': task.id});
|
||||||
.toNamed(Routes.TASK_FILES, parameters: {'id': task.id});
|
}
|
||||||
}
|
},
|
||||||
},
|
));
|
||||||
));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (isRunning()) {
|
if (isRunning()) {
|
||||||
list.add(IconButton(
|
list.add(IconButton(
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import 'browser_download_stub.dart'
|
||||||
|
if (dart.library.html) 'entry/browser_download_browser.dart';
|
||||||
|
|
||||||
|
void download(String url, String name) => doDownload(url, name);
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
void doDownload(String url, String name) => throw UnimplementedError();
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
// ignore: avoid_web_libraries_in_flutter
|
||||||
|
import 'dart:html' as html;
|
||||||
|
|
||||||
|
void doDownload(String url, String name) {
|
||||||
|
final anchorElement = html.AnchorElement(href: url);
|
||||||
|
anchorElement.download = name;
|
||||||
|
anchorElement.target = '_blank';
|
||||||
|
anchorElement.click();
|
||||||
|
}
|
||||||
Reference in new issue
Block a user