feat: add get disk usage and toast infomation

This commit is contained in:
Samuel N Cui
2023-10-20 20:22:07 +08:00
parent e18b4e905a
commit ed5ed8c7a1
21 changed files with 1064 additions and 567 deletions

View File

@@ -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
View 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
}

View File

@@ -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 {