fix(upload): memory leak on form upload as task (close #5185)

This commit is contained in:
Andy Hsu 2023-09-07 15:51:52 +08:00
parent cd2f8077fa
commit 3504f017b9

View File

@ -1,13 +1,9 @@
package handles package handles
import ( import (
"github.com/alist-org/alist/v3/internal/conf"
"github.com/alist-org/alist/v3/pkg/utils/random"
"io" "io"
"net/url" "net/url"
"os"
stdpath "path" stdpath "path"
"reflect"
"strconv" "strconv"
"time" "time"
@ -102,28 +98,12 @@ func FsForm(c *gin.Context) {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
tmpFile, tmpInSys := "", "" f, err := file.Open()
fv := reflect.ValueOf(*file)
tmpInSys = fv.FieldByName("tmpfile").String()
var f io.Reader
var osFile *os.File
if len(tmpInSys) > 0 {
tmpFile = conf.Conf.TempDir + "file-" + random.String(8)
err = os.Rename(tmpInSys, tmpFile)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
osFile, err = os.Open(tmpFile)
f = osFile
} else {
f, err = file.Open()
}
if err != nil { if err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
defer f.Close()
dir, name := stdpath.Split(path) dir, name := stdpath.Split(path)
s := stream.FileStream{ s := stream.FileStream{
Obj: &model.Object{ Obj: &model.Object{
@ -135,21 +115,19 @@ func FsForm(c *gin.Context) {
Mimetype: file.Header.Get("Content-Type"), Mimetype: file.Header.Get("Content-Type"),
WebPutAsTask: asTask, WebPutAsTask: asTask,
} }
ss, err := stream.NewSeekableStream(s, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
if osFile != nil {
ss.SetTmpFile(osFile)
}
if asTask { if asTask {
err = fs.PutAsTask(dir, ss) s.Reader = struct {
io.Reader
}{f}
err = fs.PutAsTask(dir, &s)
} else { } else {
ss, err := stream.NewSeekableStream(s, nil)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
err = fs.PutDirectly(c, dir, ss, true) err = fs.PutDirectly(c, dir, ss, true)
} }
if err != nil { if err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return