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

@ -80,6 +80,13 @@ type Put interface {
Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up UpdateProgress) error
}
type PutURL interface {
// PutURL directly put a URL into the storage
// Applicable to index-based drivers like URL-Tree or drivers that support uploading files as URLs
// Called when using SimpleHttp for offline downloading, skipping creating a download task
PutURL(ctx context.Context, dstDir model.Obj, name, url string) error
}
//type WriteResult interface {
// MkdirResult
// MoveResult
@ -109,6 +116,13 @@ type PutResult interface {
Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up UpdateProgress) (model.Obj, error)
}
type PutURLResult interface {
// PutURL directly put a URL into the storage
// Applicable to index-based drivers like URL-Tree or drivers that support uploading files as URLs
// Called when using SimpleHttp for offline downloading, skipping creating a download task
PutURL(ctx context.Context, dstDir model.Obj, name, url string) (model.Obj, error)
}
type UpdateProgress func(percentage float64)
type Progress struct {

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
}

View File

@ -586,3 +586,43 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod
}
return errors.WithStack(err)
}
func PutURL(ctx context.Context, storage driver.Driver, dstDirPath, dstName, url string, lazyCache ...bool) error {
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
dstDirPath = utils.FixAndCleanPath(dstDirPath)
_, err := GetUnwrap(ctx, storage, stdpath.Join(dstDirPath, dstName))
if err == nil {
return errors.New("obj already exists")
}
err = MakeDir(ctx, storage, dstDirPath)
if err != nil {
return errors.WithMessagef(err, "failed to put url")
}
dstDir, err := GetUnwrap(ctx, storage, dstDirPath)
if err != nil {
return errors.WithMessagef(err, "failed to put url")
}
switch s := storage.(type) {
case driver.PutURLResult:
var newObj model.Obj
newObj, err = s.PutURL(ctx, dstDir, dstName, url)
if err == nil {
if newObj != nil {
addCacheObj(storage, dstDirPath, model.WrapObjName(newObj))
} else if !utils.IsBool(lazyCache...) {
ClearCache(storage, dstDirPath)
}
}
case driver.PutURL:
err = s.PutURL(ctx, dstDir, dstName, url)
if err == nil && !utils.IsBool(lazyCache...) {
ClearCache(storage, dstDirPath)
}
default:
return errs.NotImplement
}
log.Debugf("put url [%s](%s) done", dstName, url)
return errors.WithStack(err)
}