Skip to content

Commit

Permalink
chore:add biz file unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
geebytes committed May 23, 2024
1 parent 2cec07a commit 55dc723
Show file tree
Hide file tree
Showing 8 changed files with 398 additions and 71 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ require (
github.com/cockroachdb/errors v1.11.1
github.com/google/wire v0.6.0
github.com/smartystreets/goconvey v1.8.1
github.com/spark-lence/tiga v0.0.0-20240517061929-e81eba889226
github.com/spark-lence/tiga v0.0.0-20240523101823-47fd881052e5
github.com/spf13/cobra v1.8.0
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5
google.golang.org/genproto/googleapis/api v0.0.0-20240521202816-d264139d666e
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.1
)
Expand Down Expand Up @@ -135,7 +135,7 @@ require (
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.21.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240520151616-dc85e6b867a5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
)

Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ github.com/agiledragon/gomonkey/v2 v2.11.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoa
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
Expand Down Expand Up @@ -114,6 +115,7 @@ github.com/gorilla/sessions v1.2.1 h1:DHd3rPN5lE3Ts3D8rKkQ8x/0kqfeNmBAaiSi+o7Fsg
github.com/gorilla/sessions v1.2.1/go.mod h1:dk2InVEVJ0sfLlnXv9EAgkf6ecYs/i80K/zI+bUmuGM=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down Expand Up @@ -154,6 +156,7 @@ github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCy
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand Down Expand Up @@ -191,6 +194,7 @@ github.com/r3labs/sse/v2 v2.10.0 h1:hFEkLLFY4LDifoHdiCN/LlGBAdVJYsANaLqNYa1l/v0=
github.com/r3labs/sse/v2 v2.10.0/go.mod h1:Igau6Whc+F17QUgML1fYe1VPZzTV6EMCnYktEmkNJ7I=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
Expand Down Expand Up @@ -218,6 +222,8 @@ github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9yS
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spark-lence/tiga v0.0.0-20240517061929-e81eba889226 h1:WKMb1r+0r5lDKrynMlC9v62EajmtLXMIIk6NzrVbcNs=
github.com/spark-lence/tiga v0.0.0-20240517061929-e81eba889226/go.mod h1:MSL8X9t+qvpQ4Tq3vVPKncq9RJcCzF2XGEWkCuNhm6Q=
github.com/spark-lence/tiga v0.0.0-20240523101823-47fd881052e5 h1:lbs5Qf0cOLvIu0d1hftGNYWeudonk77XA6FRxa40yN8=
github.com/spark-lence/tiga v0.0.0-20240523101823-47fd881052e5/go.mod h1:MSL8X9t+qvpQ4Tq3vVPKncq9RJcCzF2XGEWkCuNhm6Q=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
Expand Down Expand Up @@ -386,8 +392,12 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5 h1:P8OJ/WCl/Xo4E4zoe4/bifHpSmmKwARqyqE4nW6J2GQ=
google.golang.org/genproto/googleapis/api v0.0.0-20240520151616-dc85e6b867a5/go.mod h1:RGnPtTG7r4i8sPlNyDeikXF99hMM+hN6QMm4ooG9g2g=
google.golang.org/genproto/googleapis/api v0.0.0-20240521202816-d264139d666e h1:SkdGTrROJl2jRGT/Fxv5QUf9jtdKCQh4KQJXbXVLAi0=
google.golang.org/genproto/googleapis/api v0.0.0-20240521202816-d264139d666e/go.mod h1:LweJcLbyVij6rCex8YunD8DYR5VDonap/jYl3ZRxcIU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240520151616-dc85e6b867a5 h1:Q2RxlXqh1cgzzUgV261vBO2jI5R/3DD1J2pM0nI4NhU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240520151616-dc85e6b867a5/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e h1:Elxv5MwEkCI9f5SkoL6afed6NTdxaGoAo39eANBwHL8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240521202816-d264139d666e/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
Expand All @@ -397,6 +407,7 @@ gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UD
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
Expand Down
53 changes: 20 additions & 33 deletions internal/biz/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import (
"crypto/sha256"
goErr "errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"time"

"github.com/begonia-org/begonia/internal/pkg/config"
"github.com/begonia-org/begonia/internal/pkg/errors"
gosdk "github.com/begonia-org/go-sdk"
Expand All @@ -15,14 +23,6 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/spark-lence/tiga"
"google.golang.org/grpc/codes"
"io"
"log"
"net/http"
"os"
"path/filepath"
"sort"
"strings"
"time"
)

type FileRepo interface {
Expand Down Expand Up @@ -135,8 +135,8 @@ func (f *FileUsecase) commitFile(dir string, filename string, authorId string, a
// 空提交处理
if goErr.Is(err, git.ErrEmptyCommit) {
headRef, err := repo.Head()
if err != nil {
return "", err
if err != nil || headRef.Hash().IsZero() {
return "", fmt.Errorf("get head ref error:%w or head ref is nil", err)
}
return headRef.Hash().String(), nil
}
Expand All @@ -163,12 +163,6 @@ func (f *FileUsecase) checkIn(key string) (string, error) {
if key == "" || strings.HasPrefix(key, "/") {
return "", gosdk.NewError(errors.ErrInvalidFileKey, int32(api.FileSvrStatus_FILE_INVALIDATE_KEY_ERR), codes.InvalidArgument, "invalid_key")
}
// if authorId == "" {
// return "", gosdk.NewError(errors.ErrIdentityMissing, int32(user.UserSvrCode_USER_IDENTITY_MISSING_ERR), codes.InvalidArgument, "not_found_identity")
// }
// if !strings.HasPrefix(key, authorId) {
// key = authorId + "/" + key
// }
return key, nil
}

Expand Down Expand Up @@ -348,7 +342,7 @@ func (f *FileUsecase) getPersistenceKeyParts(key string) string {
}
func (f *FileUsecase) getUri(filePath string) (string, error) {
uploadRootDir := f.config.GetUploadDir()
log.Printf("uploadRootDir:%s,filePath:%s", uploadRootDir, filePath)
// log.Printf("uploadRootDir:%s,filePath:%s", uploadRootDir, filePath)
uri, err := filepath.Rel(uploadRootDir, filePath)
if err != nil {
return "", gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "get_file_uri")
Expand Down Expand Up @@ -380,7 +374,7 @@ func (f *FileUsecase) CompleteMultipartUploadFile(ctx context.Context, in *api.C
in.Key = filepath.Join(authorId, key)
partsDir := f.getPartsDir(in.UploadId)
if !pathExists(partsDir) {
err := gosdk.NewError(errors.ErrUploadIdNotFound, int32(api.FileSvrStatus_FILE_NOT_FOUND_UPLOADID_ERR), codes.NotFound, "upload_id_not_found")
err := gosdk.NewError(fmt.Errorf("%s:%s", in.UploadId, errors.ErrUploadIdNotFound.Error()), int32(api.FileSvrStatus_FILE_NOT_FOUND_UPLOADID_ERR), codes.NotFound, "upload_id_not_found")
return nil, err

}
Expand All @@ -396,7 +390,7 @@ func (f *FileUsecase) CompleteMultipartUploadFile(ctx context.Context, in *api.C
// merge files to uploadDir/key
err = f.mergeFiles(files, filePath)
if err != nil {
return nil, gosdk.NewError(fmt.Errorf("merge file error"), int32(common.Code_INTERNAL_ERROR), codes.Internal, "merge_files")
return nil, gosdk.NewError(fmt.Errorf("merge file error:%w", err), int32(common.Code_INTERNAL_ERROR), codes.Internal, "merge_files")
}
// the parts file has been merged, remove the parts dir to uploadDir/parts/key
keyParts := f.getPersistenceKeyParts(in.Key)
Expand All @@ -420,7 +414,6 @@ func (f *FileUsecase) CompleteMultipartUploadFile(ctx context.Context, in *api.C
return nil, gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "commit_file")
}
}

os.RemoveAll(filepath.Join(f.config.GetUploadDir(), in.UploadId))

return &api.CompleteMultipartUploadResponse{
Expand All @@ -442,21 +435,18 @@ func (f *FileUsecase) DownloadForRange(ctx context.Context, in *api.DownloadRequ
}

file, err := f.getReader(in.Key, in.Version)
if err == git.ErrRepositoryNotExists || os.IsNotExist(err) {
return nil, 0, gosdk.NewError(err, int32(common.Code_NOT_FOUND), codes.NotFound, "file_not_found")
}
if err != nil {
return nil, 0, gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "open_file")
code, grcpCode := f.checkStatusCode(err)
return nil, 0, gosdk.NewError(err, code, grcpCode, "open_file")
}
defer file.Close()

var buf []byte
if end > 0 {
buf = make([]byte, end-start+1)
} else {
buf = make([]byte, file.Size()-start+1)
buf = make([]byte, file.Size()-start)
}
// log.Printf("start:%d,end:%d,bufsize:%d", start, end, len(buf))
_, err = file.ReadAt(buf, start)
if err != nil && err != io.EOF {
err = gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "read_file")
Expand All @@ -474,7 +464,8 @@ func (f *FileUsecase) Metadata(ctx context.Context, in *api.FileMetadataRequest,
in.Key = key
file, err := f.getReader(in.Key, in.Version)
if err != nil {
return nil, gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "open_file")
code,grpcCode:=f.checkStatusCode(err)
return nil, gosdk.NewError(err, code, grpcCode, "open_file")

}
hasher := sha256.New()
Expand Down Expand Up @@ -528,7 +519,6 @@ func (f *FileUsecase) getReader(key string, version string) (FileReader, error)
var err error
if version != "" {
fileReader, err = NewFileVersionReader(filePath, version)
// log.Printf("version fileReader:%v", err)
if err != nil {
return nil, err
}
Expand All @@ -547,13 +537,10 @@ func (f *FileUsecase) Version(ctx context.Context, key, authorId string) (string
if err != nil {
return "", err
}
// fileDir := filepath.Join(f.config.GetUploadDir(), in.Key)
file, err := f.getReader(key, "latest")
if err == git.ErrRepositoryNotExists {
return "", gosdk.NewError(err, int32(common.Code_NOT_FOUND), codes.NotFound, "file_not_found")
}
if err != nil {
return "", gosdk.NewError(err, int32(common.Code_INTERNAL_ERROR), codes.Internal, "open_file")
code, grpcCode := f.checkStatusCode(err)
return "", gosdk.NewError(err, code, grpcCode, "open_file")
}
defer file.Close()
return file.(FileVersionReader).Version(), nil
Expand Down
Loading

0 comments on commit 55dc723

Please sign in to comment.