File size: 2,099 Bytes
7107f0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package fastwebdav

import (
	"encoding/base64"
	"encoding/json"
	"errors"
	"net/http"
	"strings"

	"github.com/alist-org/alist/v3/drivers/base"
	"github.com/alist-org/alist/v3/internal/model"
	"github.com/go-resty/resty/v2"
	log "github.com/sirupsen/logrus"
)

// do others that not defined in Driver interface

const loginPath = "/user/session"

func (d *FastWebdav) request(method string, path string, callback base.ReqCallback, resp interface{}) error {
	d.Address = strings.TrimSuffix(d.Address, "/")
	u := d.Address + "/" + path

	req := base.RestyClient.R()
	req.SetHeaders(map[string]string{
		"X-Space-App-Key": d.APIKey,
		"Accept":          "application/json, text/plain, */*",
		"Content-Type":    "application/json",
	})

	if callback != nil {
		callback(req)
	}

	if resp != nil {
		req.SetResult(resp)
	}

	res, err := req.Execute(method, u)
	if err != nil {
		return err
	}
	if !res.IsSuccess() {
		return errors.New(res.String())
	}

	return nil
}

func convertSrc(obj model.Obj) map[string]interface{} {
	m := make(map[string]interface{})
	var dirs []string
	var items []string
	if obj.IsDir() {
		dirs = append(dirs, obj.GetID())
	} else {
		items = append(items, obj.GetID())
	}
	m["dirs"] = dirs
	m["items"] = items
	return m
}

func (d *FastWebdav) getFiles(path string, id string) ([]File, error) {
	url := ""
	body := base.Json{}
	httpMethod := http.MethodGet

	if path != "/" {
		provider := getProvider(path)
		url = provider + "/list"
		log.Debug(url)
		httpMethod = http.MethodPost
		b, _ := base64.StdEncoding.DecodeString(id)
		var f File
		_ = json.Unmarshal(b, &f)
		body = base.Json{
			"path_str":       path,
			"parent_file_id": f.Id,
		}
	}

	res := make([]File, 0)
	var resp []File
	err := d.request(httpMethod, url, func(req *resty.Request) {
		req.SetBody(body)
	}, &resp)
	if err != nil {
		return nil, err
	}
	res = append(res, resp...)
	return res, nil
}

func getProvider(s string) string {
	if strings.Count(s, "/") >= 2 {
		start := strings.Index(s, "/")
		end := strings.Index(s[start+1:], "/") + start + 1
		return s[start+1 : end]
	}
	return s[1:]
}