From 5323ad2119148c29cc5d57d1e3cefae117cde4ad Mon Sep 17 00:00:00 2001 From: Master <97798661+Love-YeLin@users.noreply.github.com> Date: Sun, 28 Jan 2024 12:04:20 +0800 Subject: [PATCH] Fix/cloudmind.contentv0.4 (#16) --- biz/application/service/file.go | 8 ++-- biz/infrastructure/convertor/convertor.go | 4 +- biz/infrastructure/mapper/file/mongo.go | 51 +++++++++++++++-------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/biz/application/service/file.go b/biz/application/service/file.go index 577a44a..b003193 100644 --- a/biz/application/service/file.go +++ b/biz/application/service/file.go @@ -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 @@ -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(), @@ -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(), diff --git a/biz/infrastructure/convertor/convertor.go b/biz/infrastructure/convertor/convertor.go index 60021e8..ce9d12d 100644 --- a/biz/infrastructure/convertor/convertor.go +++ b/biz/infrastructure/convertor/convertor.go @@ -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(), } } @@ -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, diff --git a/biz/infrastructure/mapper/file/mongo.go b/biz/infrastructure/mapper/file/mongo.go index c2926e1..4cd837a 100644 --- a/biz/infrastructure/mapper/file/mongo.go +++ b/biz/infrastructure/mapper/file/mongo.go @@ -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" @@ -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) @@ -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"` @@ -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{ @@ -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))