Skip to content

Commit

Permalink
Fix/修改文件移动bug (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
Love-YeLin authored Mar 31, 2024
1 parent 3d26155 commit 1fef836
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
37 changes: 35 additions & 2 deletions biz/application/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/google/wire"
"github.com/samber/lo"
"github.com/zeromicro/go-zero/core/mr"
"github.com/zeromicro/go-zero/core/stores/monc"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -339,15 +340,15 @@ func (s *FileService) MoveFile(ctx context.Context, req *gencontent.MoveFileReq)
return err
}
for _, v := range data {
if _, err = s.FileMongoMapper.FindAndUpdate(sessionContext, &filemapper.File{ID: v.ID, Name: v.Name, Path: req.NewPath + v.Path[len(req.OldPath)-len(req.FileId)-1:]}); err != nil {
if _, err = s.FileMongoMapper.Update(sessionContext, &filemapper.File{ID: v.ID, Path: req.NewPath + v.Path[len(req.OldPath)-len(req.FileId)-1:]}); err != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "移动文件中产生错误[%v]: 回滚异常[%v]\n", err, rbErr)
}
return err
}
}
}
if _, err = s.FileMongoMapper.FindAndUpdate(sessionContext, &filemapper.File{ID: oid, Name: req.Name, Path: req.NewPath + "/" + oid.Hex(), FatherId: req.FatherId}); err != nil {
if _, err = s.FileMongoMapper.FindAndUpdate(sessionContext, &filemapper.File{ID: oid, Name: req.Name, Path: req.NewPath + "/" + oid.Hex(), FatherId: req.FatherId, IsDel: req.IsDel}); err != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "移动文件中产生错误[%v]: 回滚异常[%v]\n", err, rbErr)
}
Expand Down Expand Up @@ -513,11 +514,32 @@ func (s *FileService) RecoverRecycleBinFile(ctx context.Context, req *gencontent
}

ids := make([]string, 0, s.Config.InitialSliceLength)
updates := make(map[string]bson.M)
for _, file := range req.Files {
paths := strings.Split(file.Path, "/")
for i, id := range paths {
if i == 0 {
continue
} else if i == 1 {
var res *filemapper.File
var old filemapper.File
if res, err = s.FileMongoMapper.FindOne(ctx, id); err != nil {
return resp, err
}
if err = s.FileMongoMapper.GetConn().FindOneNoCache(ctx, &old, bson.M{consts.FatherId: res.FatherId, consts.Name: res.Name, consts.IsDel: int64(gencontent.Deletion_Deletion_notDel)}); err != nil {
if errors.Is(err, monc.ErrNotFound) {
ids = append(ids, id)
continue
}
return resp, err
}

s.FileMongoMapper.Rename(res)
updates[id] = bson.M{
"$set": bson.M{consts.IsDel: int64(gencontent.Deletion_Deletion_notDel), consts.Name: res.Name},
"$unset": bson.M{consts.DeletedAt: ""},
}
continue
}
ids = append(ids, id)
}
Expand All @@ -539,6 +561,17 @@ func (s *FileService) RecoverRecycleBinFile(ctx context.Context, req *gencontent
}
}
}

for key, v := range updates {
oid, _ := primitive.ObjectIDFromHex(key)
if _, err = s.FileMongoMapper.GetConn().UpdateOneNoCache(ctx, bson.M{consts.ID: oid}, v); err != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "恢复文件过程中产生错误[%v]: 回滚异常[%v]\n", err, rbErr)
}
return err
}
}

if _, err = s.FileMongoMapper.UpdateMany(sessionContext, ids, update); err != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "恢复文件过程中产生错误[%v]: 回滚异常[%v]\n", err, rbErr)
Expand Down
1 change: 0 additions & 1 deletion biz/infrastructure/mapper/file/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (e *EsMapper) Search(ctx context.Context, query []types.Query, fopts *Filte
}

func NewEsMapper(config *config.Config) IFileEsMapper {

esClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{
Username: config.Elasticsearch.Username,
Password: config.Elasticsearch.Password,
Expand Down
1 change: 1 addition & 0 deletions biz/infrastructure/mapper/file/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type (
UpdateMany(ctx context.Context, ids []string, update bson.M) (*mongo.UpdateResult, error)
Delete(ctx context.Context, id string) (int64, error)
DeleteMany(ctx context.Context, ids []string) (int64, error)
Rename(data *File)
GetConn() *monc.Model
StartClient() *mongo.Client
}
Expand Down
4 changes: 3 additions & 1 deletion 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-20240329084540-20ca37a52fea
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240331101048-fc52dc423cf2
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240331132549-840abd6c662c
github.com/bytedance/sonic v1.10.2
github.com/cloudwego/kitex v0.8.0
github.com/elastic/go-elasticsearch/v8 v8.11.1
Expand Down Expand Up @@ -42,6 +42,7 @@ require (
github.com/cloudwego/netpoll v0.5.1 // indirect
github.com/cloudwego/thriftgo v0.3.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/elastic/elastic-transport-go/v8 v8.3.0 // indirect
github.com/fatih/color v1.16.0 // indirect
Expand All @@ -61,6 +62,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/linvon/cuckoo-filter v0.4.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/CloudStriver/service-idl-gen-go v0.0.0-20240330084144-c7b91d79337e h1
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240330084144-c7b91d79337e/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240330085052-dce6d1369b83 h1:JaEav0yKK9zY5Jght0cipg2hpZ1Y7wjp/InW67g5Wy0=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240330085052-dce6d1369b83/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240331101048-fc52dc423cf2 h1:PeMBnOYC9SjELk2eejQA5uRz8Zdk1rAtahUZeBRpc80=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240331101048-fc52dc423cf2/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240331132549-840abd6c662c h1:6/W0sMS8fcv4J1J+q9wWPzsjt/gE1KkhEbW4oX4nygI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240331132549-840abd6c662c/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 Expand Up @@ -110,6 +110,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165 h1:BS21ZUJ/B5X2UVUbczfmdWH7GapPWAhxcMsDnjJTU1E=
github.com/dgryski/go-metro v0.0.0-20200812162917-85c65e2d0165/go.mod h1:c9O8+fpSOX1DM8cPNSkX/qsBWdkD4yd2dpciOWQjpBw=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
Expand Down Expand Up @@ -232,6 +234,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/linvon/cuckoo-filter v0.4.0 h1:vNlwcnvLOgmVJrhfE7gE4RYsxhrdW3LzLV7t27YsOuU=
github.com/linvon/cuckoo-filter v0.4.0/go.mod h1:L3YZEEsEkbEEWCA2r4sVk1dkrqz+TZ+uxGihtb6BwwI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
Expand Down

0 comments on commit 1fef836

Please sign in to comment.