Skip to content

Commit 88abb32

Browse files
lee-cqxhofeCopilot
authored
feat(url-tree): implement the Put interface to support adding links directly to the UrlTree on the web side (#8312)
* feat(url-tree)支持PUT * feat(url-tree) UrlTree更新时,需要将路径和内容分割 #8303 * fix: stdpath.Join call Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Andy Hsu <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent f0b1aea commit 88abb32

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

drivers/url_tree/driver.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,25 @@ func (d *Urls) PutURL(ctx context.Context, dstDir model.Obj, name, url string) (
243243
}
244244

245245
func (d *Urls) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
246-
return errs.UploadNotSupported
246+
if !d.Writable {
247+
return errs.PermissionDenied
248+
}
249+
d.mutex.Lock()
250+
defer d.mutex.Unlock()
251+
node := GetNodeFromRootByPath(d.root, dstDir.GetPath()) // parent
252+
if node == nil {
253+
return errs.ObjectNotFound
254+
}
255+
if node.isFile() {
256+
return errs.NotFolder
257+
}
258+
file, err := parseFileLine(stream.GetName(), d.HeadSize)
259+
if err != nil {
260+
return err
261+
}
262+
node.Children = append(node.Children, file)
263+
d.updateStorage()
264+
return nil
247265
}
248266

249267
func (d *Urls) updateStorage() {

internal/op/fs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/alist-org/alist/v3/internal/driver"
1111
"github.com/alist-org/alist/v3/internal/errs"
1212
"github.com/alist-org/alist/v3/internal/model"
13+
"github.com/alist-org/alist/v3/internal/stream"
1314
"github.com/alist-org/alist/v3/pkg/generic_sync"
1415
"github.com/alist-org/alist/v3/pkg/singleflight"
1516
"github.com/alist-org/alist/v3/pkg/utils"
@@ -517,6 +518,12 @@ func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file mod
517518
log.Errorf("failed to close file streamer, %v", err)
518519
}
519520
}()
521+
// UrlTree PUT
522+
if storage.GetStorage().Driver == "UrlTree" {
523+
var link string
524+
dstDirPath, link = urlTreeSplitLineFormPath(stdpath.Join(dstDirPath, file.GetName()))
525+
file = &stream.FileStream{Obj: &model.Object{Name: link}}
526+
}
520527
// if file exist and size = 0, delete it
521528
dstDirPath = utils.FixAndCleanPath(dstDirPath)
522529
dstPath := stdpath.Join(dstDirPath, file.GetName())

internal/op/path.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package op
22

33
import (
44
"github.com/alist-org/alist/v3/internal/errs"
5+
stdpath "path"
56
"strings"
67

78
"github.com/alist-org/alist/v3/internal/driver"
@@ -27,3 +28,30 @@ func GetStorageAndActualPath(rawPath string) (storage driver.Driver, actualPath
2728
actualPath = utils.FixAndCleanPath(strings.TrimPrefix(rawPath, mountPath))
2829
return
2930
}
31+
32+
// urlTreeSplitLineFormPath 分割path中分割真实路径和UrlTree定义字符串
33+
func urlTreeSplitLineFormPath(path string) (pp string, file string) {
34+
// url.PathUnescape 会移除 // ,手动加回去
35+
path = strings.Replace(path, "https:/", "https://", 1)
36+
path = strings.Replace(path, "http:/", "http://", 1)
37+
if strings.Contains(path, ":https:/") || strings.Contains(path, ":http:/") {
38+
// URL-Tree模式 /url_tree_drivr/file_name[:size[:time]]:https://example.com/file
39+
fPath := strings.SplitN(path, ":", 2)[0]
40+
pp, _ = stdpath.Split(fPath)
41+
file = path[len(pp):]
42+
} else if strings.Contains(path, "/https:/") || strings.Contains(path, "/http:/") {
43+
// URL-Tree模式 /url_tree_drivr/https://example.com/file
44+
index := strings.Index(path, "/http://")
45+
if index == -1 {
46+
index = strings.Index(path, "/https://")
47+
}
48+
pp = path[:index]
49+
file = path[index+1:]
50+
} else {
51+
pp, file = stdpath.Split(path)
52+
}
53+
if pp == "" {
54+
pp = "/"
55+
}
56+
return
57+
}

0 commit comments

Comments
 (0)