Skip to content

Commit

Permalink
Fix/修改cloudmind content.file.public (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Love-YeLin authored Feb 2, 2024
1 parent 02322a9 commit e957766
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 49 deletions.
53 changes: 31 additions & 22 deletions biz/application/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,14 @@ 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
var files []*filemapper.File
if files, err = s.FileMongoMapper.FindManyNotPagination(ctx, &filemapper.FilterOptions{OnlyFileIds: req.ShareFile.FileList, OnlyUserId: lo.ToPtr(req.ShareFile.UserId)}); err != nil {
return resp, err
}

if len(files) != len(req.ShareFile.FileList) { // 如果文件列表长度不一致, 说明文件列表存在问题 则返回错误
return resp, consts.ErrIllegalOperation
}
data := convertor.ShareFileToShareFileMapper(req.ShareFile)
data.CreateAt = time.Now()
if req.ShareFile.EffectiveTime >= 0 {
Expand Down Expand Up @@ -637,7 +645,7 @@ func (s *FileService) DeleteShareCode(ctx context.Context, req *gencontent.Delet
func (s *FileService) ParsingShareCode(ctx context.Context, req *gencontent.ParsingShareCodeReq) (resp *gencontent.ParsingShareCodeResp, err error) {
resp = new(gencontent.ParsingShareCodeResp)
var shareFile *sharefilemapper.ShareFile
if shareFile, err = s.ShareFileMongoMapper.FindOne(ctx, req.Code); err != nil {
if shareFile, err = s.ShareFileMongoMapper.FindOne(ctx, req.Code, req.Key); err != nil {
log.CtxError(ctx, "提取文件分享链接: 发生异常[%v]\n", err)
return resp, err
}
Expand All @@ -652,7 +660,6 @@ func (s *FileService) ParsingShareCode(ctx context.Context, req *gencontent.Pars
func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *gencontent.SaveFileToPrivateSpaceReq) (resp *gencontent.SaveFileToPrivateSpaceResp, err error) {
resp = new(gencontent.SaveFileToPrivateSpaceResp)
var (
path string
files []*filemapper.File
file *filemapper.File
objectfile *filemapper.File
Expand All @@ -665,7 +672,7 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten

if req.FileId == req.FatherId {
return resp, consts.ErrIllegalOperation
}
} // 如果目标文件和要保存的文件是同一个用户的,则返回错误

// 查看目标文件夹和要保存的文件
if files, err = s.FileMongoMapper.FindManyNotPagination(ctx, &filemapper.FilterOptions{
Expand All @@ -691,27 +698,26 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
if *objectfile.Size != consts.FolderSize {
return resp, consts.ErrFileIsNotDir
} // 如果目标文件不是文件夹,则返回错误
if file.UserId == objectfile.UserId {
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
}

tx := s.FileMongoMapper.StartClient()
err = tx.UseSession(ctx, func(sessionContext mongo.SessionContext) error { // 队列+协程
err = tx.UseSession(ctx, func(sessionContext mongo.SessionContext) error {
if err = sessionContext.StartTransaction(); err != nil {
return err
}
oid := primitive.NewObjectID()
resp.FileId = oid.Hex()
path = objectfile.Path + "/" + oid.Hex()
rootFile := &filemapper.File{
rootFile := &filemapper.File{ // 创建根文件
ID: oid,
UserId: req.UserId,
Name: file.Name,
Type: file.Type,
Path: path,
Path: objectfile.Path,
FatherId: req.FatherId,
Size: file.Size,
FileMd5: file.FileMd5,
Expand All @@ -720,15 +726,15 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
UpdateAt: time.Now(),
}

if *file.Size == consts.FolderSize {
if *file.Size == consts.FolderSize { // 若是文件夹,开始根据原文件夹层层创建
err = mr.Finish(func() error {
_, err1 = s.FileMongoMapper.Insert(sessionContext, rootFile)
_, err1 = s.FileMongoMapper.Insert(sessionContext, rootFile) // 先插入根文件
return err1
}, func() error {
var front kv
var sonFile *filemapper.File
queue := make([]kv, 0, 100)
queue = append(queue, kv{id: file.ID.Hex(), path: rootFile.Path})
queue = append(queue, kv{id: file.ID.Hex(), path: objectfile.Path + "/" + oid.Hex()})
for len(queue) > 0 {
front = queue[0]
queue = queue[1:]
Expand All @@ -747,13 +753,12 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
}
for _, v := range data {
oid = primitive.NewObjectID()
path = front.path + "/" + oid.Hex()
sonFile = &filemapper.File{
ID: oid,
UserId: req.UserId,
Name: v.Name,
Type: v.Type,
Path: path,
Path: front.path,
FatherId: front.id,
Size: v.Size,
FileMd5: v.FileMd5,
Expand All @@ -765,7 +770,7 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
return err2
}
if *v.Size == consts.FolderSize {
queue = append(queue, kv{id: v.ID.Hex(), path: sonFile.Path})
queue = append(queue, kv{id: v.ID.Hex(), path: front.path + "/" + oid.Hex()})
}
}
}
Expand Down Expand Up @@ -821,10 +826,12 @@ func (s *FileService) AddFileToPublicSpace(ctx context.Context, req *gencontent.
}
for _, v := range data {
if _, err = s.FileMongoMapper.Update(sessionContext, &filemapper.File{
ID: v.ID,
UserId: v.UserId,
Zone: req.File.Zone,
SubZone: req.File.SubZone,
ID: v.ID,
UserId: v.UserId,
Zone: req.File.Zone,
SubZone: req.File.SubZone,
Description: req.File.Description,
Labels: req.File.Labels,
}); err != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "上传文件到社区过程中产生错误[%v]: 回滚异常[%v]\n", err, rbErr)
Expand All @@ -834,10 +841,12 @@ func (s *FileService) AddFileToPublicSpace(ctx context.Context, req *gencontent.
}
}
if _, err = s.FileMongoMapper.Update(sessionContext, &filemapper.File{
ID: oid,
UserId: req.File.UserId,
Zone: req.File.Zone,
SubZone: req.File.SubZone,
ID: oid,
UserId: req.File.UserId,
Zone: req.File.Zone,
SubZone: req.File.SubZone,
Description: req.File.Description,
Labels: req.File.Labels,
}); err != nil {
if rbErr := sessionContext.AbortTransaction(sessionContext); rbErr != nil {
log.CtxError(ctx, "上传文件到社区过程中产生错误[%v]: 回滚异常[%v]\n", err, rbErr)
Expand Down
19 changes: 10 additions & 9 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package consts

const (
ID = "_id"
UserId = "userId"
TypeString = "typeString"
Name = "name"
ProductId = "productId"
ProductName = "productName"
Type = "type"
Path = "path"
FatherId = "fatherId"
ID = "_id"
UserId = "userId"
TypeString = "typeString"
Name = "name"
ProductId = "productId"
ProductName = "productName"
Type = "type"
Path = "path"
FatherId = "fatherId"
Size = "size"
FileMd5 = "fileMd5"
IsDel = "isDel"
SpaceSize = "spaceSize"
Tags = "tags"
Zone = "zone"
Key = "key"
SubZone = "subZone"
Status = "status"
Title = "title"
Expand Down
5 changes: 2 additions & 3 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func FileMapperToFile(data *file.File) *gencontent.FileInfo {
Path: data.Path,
FatherId: data.FatherId,
SpaceSize: *data.Size,
Md5: data.FileMd5,
Zone: data.Zone,
SubZone: data.SubZone,
Description: data.Description,
Expand Down Expand Up @@ -538,7 +537,7 @@ func CouponToCouponMapper(in *gencontent.Coupon) *couponmapper.Coupon {
UserId: in.UserId,
Name: in.Name,
Status: in.Status,
Description: in.Desciption,
Description: in.Description,
Type: in.ProductType,
LowSumPrice: in.LowSumPrice,
ProductType: in.ProductType,
Expand All @@ -558,7 +557,7 @@ func CouponMapperToCoupon(in *couponmapper.Coupon) *gencontent.Coupon {
CreateTime: in.CreateAt.UnixMilli(),
ExpireTime: in.ExpireTime,
Name: in.Name,
Desciption: in.Description,
Description: in.Description,
LowSumPrice: in.LowSumPrice,
ProductType: in.ProductType,
Discount: in.Discount,
Expand Down
8 changes: 5 additions & 3 deletions biz/infrastructure/mapper/file/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package file
import (
"context"
errorx "errors"
"fmt"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts"
"github.com/CloudStriver/go-pkg/utils/pagination"
Expand Down Expand Up @@ -218,9 +219,9 @@ func (m *MongoMapper) FindFolderSize(ctx context.Context, path string) (int64, e
pipeline := mongo.Pipeline{
{
{"$match", bson.M{
consts.Path: bson.M{"$regex": "^" + path},
consts.SpaceSize: bson.M{"$ne": consts.FolderSize},
consts.IsDel: gencontent.IsDel_Is_no,
consts.Path: bson.M{"$regex": "^" + path},
consts.Size: bson.M{"$ne": consts.FolderSize},
consts.IsDel: gencontent.IsDel_Is_no,
}},
},
{
Expand All @@ -239,6 +240,7 @@ func (m *MongoMapper) FindFolderSize(ctx context.Context, path string) (int64, e
if err != nil {
return 0, err
}
fmt.Printf("[%v]\n", size)
} else {
return 0, nil
}
Expand Down
7 changes: 3 additions & 4 deletions biz/infrastructure/mapper/sharefile/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type (
IMongoMapper interface {
Count(ctx context.Context, filter *ShareCodeOptions) (int64, error)
Insert(ctx context.Context, data *ShareFile) (string, string, error)
FindOne(ctx context.Context, id string) (*ShareFile, error)
FindOne(ctx context.Context, id, key string) (*ShareFile, error)
FindMany(ctx context.Context, fopts *ShareCodeOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*ShareFile, error)
FindManyAndCount(ctx context.Context, fopts *ShareCodeOptions, popts *pagination.PaginationOptions, sorter mongop.MongoCursor) ([]*ShareFile, int64, error)
Update(ctx context.Context, data *ShareFile) (*mongo.UpdateResult, error)
Expand Down Expand Up @@ -110,7 +110,7 @@ func (m *MongoMapper) Insert(ctx context.Context, data *ShareFile) (string, stri
return ID.InsertedID.(primitive.ObjectID).Hex(), data.Key, err
}

func (m *MongoMapper) FindOne(ctx context.Context, id string) (*ShareFile, error) {
func (m *MongoMapper) FindOne(ctx context.Context, id, key string) (*ShareFile, error) {
tracer := otel.GetTracerProvider().Tracer(trace.TraceName)
_, span := tracer.Start(ctx, "mongo.FindOne", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer))
defer span.End()
Expand All @@ -120,8 +120,7 @@ func (m *MongoMapper) FindOne(ctx context.Context, id string) (*ShareFile, error
return nil, consts.ErrInvalidId
}
var data ShareFile
key := prefixPublicFileCacheKey + id
err = m.conn.FindOne(ctx, key, &data, bson.M{"_id": oid})
err = m.conn.FindOneNoCache(ctx, &data, bson.M{"_id": oid, consts.Key: key})
switch {
case err == nil:
return &data, nil
Expand Down
5 changes: 1 addition & 4 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-20240202055044-f6076cb1d360
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202114307-d5edc26b91e1
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,9 +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 @@ -6,10 +6,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d h1:c5M3637HJJN+pD3erToZAw0vutEMLmC+5I63szm5PYU=
github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d/go.mod h1:Oj6+J8ixQxqNunSpl9v64q6tAQ83wY/rRFI+3rhHVCk=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240201091924-a3cefad75be4 h1:ePM5sgK+a9KIrIz0ceSzcJ+iW3ApSfFMAPM+RW8+x54=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240201091924-a3cefad75be4/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202055044-f6076cb1d360 h1:sJXZXhB5cCkZC1P8FUilJ9F22xARfFH/ZdrE9ZbLhvY=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202055044-f6076cb1d360/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202114307-d5edc26b91e1 h1:9ZenrqB/o8sJn0QWpdBPMvCBbhfZ1e1fb4QqeNrgVdE=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240202114307-d5edc26b91e1/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 e957766

Please sign in to comment.