Skip to content

Commit 1d7a4d9

Browse files
author
licong
committed
limit http memnory
1 parent 86b652c commit 1d7a4d9

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

fs.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"errors"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"log"
1514
"mime"
1615
"mime/multipart"
@@ -842,7 +841,7 @@ func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
842841
// upload file
843842
func uploadFile(w ResponseWriter, r *Request, fs FileSystem, name string) {
844843
log.Printf("upload file to path %s\n", name)
845-
r.ParseMultipartForm(1024 * 1024 * 1024 * 4)
844+
r.ParseMultipartForm(32 << 20)
846845
file, handler, err := r.FormFile("file")
847846
if err != nil {
848847
log.Printf("read upload form failed %s\n", err)
@@ -855,12 +854,17 @@ func uploadFile(w ResponseWriter, r *Request, fs FileSystem, name string) {
855854
return
856855
}
857856
defer file.Close()
858-
fileBytes, err := ioutil.ReadAll(file)
859-
if err != nil {
860-
fmt.Println(err)
857+
var buffer = make([]byte, 10*1024*1024)
858+
for {
859+
n, err := file.Read(buffer)
860+
if n > 0 {
861+
fh.Write(buffer[:n])
862+
}
863+
if err == io.EOF {
864+
break
865+
}
861866
}
862867
defer fh.Close()
863-
fh.Write(fileBytes)
864868
// write this byte array to our temporary file
865869
// return that we have successfully uploaded our file!
866870
fmt.Fprintf(w, "Successfully Uploaded File\n")

0 commit comments

Comments
 (0)