Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/bitqiu/util.go
1986 views
1
package bitqiu
2
3
import (
4
"path"
5
"strings"
6
"time"
7
8
"github.com/alist-org/alist/v3/internal/model"
9
"github.com/alist-org/alist/v3/pkg/utils"
10
)
11
12
type Object struct {
13
model.Object
14
ParentID string
15
}
16
17
func (r Resource) toObject(parentID, parentPath string) (model.Obj, error) {
18
id := r.ResourceID
19
if id == "" {
20
id = r.ResourceUID
21
}
22
obj := &Object{
23
Object: model.Object{
24
ID: id,
25
Name: r.Name,
26
IsFolder: r.ResourceType == 1,
27
},
28
ParentID: parentID,
29
}
30
if r.Size != nil {
31
if size, err := (*r.Size).Int64(); err == nil {
32
obj.Size = size
33
}
34
}
35
if ct := parseBitQiuTime(r.CreateTime); !ct.IsZero() {
36
obj.Ctime = ct
37
}
38
if mt := parseBitQiuTime(r.UpdateTime); !mt.IsZero() {
39
obj.Modified = mt
40
}
41
if r.FileMD5 != "" {
42
obj.HashInfo = utils.NewHashInfo(utils.MD5, strings.ToLower(r.FileMD5))
43
}
44
obj.SetPath(path.Join(parentPath, obj.Name))
45
return obj, nil
46
}
47
48
func parseBitQiuTime(value *string) time.Time {
49
if value == nil {
50
return time.Time{}
51
}
52
trimmed := strings.TrimSpace(*value)
53
if trimmed == "" {
54
return time.Time{}
55
}
56
if ts, err := time.ParseInLocation("2006-01-02 15:04:05", trimmed, time.Local); err == nil {
57
return ts
58
}
59
return time.Time{}
60
}
61
62
func updateObjectName(obj model.Obj, newName string) model.Obj {
63
newPath := path.Join(parentPathOf(obj.GetPath()), newName)
64
65
switch o := obj.(type) {
66
case *Object:
67
o.Name = newName
68
o.Object.Name = newName
69
o.SetPath(newPath)
70
return o
71
case *model.Object:
72
o.Name = newName
73
o.SetPath(newPath)
74
return o
75
}
76
77
if setter, ok := obj.(model.SetPath); ok {
78
setter.SetPath(newPath)
79
}
80
81
return &model.Object{
82
ID: obj.GetID(),
83
Path: newPath,
84
Name: newName,
85
Size: obj.GetSize(),
86
Modified: obj.ModTime(),
87
Ctime: obj.CreateTime(),
88
IsFolder: obj.IsDir(),
89
HashInfo: obj.GetHash(),
90
}
91
}
92
93
func parentPathOf(p string) string {
94
if p == "" {
95
return ""
96
}
97
dir := path.Dir(p)
98
if dir == "." {
99
return ""
100
}
101
return dir
102
}
103
104