Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/lark/util.go
1987 views
1
package lark
2
3
import (
4
"context"
5
"github.com/Xhofe/go-cache"
6
larkdrive "github.com/larksuite/oapi-sdk-go/v3/service/drive/v1"
7
log "github.com/sirupsen/logrus"
8
"path"
9
"time"
10
)
11
12
const objTokenCacheDuration = 5 * time.Minute
13
const emptyFolderToken = "empty"
14
15
var objTokenCache = cache.NewMemCache[string]()
16
var exOpts = cache.WithEx[string](objTokenCacheDuration)
17
18
func (c *Lark) getObjToken(ctx context.Context, folderPath string) (string, bool) {
19
if token, ok := objTokenCache.Get(folderPath); ok {
20
return token, true
21
}
22
23
dir, name := path.Split(folderPath)
24
// strip the last slash of dir if it exists
25
if len(dir) > 0 && dir[len(dir)-1] == '/' {
26
dir = dir[:len(dir)-1]
27
}
28
if name == "" {
29
return c.rootFolderToken, true
30
}
31
32
var parentToken string
33
var found bool
34
parentToken, found = c.getObjToken(ctx, dir)
35
if !found {
36
return emptyFolderToken, false
37
}
38
39
req := larkdrive.NewListFileReqBuilder().FolderToken(parentToken).Build()
40
resp, err := c.client.Drive.File.ListByIterator(ctx, req)
41
42
if err != nil {
43
log.WithError(err).Error("failed to list files")
44
return emptyFolderToken, false
45
}
46
47
var file *larkdrive.File
48
for {
49
found, file, err = resp.Next()
50
if !found {
51
break
52
}
53
54
if err != nil {
55
log.WithError(err).Error("failed to get next file")
56
break
57
}
58
59
if *file.Name == name {
60
objTokenCache.Set(folderPath, *file.Token, exOpts)
61
return *file.Token, true
62
}
63
}
64
65
return emptyFolderToken, false
66
}
67
68