optimize upload attachment logic

r/paopao-ce
Michael Li 12 months ago
parent 0d37a5f3fd
commit ec931c6183
No known key found for this signature in database

@ -22,6 +22,7 @@ import (
"github.com/rocboss/paopao-ce/internal/model/web" "github.com/rocboss/paopao-ce/internal/model/web"
"github.com/rocboss/paopao-ce/internal/servants/base" "github.com/rocboss/paopao-ce/internal/servants/base"
"github.com/rocboss/paopao-ce/internal/servants/chain" "github.com/rocboss/paopao-ce/internal/servants/chain"
"github.com/rocboss/paopao-ce/pkg/types"
"github.com/rocboss/paopao-ce/pkg/utils" "github.com/rocboss/paopao-ce/pkg/utils"
"github.com/rocboss/paopao-ce/pkg/xerror" "github.com/rocboss/paopao-ce/pkg/xerror"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -141,7 +142,11 @@ func (s *privSrv) UploadAttachment(req *web.UploadAttachmentReq) (*web.UploadAtt
// 生成随机路径 // 生成随机路径
randomPath := uuid.Must(uuid.NewV4()).String() randomPath := uuid.Must(uuid.NewV4()).String()
ossSavePath := req.UploadType + "/" + generatePath(randomPath[:8]) + "/" + randomPath[9:] + req.FileExt ossSavePath := req.UploadType + "/" + generatePath(randomPath[:8]) + "/" + randomPath[9:] + req.FileExt
objectUrl, err := s.oss.PutObject(ossSavePath, req.File, req.FileSize, req.ContentType, false) // NOTE: 注意这里将req.File Wrap到一个io.Reader的实例对象中是为了避免下游接口去主动调Closereq.File本身是实现了
// io.Closer接口的有的下游接口会断言传参是否实现了io.Closer接口如果实现了会主动去调我们这里因为下文中可能还要继续
// 使用req.File所以应避免下游Close否则会出现潜在的bug比如这里的场景就是传一个超大的图片(>10MB)可能就会触发bug了。
data := types.PureReader(req.File)
objectUrl, err := s.oss.PutObject(ossSavePath, data, req.FileSize, req.ContentType, false)
if err != nil { if err != nil {
logrus.Errorf("oss.putObject err: %s", err) logrus.Errorf("oss.putObject err: %s", err)
return nil, web.ErrFileUploadFailed return nil, web.ErrFileUploadFailed

@ -0,0 +1,24 @@
// Copyright 2024 ROC. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package types
import (
"io"
)
type readerWrap struct {
r io.Reader
}
func (rw *readerWrap) Read(p []byte) (n int, err error) {
return rw.r.Read(p)
}
// PureReader wrap a pure io.Reader object
func PureReader(r io.Reader) io.Reader {
return &readerWrap{
r: r,
}
}
Loading…
Cancel
Save