Skip to content

Commit

Permalink
Fix/修改cloudmind content.file.public.savefile (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Love-YeLin authored Feb 6, 2024
1 parent 1485682 commit a69fb0c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 45 deletions.
4 changes: 0 additions & 4 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ func (s *ContentServerImpl) DeleteFile(ctx context.Context, req *content.DeleteF
return s.FileService.DeleteFile(ctx, req)
}

func (s *ContentServerImpl) GetFileCount(ctx context.Context, req *content.GetFileCountReq) (*content.GetFileCountResp, error) {
return s.FileService.GetFileCount(ctx, req)
}

func (s *ContentServerImpl) UpdateFile(ctx context.Context, req *content.UpdateFileReq) (*content.UpdateFileResp, error) {
return s.FileService.UpdateFile(ctx, req)
}
Expand Down
57 changes: 25 additions & 32 deletions biz/application/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type IFileService interface {
GetFileIsExist(ctx context.Context, req *gencontent.GetFileIsExistReq) (resp *gencontent.GetFileIsExistResp, err error)
GetFile(ctx context.Context, req *gencontent.GetFileReq) (resp *gencontent.GetFileResp, err error)
GetFileList(ctx context.Context, req *gencontent.GetFileListReq) (resp *gencontent.GetFileListResp, err error)
GetFileCount(ctx context.Context, req *gencontent.GetFileCountReq) (resp *gencontent.GetFileCountResp, err error)
GetFileBySharingCode(ctx context.Context, req *gencontent.GetFileBySharingCodeReq) (resp *gencontent.GetFileBySharingCodeResp, err error)
GetFolderSize(ctx context.Context, path string) (resp *gencontent.GetFolderSizeResp, err error)
CreateFile(ctx context.Context, req *gencontent.CreateFileReq) (resp *gencontent.CreateFileResp, err error)
Expand Down Expand Up @@ -171,18 +170,6 @@ func (s *FileService) GetFileList(ctx context.Context, req *gencontent.GetFileLi
return resp, nil
}

func (s *FileService) GetFileCount(ctx context.Context, req *gencontent.GetFileCountReq) (resp *gencontent.GetFileCountResp, err error) {
resp = new(gencontent.GetFileCountResp)
var total int64
filter := convertor.FileFilterOptionsToFilterOptions(req.FilterOptions)
if total, err = s.FileMongoMapper.Count(ctx, filter); err != nil {
log.CtxError(ctx, "查询文件总数: 发生异常[%v]\n", err)
return resp, err
}
resp.Count = total
return resp, nil
}

func (s *FileService) CheckShareFile(ctx context.Context, shareFiles []*filemapper.File, fileId *string) (*gencontent.FileInfo, bool, error) {
var (
err error
Expand Down Expand Up @@ -685,8 +672,6 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
resp = new(gencontent.SaveFileToPrivateSpaceResp)
var (
err1 error
id string
session mongo.Session
files []*filemapper.File
file *filemapper.File
objectfile *filemapper.File
Expand Down Expand Up @@ -732,7 +717,7 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
} // 如果目标文件不是文件夹,则返回错误
if file.UserId == objectfile.UserId || req.UserId != objectfile.UserId {
return resp, consts.ErrIllegalOperation
} // 如果目标文件和要保存的文件是同一个用户的,则返回错误
} // 如果目标文件和要保存的文件是同一个用户的,则返回错误
if req.DocumentType == gencontent.DocumentType_DocumentType_public && (objectfile.Zone == "" || objectfile.SubZone == "") { // 如果要保存的文件不是社区文件,则返回错误
return resp, consts.ErrIllegalOperation
}
Expand All @@ -754,20 +739,20 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
if resp.FileId, err1 = s.FileMongoMapper.Insert(sessionContext, rootFile); err1 != nil {
if resp.FileId, err1 = s.FileMongoMapper.FindAndInsert(sessionContext, rootFile); err1 != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "保存文件中产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
}
return err1
}
if *file.Size == consts.FolderSize { // 若是文件夹,开始根据原文件夹层层创建
var front kv
var sonFile *filemapper.File
queue := make([]kv, 0, 20)
queue = append(queue, kv{id: file.ID.Hex(), path: objectfile.Path + "/" + resp.FileId})
for len(queue) > 0 {
front = queue[0]
queue = queue[1:]
var ids []string
var data []*filemapper.File
var filter bson.M
if req.DocumentType == gencontent.DocumentType_DocumentType_public {
Expand All @@ -787,27 +772,36 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
return rbErr
}
}
for _, v := range data {
sonFile = &filemapper.File{

if len(data) <= 0 {
continue
}

sonFiles := lo.Map(data, func(item *filemapper.File, _ int) *filemapper.File {
return &filemapper.File{
UserId: req.UserId,
Name: v.Name,
Type: v.Type,
Name: item.Name,
Type: item.Type,
Path: front.path,
FatherId: front.path[len(front.path)-len(front.id):],
Size: v.Size,
FileMd5: v.FileMd5,
IsDel: int64(gencontent.IsDel_Is_no),
Size: item.Size,
FileMd5: item.FileMd5,
IsDel: consts.NotDel,
CreateAt: time.Now(),
UpdateAt: time.Now(),
}
if id, err1 = s.FileMongoMapper.Insert(sessionContext, sonFile); err1 != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "保存文件中产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
}
return err1
})

if ids, err1 = s.FileMongoMapper.FindAndInsertMany(sessionContext, sonFiles); err1 != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "保存文件中产生错误[%v]: 回滚异常[%v]\n", err1, rbErr)
}
return err1
}

for i, v := range data {
if *v.Size == consts.FolderSize {
queue = append(queue, kv{id: v.ID.Hex(), path: front.path + "/" + id})
queue = append(queue, kv{id: v.ID.Hex(), path: front.path + "/" + ids[i]})
}
}
}
Expand All @@ -819,7 +813,6 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
}
return nil
})
session.EndSession(ctx)

return resp, err
}
Expand Down
63 changes: 63 additions & 0 deletions biz/infrastructure/mapper/file/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type (
FindFileIsExist(ctx context.Context, md5 string) (bool, error)
Count(ctx context.Context, filter *FilterOptions) (int64, error)
Insert(ctx context.Context, data *File) (string, error)
FindAndInsert(ctx context.Context, data *File) (string, error)
FindAndInsertMany(ctx context.Context, data []*File) ([]string, error)
FindOne(ctx context.Context, fopts *FilterOptions) (*File, error)
FindMany(ctx context.Context, fopts *FilterOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*File, error)
FindManyNotPagination(ctx context.Context, fopts *FilterOptions) ([]*File, error)
Expand Down Expand Up @@ -120,6 +122,67 @@ func (m *MongoMapper) FindManyNotPagination(ctx context.Context, fopts *FilterOp
return data, nil
}

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

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, consts.ErrNotFound) {
return m.Insert(ctx, data)
}
return "", err
}

// 如果文件已存在,修改文件名
data.Name = data.Name + "_" + strconv.FormatInt(time.Now().Unix(), 10)
return m.Insert(ctx, data)
}

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

mp := make(map[string]int)
for i := 0; i < len(data); i++ {
if data[i].ID.IsZero() {
data[i].ID = primitive.NewObjectID()
data[i].CreateAt = time.Now()
data[i].UpdateAt = time.Now()
}
mp[data[i].ID.Hex()] = i
data[i].Path = data[i].Path + "/" + data[i].ID.Hex()
}

if err := mr.Finish(lo.Map(data, func(item *File, _ int) func() error {
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) {
return nil
} else {
return err
}
}
// 如果文件已存在,修改文件名
item.Name = item.Name + "_" + strconv.FormatInt(time.Now().Unix(), 10)
return nil
}
})...); err != nil {
return nil, err
}

dataAny := lo.Map(data, func(item *File, _ int) any { return item })
res, err := m.conn.InsertMany(ctx, dataAny)
ids := make([]string, len(res.InsertedIDs))
for _, v := range res.InsertedIDs {
ids[mp[v.(primitive.ObjectID).Hex()]] = v.(primitive.ObjectID).Hex()
}
return ids, err
}

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))
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206115825-723f79e23a9a
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206130055-0d56397126c5
github.com/bytedance/sonic v1.10.2
github.com/cloudwego/kitex v0.8.0
github.com/elastic/go-elasticsearch/v8 v8.11.1
Expand All @@ -19,10 +19,6 @@ require (
google.golang.org/grpc v1.60.1
)

//replace (
// github.com/CloudStriver/service-idl-gen-go => ../service-idl-gen-go
//)

require (
github.com/apache/thrift v0.16.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202114307-d5edc26b91e1 h1
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202114307-d5edc26b91e1/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206035429-8096c700ad87 h1:OGZxm1tDqseEZkbS+9gDu9R+T5KSuezqNV67ONxsgwg=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206035429-8096c700ad87/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206085239-35c6846693f2 h1:uL459qhf+/YtP3E400LNuyfqY6TTJcbA1R51sBatD+E=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206085239-35c6846693f2/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206115825-723f79e23a9a h1:evyPD6LqMgpeS3pV7ocRSwEip4oscUGpYo1R4rRCHaI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206115825-723f79e23a9a/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206130055-0d56397126c5 h1:0dYU5eoQG10ijYOJEUrXmaETPhguipbELFK13PcYIQA=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240206130055-0d56397126c5/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
Expand Down

0 comments on commit a69fb0c

Please sign in to comment.