add object pool for SaveToFile
This commit is contained in:
parent
4c523830a7
commit
58b3f37e9e
@ -28,6 +28,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/beego/beego/v2/server/web/context"
|
"github.com/beego/beego/v2/server/web/context"
|
||||||
"github.com/beego/beego/v2/server/web/context/param"
|
"github.com/beego/beego/v2/server/web/context/param"
|
||||||
@ -39,8 +40,21 @@ var (
|
|||||||
ErrAbort = errors.New("user stop run")
|
ErrAbort = errors.New("user stop run")
|
||||||
// GlobalControllerRouter store comments with controller. pkgpath+controller:comments
|
// GlobalControllerRouter store comments with controller. pkgpath+controller:comments
|
||||||
GlobalControllerRouter = make(map[string][]ControllerComments)
|
GlobalControllerRouter = make(map[string][]ControllerComments)
|
||||||
|
copyBufferPool sync.Pool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
bytePerKb = 1024
|
||||||
|
copyBufferKb = 32
|
||||||
|
filePerm = 0666
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
copyBufferPool.New = func() interface{} {
|
||||||
|
return make([]byte, bytePerKb*copyBufferKb)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ControllerFilter store the filter for controller
|
// ControllerFilter store the filter for controller
|
||||||
type ControllerFilter struct {
|
type ControllerFilter struct {
|
||||||
Pattern string
|
Pattern string
|
||||||
@ -605,20 +619,9 @@ func (c *Controller) GetFiles(key string) ([]*multipart.FileHeader, error) {
|
|||||||
// SaveToFile saves uploaded file to new path.
|
// SaveToFile saves uploaded file to new path.
|
||||||
// it only operates the first one of mutil-upload form file field.
|
// it only operates the first one of mutil-upload form file field.
|
||||||
func (c *Controller) SaveToFile(fromFile, toFile string) error {
|
func (c *Controller) SaveToFile(fromFile, toFile string) error {
|
||||||
file, _, err := c.Ctx.Request.FormFile(fromFile)
|
buf := copyBufferPool.Get().([]byte)
|
||||||
if err != nil {
|
defer copyBufferPool.Put(buf)
|
||||||
return err
|
return c.SaveToFileWithBuffer(fromFile, toFile, buf)
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
f, err := os.OpenFile(toFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
_, err = io.Copy(f, file)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type onlyWriter struct {
|
type onlyWriter struct {
|
||||||
@ -632,10 +635,11 @@ func (c *Controller) SaveToFileWithBuffer(fromFile string, toFile string, buf []
|
|||||||
}
|
}
|
||||||
defer src.Close()
|
defer src.Close()
|
||||||
|
|
||||||
dst, err := os.OpenFile(toFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
dst, err := os.OpenFile(toFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer dst.Close()
|
||||||
|
|
||||||
_, err = io.CopyBuffer(onlyWriter{dst}, src, buf)
|
_, err = io.CopyBuffer(onlyWriter{dst}, src, buf)
|
||||||
return err
|
return err
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user