Skip to content

Commit

Permalink
Fix/cloudmind.contentv0.4 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Love-YeLin authored Jan 28, 2024
1 parent cf14ff4 commit 5323ad2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
8 changes: 3 additions & 5 deletions biz/application/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ var FileSet = wire.NewSet(

func (s *FileService) GetFileIsExist(ctx context.Context, req *gencontent.GetFileIsExistReq) (resp *gencontent.GetFileIsExistResp, err error) {
resp = new(gencontent.GetFileIsExistResp)
_, err = s.FileMongoMapper.FindOne(ctx, &filemapper.FilterOptions{
OnlyMd5: lo.ToPtr(req.Md5),
})
_, err = s.FileMongoMapper.FindFileIsExist(ctx, req.Md5)
if err != nil {
log.CtxError(ctx, "查询文件md5值是否存在: 发生异常[%v]\n", err)
return resp, err
Expand Down Expand Up @@ -647,7 +645,7 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
Path: path,
FatherId: req.FatherId,
Size: file.Size,
Md5: file.Md5,
FileMd5: file.FileMd5,
IsDel: int64(gencontent.IsDel_Is_no),
CreateAt: time.Now(),
UpdateAt: time.Now(),
Expand Down Expand Up @@ -689,7 +687,7 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
Path: path,
FatherId: front.id,
Size: v.Size,
Md5: v.Md5,
FileMd5: v.FileMd5,
IsDel: int64(gencontent.IsDel_Is_no),
CreateAt: time.Now(),
UpdateAt: time.Now(),
Expand Down
4 changes: 2 additions & 2 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func FileMapperToFile(data *file.File) *gencontent.FileInfo {
UserId: data.UserId,
FatherId: data.FatherId,
SpaceSize: *data.Size,
Md5: data.Md5,
Md5: data.FileMd5,
UpdateAt: data.UpdateAt.Unix(),
}
}
Expand All @@ -79,7 +79,7 @@ func FileToFileMapper(data *gencontent.File) (*file.File, error) {
Path: data.Path,
FatherId: data.FatherId,
Size: data.SpaceSize,
Md5: data.Md5,
FileMd5: data.Md5,
IsDel: data.IsDel,
Tags: data.Tag,
Description: data.Description,
Expand Down
51 changes: 34 additions & 17 deletions biz/infrastructure/mapper/file/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/CloudStriver/go-pkg/utils/pagination"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
"github.com/CloudStriver/go-pkg/utils/util/log"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/trace"
"go.opentelemetry.io/otel"
oteltrace "go.opentelemetry.io/otel/trace"
Expand All @@ -30,6 +31,7 @@ var _ IMongoMapper = (*MongoMapper)(nil)

type (
IMongoMapper interface {
FindFileIsExist(ctx context.Context, md5 string) (bool, error)
Count(ctx context.Context, filter *FilterOptions) (int64, error)
Insert(ctx context.Context, data *File) (string, error)
FindOne(ctx context.Context, fopts *FilterOptions) (*File, error)
Expand All @@ -52,7 +54,7 @@ type (
Path string `bson:"path,omitempty" json:"path,omitempty"`
FatherId string `bson:"fatherId,omitempty" json:"fatherId,omitempty"`
Size *int64 `bson:"size,omitempty" json:"size,omitempty"`
Md5 string `bson:"md5,omitempty" json:"md5,omitempty"`
FileMd5 string `bson:"fileMd5,omitempty" json:"fileMd5,omitempty"`
IsDel int64 `bson:"isDel,omitempty" json:"isDel,omitempty"`
Tags []string `bson:"tags,omitempty" json:"tags,omitempty"`
Description string `bson:"description,omitempty" json:"description,omitempty"`
Expand All @@ -71,22 +73,6 @@ type (
}
)

func (m *MongoMapper) FindManyNotPagination(ctx context.Context, fopts *FilterOptions) ([]*File, error) {
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
_, span := tracer.Start(ctx, "mongo.FindManyNotPagination", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer))
defer span.End()

filter := makeMongoFilter(fopts)
var data []*File
if err := m.conn.Find(ctx, &data, filter, &options.FindOptions{}); err != nil {
if errorx.Is(err, monc.ErrNotFound) {
return nil, consts.ErrNotFound
}
return nil, err
}
return data, nil
}

func NewMongoMapper(config *config.Config) IMongoMapper {
conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.CacheConf)
indexModel := mongo.IndexModel{
Expand All @@ -107,6 +93,37 @@ func NewMongoMapper(config *config.Config) IMongoMapper {
}
}

func (m *MongoMapper) FindManyNotPagination(ctx context.Context, fopts *FilterOptions) ([]*File, error) {
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
_, span := tracer.Start(ctx, "mongo.FindManyNotPagination", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer))
defer span.End()

filter := makeMongoFilter(fopts)
var data []*File
if err := m.conn.Find(ctx, &data, filter, &options.FindOptions{}); err != nil {
if errorx.Is(err, monc.ErrNotFound) {
return nil, consts.ErrNotFound
}
return nil, err
}
return data, nil
}

func (m *MongoMapper) FindFileIsExist(ctx context.Context, md5 string) (bool, error) {
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
_, span := tracer.Start(ctx, "mongo.FindFileIsExist", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer))
defer span.End()

var data []*File
if err := m.conn.Find(ctx, &data, bson.M{consts.FileMd5: md5}, &options.FindOptions{Limit: lo.ToPtr(int64(1))}); err != nil {
if errorx.Is(err, monc.ErrNotFound) {
return false, consts.ErrNotFound
}
return false, err
}
return true, nil
}

func (m *MongoMapper) Insert(ctx context.Context, data *File) (string, error) {
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
_, span := tracer.Start(ctx, "mongo.Insert", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer))
Expand Down

0 comments on commit 5323ad2

Please sign in to comment.