2019-04-19 12:43:36 +08:00
|
|
|
package needle
|
2018-07-22 03:01:35 +08:00
|
|
|
|
|
|
|
import (
|
2020-01-03 16:37:24 +08:00
|
|
|
"fmt"
|
2019-04-16 12:43:28 +08:00
|
|
|
"io"
|
2018-07-22 08:39:10 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"mime"
|
2018-07-22 03:01:35 +08:00
|
|
|
"net/http"
|
|
|
|
"path"
|
2018-07-27 17:10:10 +08:00
|
|
|
"strconv"
|
2018-07-27 17:10:42 +08:00
|
|
|
"strings"
|
2020-01-03 16:37:24 +08:00
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-07-22 03:01:35 +08:00
|
|
|
)
|
|
|
|
|
2020-01-03 16:37:24 +08:00
|
|
|
func parseMultipart(r *http.Request, sizeLimit int64) (
|
2018-12-23 05:11:07 +08:00
|
|
|
fileName string, data []byte, mimeType string, isGzipped bool, originalDataSize int, isChunkedFile bool, e error) {
|
2019-04-16 12:43:28 +08:00
|
|
|
defer func() {
|
|
|
|
if e != nil && r.Body != nil {
|
|
|
|
io.Copy(ioutil.Discard, r.Body)
|
|
|
|
r.Body.Close()
|
|
|
|
}
|
|
|
|
}()
|
2018-07-22 03:01:35 +08:00
|
|
|
form, fe := r.MultipartReader()
|
|
|
|
if fe != nil {
|
|
|
|
glog.V(0).Infoln("MultipartReader [ERROR]", fe)
|
|
|
|
e = fe
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//first multi-part item
|
|
|
|
part, fe := form.NextPart()
|
|
|
|
if fe != nil {
|
|
|
|
glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
|
|
|
|
e = fe
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName = part.FileName()
|
|
|
|
if fileName != "" {
|
|
|
|
fileName = path.Base(fileName)
|
|
|
|
}
|
|
|
|
|
2020-01-03 16:37:24 +08:00
|
|
|
data, e = ioutil.ReadAll(io.LimitReader(part, sizeLimit+1))
|
2018-07-22 03:01:35 +08:00
|
|
|
if e != nil {
|
|
|
|
glog.V(0).Infoln("Reading Content [ERROR]", e)
|
|
|
|
return
|
|
|
|
}
|
2020-01-03 16:37:24 +08:00
|
|
|
if len(data) == int(sizeLimit)+1 {
|
|
|
|
e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
|
|
|
|
return
|
|
|
|
}
|
2018-07-22 03:01:35 +08:00
|
|
|
|
|
|
|
//if the filename is empty string, do a search on the other multi-part items
|
|
|
|
for fileName == "" {
|
|
|
|
part2, fe := form.NextPart()
|
|
|
|
if fe != nil {
|
|
|
|
break // no more or on error, just safely break
|
|
|
|
}
|
|
|
|
|
|
|
|
fName := part2.FileName()
|
|
|
|
|
|
|
|
//found the first <file type> multi-part has filename
|
|
|
|
if fName != "" {
|
2020-01-03 16:37:24 +08:00
|
|
|
data2, fe2 := ioutil.ReadAll(io.LimitReader(part2, sizeLimit+1))
|
2018-07-22 03:01:35 +08:00
|
|
|
if fe2 != nil {
|
|
|
|
glog.V(0).Infoln("Reading Content [ERROR]", fe2)
|
|
|
|
e = fe2
|
|
|
|
return
|
|
|
|
}
|
2020-01-03 16:37:24 +08:00
|
|
|
if len(data) == int(sizeLimit)+1 {
|
|
|
|
e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
|
|
|
|
return
|
|
|
|
}
|
2018-07-22 03:01:35 +08:00
|
|
|
|
|
|
|
//update
|
|
|
|
data = data2
|
|
|
|
fileName = path.Base(fName)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-23 05:11:07 +08:00
|
|
|
originalDataSize = len(data)
|
|
|
|
|
2018-07-27 17:10:10 +08:00
|
|
|
isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
|
|
|
|
|
2018-07-22 03:01:35 +08:00
|
|
|
if !isChunkedFile {
|
|
|
|
|
|
|
|
dotIndex := strings.LastIndex(fileName, ".")
|
|
|
|
ext, mtype := "", ""
|
|
|
|
if dotIndex > 0 {
|
|
|
|
ext = strings.ToLower(fileName[dotIndex:])
|
|
|
|
mtype = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
contentType := part.Header.Get("Content-Type")
|
|
|
|
if contentType != "" && mtype != contentType {
|
|
|
|
mimeType = contentType //only return mime type if not deductable
|
|
|
|
mtype = contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
if part.Header.Get("Content-Encoding") == "gzip" {
|
2019-04-19 12:43:36 +08:00
|
|
|
if unzipped, e := util.UnGzipData(data); e == nil {
|
2018-12-23 05:58:16 +08:00
|
|
|
originalDataSize = len(unzipped)
|
|
|
|
}
|
2018-07-22 03:01:35 +08:00
|
|
|
isGzipped = true
|
2019-04-19 12:43:36 +08:00
|
|
|
} else if util.IsGzippable(ext, mtype, data) {
|
|
|
|
if compressedData, err := util.GzipData(data); err == nil {
|
2019-04-06 15:10:52 +08:00
|
|
|
if len(data) > len(compressedData) {
|
|
|
|
data = compressedData
|
|
|
|
isGzipped = true
|
|
|
|
}
|
2018-07-22 03:01:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|