mirror of
https://github.com/samuelncui/yatm.git
synced 2026-01-03 11:45:21 +00:00
feat: add get disk usage and toast infomation
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/samber/lo"
|
||||
"github.com/samuelncui/yatm/entity"
|
||||
"github.com/samuelncui/yatm/library"
|
||||
)
|
||||
@@ -24,14 +25,28 @@ func (api *API) FileGet(ctx context.Context, req *entity.FileGetRequest) (*entit
|
||||
return nil, err
|
||||
}
|
||||
|
||||
children, err := api.lib.ListWithSize(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &entity.FileGetReply{
|
||||
reply := &entity.FileGetReply{
|
||||
File: file,
|
||||
Positions: convertPositions(positions...),
|
||||
Children: convertFiles(children...),
|
||||
}, nil
|
||||
}
|
||||
|
||||
if req.GetNeedSize() {
|
||||
children, err := api.lib.ListWithSize(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reply.Children = convertFiles(children...)
|
||||
|
||||
if reply.File != nil {
|
||||
reply.File.Size += lo.Sum(lo.Map(children, func(file *library.File, _ int) int64 { return file.Size }))
|
||||
}
|
||||
} else {
|
||||
children, err := api.lib.List(ctx, req.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reply.Children = convertFiles(children...)
|
||||
}
|
||||
|
||||
return reply, nil
|
||||
}
|
||||
|
||||
31
apis/source_get_size.go
Normal file
31
apis/source_get_size.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package apis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/samuelncui/yatm/entity"
|
||||
)
|
||||
|
||||
func (api *API) SourceGetSize(ctx context.Context, req *entity.SourceGetSizeRequest) (*entity.SourceGetSizeReply, error) {
|
||||
if req.Path == "./" {
|
||||
req.Path = ""
|
||||
}
|
||||
|
||||
var size int64
|
||||
if err := filepath.Walk(path.Join(api.sourceBase, req.Path), func(_ string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !info.IsDir() {
|
||||
size += info.Size()
|
||||
}
|
||||
return err
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &entity.SourceGetSizeReply{Size: size}, nil
|
||||
}
|
||||
@@ -26,9 +26,6 @@ func (api *API) SourceList(ctx context.Context, req *entity.SourceListRequest) (
|
||||
filteredParts = append(filteredParts, part)
|
||||
}
|
||||
|
||||
// buf, _ := json.Marshal(filteredParts)
|
||||
// logrus.WithContext(ctx).Infof("parts= %s", buf)
|
||||
|
||||
current := ""
|
||||
chain := make([]*entity.SourceFile, 0, len(filteredParts))
|
||||
for _, part := range filteredParts {
|
||||
|
||||
Reference in New Issue
Block a user