Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/pkg/gowebdav/utils.go
1560 views
1
package gowebdav
2
3
import (
4
"bytes"
5
"encoding/xml"
6
"fmt"
7
"io"
8
"net/url"
9
"strconv"
10
"strings"
11
"time"
12
)
13
14
func log(msg interface{}) {
15
fmt.Println(msg)
16
}
17
18
// PathEscape escapes all segments of a given path
19
func PathEscape(path string) string {
20
s := strings.Split(path, "/")
21
for i, e := range s {
22
s[i] = url.PathEscape(e)
23
}
24
return strings.Join(s, "/")
25
}
26
27
// FixSlash appends a trailing / to our string
28
func FixSlash(s string) string {
29
if !strings.HasSuffix(s, "/") {
30
s += "/"
31
}
32
return s
33
}
34
35
// FixSlashes appends and prepends a / if they are missing
36
func FixSlashes(s string) string {
37
if !strings.HasPrefix(s, "/") {
38
s = "/" + s
39
}
40
41
return FixSlash(s)
42
}
43
44
// Join joins two paths
45
func Join(path0 string, path1 string) string {
46
return strings.TrimSuffix(path0, "/") + "/" + strings.TrimPrefix(path1, "/")
47
}
48
49
// String pulls a string out of our io.Reader
50
func String(r io.Reader) string {
51
buf := new(bytes.Buffer)
52
// TODO - make String return an error as well
53
_, _ = buf.ReadFrom(r)
54
return buf.String()
55
}
56
57
func parseUint(s *string) uint {
58
if n, e := strconv.ParseUint(*s, 10, 32); e == nil {
59
return uint(n)
60
}
61
return 0
62
}
63
64
func parseInt64(s *string) int64 {
65
if n, e := strconv.ParseInt(*s, 10, 64); e == nil {
66
return n
67
}
68
return 0
69
}
70
71
func parseModified(s *string) time.Time {
72
if t, e := time.Parse(time.RFC1123, *s); e == nil {
73
return t
74
}
75
return time.Unix(0, 0)
76
}
77
78
func parseXML(data io.Reader, resp interface{}, parse func(resp interface{}) error) error {
79
decoder := xml.NewDecoder(data)
80
for t, _ := decoder.Token(); t != nil; t, _ = decoder.Token() {
81
switch se := t.(type) {
82
case xml.StartElement:
83
if se.Name.Local == "response" {
84
if e := decoder.DecodeElement(resp, &se); e == nil {
85
if err := parse(resp); err != nil {
86
return err
87
}
88
}
89
}
90
}
91
}
92
return nil
93
}
94
95
// limitedReadCloser wraps a io.ReadCloser and limits the number of bytes that can be read from it.
96
type limitedReadCloser struct {
97
rc io.ReadCloser
98
remaining int
99
}
100
101
func (l *limitedReadCloser) Read(buf []byte) (int, error) {
102
if l.remaining <= 0 {
103
return 0, io.EOF
104
}
105
106
if len(buf) > l.remaining {
107
buf = buf[0:l.remaining]
108
}
109
110
n, err := l.rc.Read(buf)
111
l.remaining -= n
112
113
return n, err
114
}
115
116
func (l *limitedReadCloser) Close() error {
117
return l.rc.Close()
118
}
119
120