File size: 1,446 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 |
package lark
import (
"context"
"github.com/Xhofe/go-cache"
larkdrive "github.com/larksuite/oapi-sdk-go/v3/service/drive/v1"
log "github.com/sirupsen/logrus"
"path"
"time"
)
const objTokenCacheDuration = 5 * time.Minute
const emptyFolderToken = "empty"
var objTokenCache = cache.NewMemCache[string]()
var exOpts = cache.WithEx[string](objTokenCacheDuration)
func (c *Lark) getObjToken(ctx context.Context, folderPath string) (string, bool) {
if token, ok := objTokenCache.Get(folderPath); ok {
return token, true
}
dir, name := path.Split(folderPath)
// strip the last slash of dir if it exists
if len(dir) > 0 && dir[len(dir)-1] == '/' {
dir = dir[:len(dir)-1]
}
if name == "" {
return c.rootFolderToken, true
}
var parentToken string
var found bool
parentToken, found = c.getObjToken(ctx, dir)
if !found {
return emptyFolderToken, false
}
req := larkdrive.NewListFileReqBuilder().FolderToken(parentToken).Build()
resp, err := c.client.Drive.File.ListByIterator(ctx, req)
if err != nil {
log.WithError(err).Error("failed to list files")
return emptyFolderToken, false
}
var file *larkdrive.File
for {
found, file, err = resp.Next()
if !found {
break
}
if err != nil {
log.WithError(err).Error("failed to get next file")
break
}
if *file.Name == name {
objTokenCache.Set(folderPath, *file.Token, exOpts)
return *file.Token, true
}
}
return emptyFolderToken, false
}
|