diff --git a/internal/config/cache.go b/internal/config/cache.go new file mode 100644 index 0000000..b073dc2 --- /dev/null +++ b/internal/config/cache.go @@ -0,0 +1,49 @@ +package config + +import ( + "github.com/tickstep/aliyunpan-api/aliyunpan" + "github.com/tickstep/aliyunpan-api/aliyunpan/apierror" + "github.com/tickstep/library-go/expires" + "path" + "time" +) + +// deleteCache 删除含有 dirs 的缓存 +func (pu *PanUser) deleteCache(dirs []string) { + cache := pu.cacheOpMap.LazyInitCachePoolOp(pu.ActiveDriveId) + for _, v := range dirs { + key := v + "_" + "OrderByName" + _, ok := cache.Load(key) + if ok { + cache.Delete(key) + } + } +} + +// CacheFilesDirectoriesList 缓存获取 +func (pu *PanUser) CacheFilesDirectoriesList(pathStr string) (fdl aliyunpan.FileList, apiError *apierror.ApiError) { + data := pu.cacheOpMap.CacheOperation(pu.ActiveDriveId, pathStr+"_OrderByName", func() expires.DataExpires { + var fi *aliyunpan.FileEntity + fi, apiError = pu.panClient.FileInfoByPath(pu.ActiveDriveId, pathStr) + if apiError != nil { + return nil + } + fileListParam := &aliyunpan.FileListParam{ + DriveId: pu.ActiveDriveId, + ParentFileId: fi.FileId, + } + fdl, apiError = pu.panClient.FileListGetAll(fileListParam) + if apiError != nil { + return nil + } + // construct full path + for _, f := range fdl { + f.Path = path.Join(pathStr, f.FileName) + } + return expires.NewDataExpires(fdl, 1*time.Minute) + }) + if apiError != nil { + return + } + return data.Data().(aliyunpan.FileList), nil +} diff --git a/internal/config/pan_user.go b/internal/config/pan_user.go index 0c12060..3586aef 100644 --- a/internal/config/pan_user.go +++ b/internal/config/pan_user.go @@ -17,6 +17,7 @@ import ( "fmt" "github.com/tickstep/aliyunpan-api/aliyunpan" "github.com/tickstep/aliyunpan-api/aliyunpan/apierror" + "github.com/tickstep/library-go/expires/cachemap" "github.com/tickstep/library-go/logger" "path" "path/filepath" @@ -47,6 +48,7 @@ type PanUser struct { WebToken aliyunpan.WebLoginToken `json:"webToken"` panClient *aliyunpan.PanClient + cacheOpMap cachemap.CacheOpMap } type PanUserList []*PanUser diff --git a/main.go b/main.go index 84893b5..9719f48 100644 --- a/main.go +++ b/main.go @@ -213,13 +213,13 @@ func main() { var ( activeUser = config.Config.ActiveUser() runeFunc = unicode.IsSpace - //cmdRuneFunc = func(r rune) bool { - // switch r { - // case '\'', '"': - // return true - // } - // return unicode.IsSpace(r) - //} + cmdRuneFunc = func(r rune) bool { + switch r { + case '\'', '"': + return true + } + return unicode.IsSpace(r) + } targetPath string ) @@ -248,12 +248,54 @@ func main() { if isAbs { targetDir = path.Dir(targetPath) } else { - targetDir = path.Join(activeUser.Workdir, targetPath) + wd := "/" + if activeUser.IsFileDriveActive() { + wd = activeUser.Workdir + } else if activeUser.IsAlbumDriveActive() { + wd = activeUser.AlbumWorkdir + } + targetDir = path.Join(wd, targetPath) if !isDir { targetDir = path.Dir(targetDir) } } + files, err := activeUser.CacheFilesDirectoriesList(targetDir) + if err != nil { + return + } + for _, file := range files { + if file == nil { + continue + } + var ( + appendLine string + ) + + // 已经有的情况 + if !closed { + if !strings.HasPrefix(file.Path, path.Clean(path.Join(targetDir, path.Base(targetPath)))) { + if path.Base(targetDir) == path.Base(targetPath) { + appendLine = strings.Join(append(lineArgs[:numArgs-1], escaper.EscapeByRuneFunc(path.Join(targetPath, file.FileName), cmdRuneFunc)), " ") + goto handle + } + continue + } + appendLine = strings.Join(append(lineArgs[:numArgs-1], escaper.EscapeByRuneFunc(path.Clean(path.Join(path.Dir(targetPath), file.FileName)), cmdRuneFunc)), " ") + goto handle + } + // 没有的情况 + appendLine = strings.Join(append(lineArgs, escaper.EscapeByRuneFunc(file.FileName, cmdRuneFunc)), " ") + goto handle + + handle: + if file.IsFolder() { + s = append(s, appendLine+"/") + continue + } + s = append(s, appendLine+" ") + continue + } return }) @@ -487,6 +529,10 @@ func main() { // Action: func(c *cli.Context) error { // os.Setenv(config.EnvVerbose, c.String("verbose")) // fmt.Println("显示调试日志", logger.IsVerbose) + // + // user := config.Config.ActiveUser() + // fdl,_ := user.CacheFilesDirectoriesList("/tmp") + // fmt.Println(fdl) // return nil // }, // Flags: []cli.Flag{