Skip to content

Commit

Permalink
feat: add get disk usage and toast infomation
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelncui committed Oct 20, 2023
1 parent e18b4e9 commit ed5ed8c
Show file tree
Hide file tree
Showing 21 changed files with 1,065 additions and 568 deletions.
31 changes: 23 additions & 8 deletions apis/file_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"

"github.com/samber/lo"
"github.com/samuelncui/yatm/entity"
"github.com/samuelncui/yatm/library"
)
Expand All @@ -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 changes: 31 additions & 0 deletions apis/source_get_size.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 0 additions & 3 deletions apis/source_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions cmd/httpd/yatm-httpd.service
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ After=network.target
[Service]
User=root
Type=simple
UMask=0002
WorkingDirectory=/opt/yatm
ExecStart=/opt/yatm/yatm-httpd
Restart=always
Expand Down
Loading

0 comments on commit ed5ed8c

Please sign in to comment.