Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed May 14, 2023
1 parent bd603ee commit 636134b
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 9 deletions.
28 changes: 26 additions & 2 deletions upload/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type BaseClient struct {
RespData interface{}
Results Results
err error
uploadMaxSize int64 // 单位字节 (0 代表未设置,小于 0 代表不限制)
uploadMaxSize int64 // 单个文件最大尺寸 单位字节 (0 代表未设置,小于 0 代表不限制)
bodyMaxSize int64 // 限制整个提交body的尺寸
readBefore []ReadBeforeHook
chunkUpload *ChunkUpload
fieldMapping map[string]string
Expand All @@ -55,6 +56,7 @@ func (a *BaseClient) Reset() {
a.Results = nil
a.err = nil
a.uploadMaxSize = 0
a.bodyMaxSize = 0
a.chunkUpload = nil
a.fieldMapping = nil
}
Expand All @@ -64,6 +66,11 @@ func (a *BaseClient) SetUploadMaxSize(maxSize int64) Client {
return a
}

func (a *BaseClient) SetBodyMaxSize(maxSize int64) Client {
a.bodyMaxSize = maxSize
return a
}

func (a *BaseClient) SetReadBeforeHook(hooks ...ReadBeforeHook) Client {
a.readBefore = hooks
return a
Expand Down Expand Up @@ -109,8 +116,25 @@ func (a *BaseClient) ErrorString() string {
return ``
}

func (a *BaseClient) checkRequestBodySize() error {
if a.bodyMaxSize > 0 {
if a.bodyMaxSize > 0 && a.Context.Request().MaxSize() > int(a.bodyMaxSize) {
return fmt.Errorf(
`%w: %d>%d `,
ErrRequestBodyExceedsLimit,
a.Context.Request().MaxSize(),
a.bodyMaxSize,
)
}
}
return nil
}

func (a *BaseClient) Body() (file ReadCloserWithSize, err error) {
file, a.Data.FileName, err = Receive(a.Name(), a.Context)
if err := a.checkRequestBodySize(); err != nil {
return nil, err
}
file, a.Data.FileName, err = Receive(a.Context, a.Name())
if err != nil {
return
}
Expand Down
15 changes: 13 additions & 2 deletions upload/base_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func (a *BaseClient) Upload(opts ...OptionsSetter) Client {
uploadMaxSize = a.UploadMaxSize()
}
if uploadMaxSize > 0 && body.Size() > uploadMaxSize {
a.err = fmt.Errorf(`%w: %v`, ErrFileTooLarge, com.FormatBytes(uploadMaxSize))
a.err = fmt.Errorf(`%w: %v>%v`, ErrFileTooLarge,
com.FormatBytes(body.Size(), 2, true),
com.FormatBytes(uploadMaxSize, 2, true),
)
return a
}

Expand Down Expand Up @@ -118,6 +121,10 @@ func (a *BaseClient) BatchUpload(opts ...OptionsSetter) Client {
a.err = ErrInvalidContent
return a
}
if err := a.checkRequestBodySize(); err != nil {
a.err = err
return a
}
m := req.MultipartForm()
if m == nil || m.File == nil {
a.err = ErrInvalidContent
Expand All @@ -139,7 +146,11 @@ func (a *BaseClient) BatchUpload(opts ...OptionsSetter) Client {
for _, fileHdr := range files {
//for each fileheader, get a handle to the actual file
if uploadMaxSize > 0 && fileHdr.Size > uploadMaxSize {
a.err = fmt.Errorf(`%w: %v`, ErrFileTooLarge, com.FormatBytes(uploadMaxSize))
a.err = fmt.Errorf(
`%w: %v>%v`, ErrFileTooLarge,
com.FormatBytes(fileHdr.Size, 2, true),
com.FormatBytes(uploadMaxSize, 2, true),
)
return a
}

Expand Down
7 changes: 4 additions & 3 deletions upload/chunk_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ var (
ErrChunkUploadCompleted = errors.New("文件分片已经上传完成")

// Support
ErrChunkUnsupported = errors.New("不支持分片上传")
ErrFileSizeExceedsLimit = errors.New("文件尺寸超出限制")
ErrIncorrectSize = errors.New("文件尺寸不正确")
ErrChunkUnsupported = errors.New("不支持分片上传")
ErrFileSizeExceedsLimit = errors.New("文件尺寸超出限制")
ErrRequestBodyExceedsLimit = errors.New("提交内容尺寸超出限制")
ErrIncorrectSize = errors.New("文件尺寸不正确")

// Failure
ErrChunkHistoryOpenFailed = errors.New("打开历史分片文件失败")
Expand Down
1 change: 1 addition & 0 deletions upload/chunk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func uploadTestFile(t *testing.T, subdir string, readSeeker io.ReadSeeker, total
TempDir: tempDir,
SaveDir: saveDir,
DelayMerge: delayMerge,
//FileMaxBytes: 1048576 * 1, // 1M,
}
cu.OnBeforeMerge(func(ctx context.Context, info upload.ChunkInfor, filename string) error {
counters.Add(filename, 1)
Expand Down
11 changes: 11 additions & 0 deletions upload/chunk_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ func (c *ChunkUpload) Upload(r *http.Request, opts ...ChunkInfoOpter) (int64, er
if !c.IsSupported(info) {
return 0, ErrChunkUnsupported
}
if c.FileMaxBytes > 0 {
if r.ContentLength > int64(c.FileMaxBytes) {
return 0, fmt.Errorf(`%w: %d>%d `, ErrRequestBodyExceedsLimit, r.ContentLength, c.FileMaxBytes)
}
if r.MultipartForm == nil {
err := r.ParseMultipartForm(int64(c.FileMaxBytes))
if err != nil {
return 0, fmt.Errorf("上传文件错误: %w", err)
}
}
}
// 获取上传文件
upFile, fileHeader, err := r.FormFile(info.FormField)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions upload/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Client interface {
//初始化
Init(echo.Context, *Result)
SetUploadMaxSize(maxSize int64) Client
SetBodyMaxSize(maxSize int64) Client
SetReadBeforeHook(hooks ...ReadBeforeHook) Client
AddReadBeforeHook(hooks ...ReadBeforeHook) Client

Expand Down
2 changes: 1 addition & 1 deletion upload/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (w *wrapFileWithSize) Size() int64 {
return w.size
}

func Receive(name string, ctx echo.Context) (f ReadCloserWithSize, fileName string, err error) {
func Receive(ctx echo.Context, name string) (f ReadCloserWithSize, fileName string, err error) {
switch ctx.ResolveContentType() {
case "application/octet-stream":
val := ctx.Request().Header().Get("Content-Disposition")
Expand Down
4 changes: 3 additions & 1 deletion upload/utesting/chunk_testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,7 @@ func VerifyUploadedTestFile(t *testing.T, parentCU *upload.ChunkUpload, fileName
assert.NoError(t, err)
fi, err := os.Stat(savePath)
test.Eq(t, nil, err)
test.Eq(t, totalSize, fi.Size())
if err == nil {
test.Eq(t, totalSize, fi.Size())
}
}

0 comments on commit 636134b

Please sign in to comment.