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