feat(url-tree): support url tree driver writing (#7779 close #5166)

* feat: support url tree writing

* fix: meta writable

* feat: disable writable via addition
This commit is contained in:
KirCute_ECT
2025-01-10 20:50:56 +08:00
committed by GitHub
parent 25b4b55ee1
commit 51bcf83511
8 changed files with 338 additions and 15 deletions

View File

@ -2,8 +2,11 @@ package tool
import (
"context"
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/internal/task"
"net/url"
"path"
"path/filepath"
"github.com/alist-org/alist/v3/internal/conf"
@ -30,18 +33,6 @@ type AddURLArgs struct {
}
func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, error) {
// get tool
tool, err := Tools.Get(args.Tool)
if err != nil {
return nil, errors.Wrapf(err, "failed get tool")
}
// check tool is ready
if !tool.IsReady() {
// try to init tool
if _, err := tool.Init(); err != nil {
return nil, errors.Wrapf(err, "failed init tool %s", args.Tool)
}
}
// check storage
storage, dstDirActualPath, err := op.GetStorageAndActualPath(args.DstDirPath)
if err != nil {
@ -63,6 +54,23 @@ func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, erro
return nil, errors.WithStack(errs.NotFolder)
}
}
// try putting url
if args.Tool == "SimpleHttp" && tryPutUrl(ctx, storage, dstDirActualPath, args.URL) {
return nil, nil
}
// get tool
tool, err := Tools.Get(args.Tool)
if err != nil {
return nil, errors.Wrapf(err, "failed get tool")
}
// check tool is ready
if !tool.IsReady() {
// try to init tool
if _, err := tool.Init(); err != nil {
return nil, errors.Wrapf(err, "failed init tool %s", args.Tool)
}
}
uid := uuid.NewString()
tempDir := filepath.Join(conf.Conf.TempDir, args.Tool, uid)
@ -98,3 +106,18 @@ func AddURL(ctx context.Context, args *AddURLArgs) (task.TaskExtensionInfo, erro
DownloadTaskManager.Add(t)
return t, nil
}
func tryPutUrl(ctx context.Context, storage driver.Driver, dstDirActualPath, urlStr string) bool {
_, ok := storage.(driver.PutURL)
_, okResult := storage.(driver.PutURLResult)
if !ok && !okResult {
return false
}
u, err := url.Parse(urlStr)
if err != nil {
return false
}
dstName := path.Base(u.Path)
err = op.PutURL(ctx, storage, dstDirActualPath, dstName, urlStr)
return err == nil
}