diff --git a/biz/application/service/file.go b/biz/application/service/file.go index 7868d66..6ae747c 100644 --- a/biz/application/service/file.go +++ b/biz/application/service/file.go @@ -3,11 +3,11 @@ package service import ( "context" "errors" - "fmt" "github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts" "github.com/CloudStriver/cloudmind-content/biz/infrastructure/convertor" filemapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/file" sharefilemapper "github.com/CloudStriver/cloudmind-content/biz/infrastructure/mapper/sharefile" + "github.com/CloudStriver/go-pkg/utils/pagination" "github.com/CloudStriver/go-pkg/utils/pagination/esp" "github.com/CloudStriver/go-pkg/utils/pagination/mongop" "github.com/CloudStriver/go-pkg/utils/util/log" @@ -58,14 +58,10 @@ var FileSet = wire.NewSet( func (s *FileService) GetFileIsExist(ctx context.Context, req *gencontent.GetFileIsExistReq) (resp *gencontent.GetFileIsExistResp, err error) { resp = new(gencontent.GetFileIsExistResp) - var ok bool - ok, err = s.FileMongoMapper.FindFileIsExist(ctx, req.Md5) + resp.Ok, err = s.FileMongoMapper.FindFileIsExist(ctx, req.Md5) if err != nil { - log.CtxError(ctx, "查询文件md5值是否存在: 发生异常[%v]\n", err) return resp, err } - - resp.Ok = ok return resp, nil } @@ -73,13 +69,14 @@ func (s *FileService) GetFile(ctx context.Context, req *gencontent.GetFileReq) ( resp = new(gencontent.GetFileResp) var file *filemapper.File if file, err = s.FileMongoMapper.FindOne(ctx, req.FileId); err != nil { - log.CtxError(ctx, "查询文件详细信息: 发生异常[%v]\n", err) return resp, err } resp.File = convertor.FileMapperToFile(file) + + // 如果是获取文件夹大小,需要计算文件夹大小 if req.IsGetSize && resp.File.SpaceSize == int64(gencontent.Folder_Folder_Size) { if resp.File.SpaceSize, err = s.GetFolderSize(ctx, resp.File.Path); err != nil { - return resp, consts.ErrCalFileSize + return resp, err } } return resp, nil @@ -89,15 +86,14 @@ func (s *FileService) GetFilesByIds(ctx context.Context, req *gencontent.GetFile resp = new(gencontent.GetFilesByIdsResp) var files []*filemapper.File if files, err = s.FileMongoMapper.FindManyByIds(ctx, req.FileIds); err != nil { - log.CtxError(ctx, "获取标签集 失败[%v]\n", err) return resp, err } // 创建映射:文件ID到文件 - fileMap := make(map[string]*filemapper.File) - for _, file := range files { + fileMap := make(map[string]*filemapper.File, len(req.FileIds)) + lo.ForEach(files, func(file *filemapper.File, _ int) { fileMap[file.ID.Hex()] = file - } + }) // 按req.FileIds中的ID顺序映射和转换 resp.Files = lo.Map(req.FileIds, func(id string, _ int) *gencontent.FileInfo { @@ -110,12 +106,13 @@ func (s *FileService) GetFilesByIds(ctx context.Context, req *gencontent.GetFile } func (s *FileService) GetRecycleBinFiles(ctx context.Context, req *gencontent.GetRecycleBinFilesReq) (resp *gencontent.GetRecycleBinFilesResp, err error) { resp = new(gencontent.GetRecycleBinFilesResp) - var total int64 - var files []*filemapper.File - filter := convertor.FileFilterOptionsToFilterOptions(req.FilterOptions) + var ( + total int64 + files []*filemapper.File + ) p := convertor.ParsePagination(req.PaginationOptions) - if files, total, err = s.FileMongoMapper.FindManyAndCount(ctx, filter, p, mongop.IdCursorType); err != nil { - log.CtxError(ctx, "查询回收站文件列表: 发生异常[%v]\n", err) + if files, total, err = s.FileMongoMapper.FindManyAndCount(ctx, convertor.FileFilterOptionsToFilterOptions(req.FilterOptions), + p, mongop.IdCursorType); err != nil { return resp, err } if p.LastToken != nil { @@ -152,9 +149,11 @@ func (s *FileService) GetFileList(ctx context.Context, req *gencontent.GetFileLi resp.FatherIdPath = getFileResp.File.Path paths := strings.Split(getFileResp.File.Path, "/") if len(paths) > 1 { - filelist, err1 := s.FileMongoMapper.FindManyNotPagination(ctx, &filemapper.FilterOptions{ + filelist, err1 := s.FileMongoMapper.FindMany(ctx, &filemapper.FilterOptions{ OnlyFileIds: paths[1:], - }) + }, &pagination.PaginationOptions{ + Limit: lo.ToPtr(int64(len(paths) - 1)), + }, mongop.IdCursorType) if err1 != nil { return err1 } @@ -179,7 +178,6 @@ func (s *FileService) GetFileList(ctx context.Context, req *gencontent.GetFileLi p := convertor.ParsePagination(req.PaginationOptions) if req.SearchOptions == nil { if files, total, err2 = s.FileMongoMapper.FindManyAndCount(ctx, filter, p, cursor); err != nil { - log.CtxError(ctx, "查询文件列表: 发生异常[%v]\n", err2) return err2 } } else { @@ -242,22 +240,24 @@ func (s *FileService) GetFileBySharingCode(ctx context.Context, req *gencontent. data *gencontent.GetFileListResp ) - if shareFiles, err = s.FileMongoMapper.FindManyNotPagination(ctx, &filemapper.FilterOptions{ + if shareFiles, err = s.FileMongoMapper.FindMany(ctx, &filemapper.FilterOptions{ OnlyFileIds: req.FileIds, OnlyIsDel: lo.ToPtr(int64(gencontent.Deletion_Deletion_notDel)), - }); err != nil { + }, &pagination.PaginationOptions{ + Limit: lo.ToPtr(int64(len(req.FileIds))), + }, mongop.IdCursorType); err != nil { return resp, err } - fmt.Printf("\n[%v]\n", req) - if req.OnlyFileId != nil { + switch { + case req.OnlyFileId != nil: if res, ok, err = s.CheckShareFile(ctx, shareFiles, req.OnlyFileId); err != nil { return resp, err } if ok { resp.Files = []*gencontent.FileInfo{res} } - } else if req.OnlyFatherId != nil { + case req.OnlyFatherId != nil: if _, ok, err = s.CheckShareFile(ctx, shareFiles, req.OnlyFatherId); err != nil { return resp, err } @@ -275,7 +275,7 @@ func (s *FileService) GetFileBySharingCode(ctx context.Context, req *gencontent. resp.FatherIdPath = data.FatherIdPath resp.FatherNamePath = data.FatherNamePath } - } else { + default: resp.Files = lo.Map[*filemapper.File, *gencontent.FileInfo](shareFiles, func(item *filemapper.File, _ int) *gencontent.FileInfo { return convertor.FileMapperToFile(item) }) @@ -289,7 +289,6 @@ func (s *FileService) GetFileBySharingCode(ctx context.Context, req *gencontent. func (s *FileService) GetFolderSize(ctx context.Context, path string) (resp int64, err error) { if resp, err = s.FileMongoMapper.FindFolderSize(ctx, path); err != nil { - log.CtxError(ctx, "查询文件夹空间大小: 发生异常[%v]\n", err) return 0, err } return resp, nil @@ -297,10 +296,8 @@ func (s *FileService) GetFolderSize(ctx context.Context, path string) (resp int6 func (s *FileService) CreateFile(ctx context.Context, req *gencontent.CreateFileReq) (resp *gencontent.CreateFileResp, err error) { resp = new(gencontent.CreateFileResp) - data := convertor.FileToFileMapper(req.File) - resp.FileId, err = s.FileMongoMapper.Insert(ctx, data) + resp.FileId, err = s.FileMongoMapper.Insert(ctx, convertor.FileToFileMapper(req.File)) if err != nil { - log.CtxError(ctx, "创建文件: 发生异常[%v]\n", err) return resp, err } @@ -311,7 +308,6 @@ func (s *FileService) UpdateFile(ctx context.Context, req *gencontent.UpdateFile resp = new(gencontent.UpdateFileResp) data := convertor.FileToFileMapper(req.File) if _, err = s.FileMongoMapper.Update(ctx, data); err != nil { - log.CtxError(ctx, "更新文件信息: 发生异常[%v]\n", err) return resp, err } return resp, nil @@ -366,7 +362,6 @@ func (s *FileService) MoveFile(ctx context.Context, req *gencontent.MoveFileReq) func (s *FileService) CompletelyRemoveFile(ctx context.Context, req *gencontent.CompletelyRemoveFileReq) (resp *gencontent.CompletelyRemoveFileResp, err error) { resp = new(gencontent.CompletelyRemoveFileResp) if _, err = s.FileMongoMapper.Delete(ctx, req.FileId, req.UserId); err != nil { - log.CtxError(ctx, "删除文件: 发生异常[%v]\n", err) return resp, err } return resp, nil @@ -493,8 +488,7 @@ func (s *FileService) GetShareList(ctx context.Context, req *gencontent.GetShare p := convertor.ParsePagination(req.PaginationOptions) if shareCodes, total, err = s.ShareFileMongoMapper.FindManyAndCount(ctx, convertor.ShareFileFilterOptionsToShareCodeOptions(req.ShareFileFilterOptions), p, mongop.IdCursorType); err != nil { - log.CtxError(ctx, "查询文件分享链接列表: 发生异常[%v]\n", err) - return nil, err + return resp, err } if p.LastToken != nil { @@ -509,36 +503,28 @@ func (s *FileService) GetShareList(ctx context.Context, req *gencontent.GetShare func (s *FileService) CreateShareCode(ctx context.Context, req *gencontent.CreateShareCodeReq) (resp *gencontent.CreateShareCodeResp, err error) { resp = new(gencontent.CreateShareCodeResp) - var id, key string data := convertor.ShareFileToShareFileMapper(req.ShareFile) data.CreateAt = time.Now() if req.ShareFile.EffectiveTime >= 0 { data.DeletedAt = data.CreateAt.Add(time.Duration(req.ShareFile.EffectiveTime)*time.Second + 720*time.Hour) } - if id, key, err = s.ShareFileMongoMapper.Insert(ctx, data); err != nil { - log.CtxError(ctx, "创建文件分享链接: 发生异常[%v]\n", err) + if resp.Code, resp.Key, err = s.ShareFileMongoMapper.Insert(ctx, data); err != nil { return resp, err } - resp.Code = id - resp.Key = key return resp, nil } func (s *FileService) UpdateShareCode(ctx context.Context, req *gencontent.UpdateShareCodeReq) (resp *gencontent.UpdateShareCodeResp, err error) { - resp = new(gencontent.UpdateShareCodeResp) data := convertor.ShareFileToShareFileMapper(req.ShareFile) if _, err = s.ShareFileMongoMapper.Update(ctx, data); err != nil { - log.CtxError(ctx, "修改文件分享链接: 发生异常[%v]\n", err) return resp, err } return resp, nil } func (s *FileService) DeleteShareCode(ctx context.Context, req *gencontent.DeleteShareCodeReq) (resp *gencontent.DeleteShareCodeResp, err error) { - resp = new(gencontent.DeleteShareCodeResp) - filter := convertor.ShareFileFilterOptionsToShareCodeOptions(req.ShareFileFilterOptions) - if _, err := s.ShareFileMongoMapper.Delete(ctx, filter); err != nil { + if _, err := s.ShareFileMongoMapper.Delete(ctx, convertor.ShareFileFilterOptionsToShareCodeOptions(req.ShareFileFilterOptions)); err != nil { log.CtxError(ctx, "删除文件分享链接: 发生异常[%v]\n", err) return resp, err } @@ -549,7 +535,6 @@ func (s *FileService) ParsingShareCode(ctx context.Context, req *gencontent.Pars resp = new(gencontent.ParsingShareCodeResp) var shareFile *sharefilemapper.ShareFile if shareFile, err = s.ShareFileMongoMapper.FindOne(ctx, req.Code, req.Key); err != nil { - log.CtxError(ctx, "提取文件分享链接: 发生异常[%v]\n", err) return resp, err } res := convertor.ShareFileMapperToShareFile(shareFile) diff --git a/biz/infrastructure/mapper/file/mongo.go b/biz/infrastructure/mapper/file/mongo.go index c8d5813..48cc0e9 100644 --- a/biz/infrastructure/mapper/file/mongo.go +++ b/biz/infrastructure/mapper/file/mongo.go @@ -2,8 +2,7 @@ package file import ( "context" - errorx "errors" - "fmt" + "errors" "github.com/CloudStriver/cloudmind-content/biz/infrastructure/config" "github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts" "github.com/CloudStriver/go-pkg/utils/pagination" @@ -40,7 +39,6 @@ type ( FindOne(ctx context.Context, id string) (*File, error) FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*File, error) FindManyByIds(ctx context.Context, ids []string) ([]*File, error) - FindManyNotPagination(ctx context.Context, fopts *FilterOptions) ([]*File, error) FindFolderSize(ctx context.Context, path string) (int64, error) FindManyAndCount(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*File, int64, error) Update(ctx context.Context, data *File) (*mongo.UpdateResult, error) @@ -120,29 +118,16 @@ func (m *MongoMapper) FindManyByIds(ctx context.Context, ids []string) ([]*File, }), }, } - if err := m.conn.Find(ctx, &data, filter); err != nil { - if errorx.Is(err, monc.ErrNotFound) { - return nil, consts.ErrNotFound - } - return nil, err - } - return data, nil -} - -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); err != nil { - if errorx.Is(err, monc.ErrNotFound) { - return nil, consts.ErrNotFound - } + err := m.conn.Find(ctx, &data, filter) + switch { + case errors.Is(err, monc.ErrNotFound): + return nil, consts.ErrNotFound + case err == nil: + return data, nil + default: + log.CtxError(ctx, "获取文件列表异常[%v]\n", err) return nil, err } - return data, nil } func (m *MongoMapper) FindAndInsert(ctx context.Context, data *File) (string, error) { @@ -150,11 +135,9 @@ func (m *MongoMapper) FindAndInsert(ctx context.Context, data *File) (string, er _, span := tracer.Start(ctx, "mongo.FindAndInsert", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer)) defer span.End() - fmt.Printf("\n[%v]\n", *data) - var res File if err := m.conn.FindOneNoCache(ctx, &res, bson.M{consts.FatherId: data.FatherId, consts.Name: data.Name, consts.IsDel: data.IsDel}); err != nil { - if errorx.Is(err, monc.ErrNotFound) { + if errors.Is(err, monc.ErrNotFound) { return m.Insert(ctx, data) } return "", err @@ -185,7 +168,7 @@ func (m *MongoMapper) FindAndInsertMany(ctx context.Context, data []*File) ([]st return func() error { var file File if err := m.conn.FindOneNoCache(ctx, &file, bson.M{consts.FatherId: item.FatherId, consts.Name: item.Name, consts.IsDel: item.IsDel}); err != nil { - if errorx.Is(err, monc.ErrNotFound) { + if errors.Is(err, monc.ErrNotFound) { return nil } else { return err @@ -214,13 +197,17 @@ func (m *MongoMapper) FindFileIsExist(ctx context.Context, md5 string) (bool, er 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 { - return false, err - } - if len(data) == 0 { + key := prefixFileCacheKey + md5 + err := m.conn.FindOne(ctx, key, &data, bson.M{consts.FileMd5: md5}) + switch { + case errors.Is(err, monc.ErrNotFound): return false, nil + case err == nil: + return true, nil + default: + log.CtxError(ctx, "查询文件md5值是否存在: 发生异常[%v]\n", err) + return false, err } - return true, nil } func (m *MongoMapper) Insert(ctx context.Context, data *File) (string, error) { @@ -230,41 +217,39 @@ func (m *MongoMapper) Insert(ctx context.Context, data *File) (string, error) { if data.ID.IsZero() { data.ID = primitive.NewObjectID() - data.CreateAt = time.Now() } + data.CreateAt = time.Now() data.UpdateAt = time.Now() data.Path = data.Path + "/" + data.ID.Hex() key := prefixFileCacheKey + data.ID.Hex() - fmt.Printf("\n[%v]\n", *data) - ID, err := m.conn.InsertOne(ctx, key, data) + _, err := m.conn.InsertOne(ctx, key, data) if err != nil { data.Name = data.Name + "_" + strconv.FormatInt(time.Now().Unix(), 10) - if ID, err = m.conn.InsertOne(ctx, key, data); err != nil { + if _, err = m.conn.InsertOne(ctx, key, data); err != nil { + log.CtxError(ctx, "插入文件信息: 发生异常[%v]\n", err) return "", err } } - return ID.InsertedID.(primitive.ObjectID).Hex(), nil + return data.ID.Hex(), nil } func (m *MongoMapper) FindOne(ctx context.Context, id string) (*File, error) { tracer := otel.GetTracerProvider().Tracer(trace.TraceName) _, span := tracer.Start(ctx, "mongo.FindOne", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer)) defer span.End() - var data File - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidId - } + oid, _ := primitive.ObjectIDFromHex(id) key := prefixFileCacheKey + id - if err := m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid}); err != nil { - if errorx.Is(err, monc.ErrNotFound) { - return nil, consts.ErrNotFound - } else { - return nil, err - } + err := m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid}) + switch { + case errors.Is(err, monc.ErrNotFound): + return nil, consts.ErrNotFound + case err == nil: + return &data, nil + default: + log.CtxError(ctx, "查询文件详细信息: 发生异常[%v]\n", err) + return nil, err } - return &data, nil } func (m *MongoMapper) Count(ctx context.Context, fopts *FilterOptions) (int64, error) { @@ -289,14 +274,16 @@ func (m *MongoMapper) FindMany(ctx context.Context, fopts *FilterOptions, popts } var data []*File - if err = m.conn.Find(ctx, &data, filter, &options.FindOptions{ + err = m.conn.Find(ctx, &data, filter, &options.FindOptions{ Sort: sort, Limit: popts.Limit, Skip: popts.Offset, - }); err != nil { - if errorx.Is(err, monc.ErrNotFound) { - return nil, consts.ErrNotFound - } + }) + switch { + case errors.Is(err, monc.ErrNotFound): + return nil, consts.ErrNotFound + case err != nil: + log.CtxError(ctx, "查询文件列表: 发生异常[%v]\n", err) return nil, err } @@ -341,19 +328,23 @@ func (m *MongoMapper) FindFolderSize(ctx context.Context, path string) (int64, e if result.Next(ctx) { err = result.Decode(&size) if err != nil { + log.CtxError(ctx, "查询文件夹大小: 解码结果失败[%v]\n", err) return 0, err } } else { + log.CtxError(ctx, "查询文件夹大小: 未查询到结果\n") return 0, nil } if err = result.Err(); err != nil { + log.CtxError(ctx, "查询文件夹大小: 获取结果失败[%v]\n", err) return 0, err } return size.Size, nil - case errorx.Is(err, monc.ErrNotFound): + case errors.Is(err, monc.ErrNotFound): return 0, consts.ErrNotFound default: + log.CtxError(ctx, "查询文件夹大小: 发生异常[%v]\n", err) return 0, err } } @@ -392,6 +383,7 @@ func (m *MongoMapper) Update(ctx context.Context, data *File) (*mongo.UpdateResu if err != nil { data.Name = data.Name + "_" + strconv.FormatInt(time.Now().Unix(), 10) if res, err = m.conn.UpdateOne(ctx, key, bson.M{consts.ID: data.ID, consts.UserId: data.UserId}, bson.M{"$set": data}); err != nil { + log.CtxError(ctx, "更新文件信息: 发生异常[%v]\n", err) return res, err } } @@ -424,13 +416,13 @@ func (m *MongoMapper) Delete(ctx context.Context, id, userId string) (int64, err tracer := otel.GetTracerProvider().Tracer(trace.TraceName) _, span := tracer.Start(ctx, "mongo.Delete", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer)) defer span.End() - - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return 0, consts.ErrInvalidId - } + oid, _ := primitive.ObjectIDFromHex(id) key := prefixFileCacheKey + id resp, err := m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid, consts.UserId: userId}) + if err != nil { + log.CtxError(ctx, "删除文件信息: 发生异常[%v]\n", err) + return 0, err + } return resp, err } diff --git a/biz/infrastructure/mapper/sharefile/mongo.go b/biz/infrastructure/mapper/sharefile/mongo.go index 23cf098..f001f12 100644 --- a/biz/infrastructure/mapper/sharefile/mongo.go +++ b/biz/infrastructure/mapper/sharefile/mongo.go @@ -8,6 +8,8 @@ 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/pkg/errors" + "github.com/samber/lo" "github.com/zeromicro/go-zero/core/mr" "github.com/zeromicro/go-zero/core/stores/monc" "github.com/zeromicro/go-zero/core/trace" @@ -103,11 +105,12 @@ func (m *MongoMapper) Insert(ctx context.Context, data *ShareFile) (string, stri data.Key = RandomString(4) key := prefixPublicFileCacheKey + data.ID.Hex() - ID, err := m.conn.InsertOne(ctx, key, data) + _, err := m.conn.InsertOne(ctx, key, data) if err != nil { + log.CtxError(ctx, "创建文件分享链接: 发生异常[%v]\n", err) return "", "", err } - return ID.InsertedID.(primitive.ObjectID).Hex(), data.Key, err + return data.ID.Hex(), data.Key, err } func (m *MongoMapper) FindOne(ctx context.Context, id, key string) (*ShareFile, error) { @@ -115,18 +118,16 @@ func (m *MongoMapper) FindOne(ctx context.Context, id, key string) (*ShareFile, _, span := tracer.Start(ctx, "mongo.FindOne", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer)) defer span.End() - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidId - } + oid, _ := primitive.ObjectIDFromHex(id) var data ShareFile - err = m.conn.FindOneNoCache(ctx, &data, bson.M{"_id": oid, consts.Key: key}) + err := m.conn.FindOneNoCache(ctx, &data, bson.M{"_id": oid, consts.Key: key}) switch { case err == nil: return &data, nil case errorx.Is(err, monc.ErrNotFound): return nil, consts.ErrNotFound default: + log.CtxError(ctx, "mongo.FindOne error: %v", err) return nil, err } } @@ -144,21 +145,21 @@ func (m *MongoMapper) FindMany(ctx context.Context, fopts *ShareCodeOptions, pop } var data []*ShareFile - if err = m.conn.Find(ctx, &data, filter, &options.FindOptions{ + err = m.conn.Find(ctx, &data, filter, &options.FindOptions{ Sort: sort, Limit: popts.Limit, Skip: popts.Offset, - }); err != nil { - if errorx.Is(err, monc.ErrNotFound) { - return nil, consts.ErrNotFound - } + }) + switch { + case errors.Is(err, monc.ErrNotFound): + return nil, consts.ErrNotFound + case err != nil: + log.CtxError(ctx, "mongo.FindMany error: %v", err) return nil, err } if *popts.Backward { - for i := 0; i < len(data)/2; i++ { - data[i], data[len(data)-i-1] = data[len(data)-i-1], data[i] - } + lo.Reverse(data) } if len(data) > 0 { if err = p.StoreCursor(ctx, data[0], data[len(data)-1]); err != nil { @@ -180,7 +181,7 @@ func (m *MongoMapper) FindManyAndCount(ctx context.Context, fopts *ShareCodeOpti ctx, cancel := context.WithCancel(ctx) defer cancel() - err = mr.Finish(func() error { + if err = mr.Finish(func() error { data, err1 = m.FindMany(ctx, fopts, popts, sorter) if err1 != nil { return err1 @@ -192,9 +193,7 @@ func (m *MongoMapper) FindManyAndCount(ctx context.Context, fopts *ShareCodeOpti return err2 } return nil - }) - - if err != nil { + }); err != nil { return nil, 0, err } @@ -208,7 +207,11 @@ func (m *MongoMapper) Update(ctx context.Context, data *ShareFile) (*mongo.Updat key := prefixPublicFileCacheKey + data.ID.Hex() res, err := m.conn.UpdateOne(ctx, key, bson.M{"_id": data.ID}, bson.M{"$set": data}) - return res, err + if err != nil { + log.CtxError(ctx, "修改文件分享链接: 发生异常[%v]\n", err) + return res, err + } + return res, nil } func (m *MongoMapper) Delete(ctx context.Context, fopts *ShareCodeOptions) (int64, error) { diff --git a/go.mod b/go.mod index 523c6b8..74d2604 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/google/wire v0.5.0 github.com/kitex-contrib/obs-opentelemetry v0.2.5 github.com/mitchellh/mapstructure v1.1.2 + github.com/pkg/errors v0.9.1 github.com/samber/lo v1.38.1 github.com/zeromicro/go-zero v1.6.1 go.mongodb.org/mongo-driver v1.13.1 diff --git a/go.sum b/go.sum index 0630d54..a09a5ae 100644 --- a/go.sum +++ b/go.sum @@ -269,6 +269,7 @@ github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2 github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=