2018-07-21 19:01:35 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-07-22 00:39:10 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
|
|
"io/ioutil"
|
|
|
|
"mime"
|
2018-07-21 19:01:35 +00:00
|
|
|
"net/http"
|
|
|
|
"path"
|
2018-07-27 09:10:10 +00:00
|
|
|
"strconv"
|
2018-07-27 09:10:42 +00:00
|
|
|
"strings"
|
2018-07-21 19:01:35 +00:00
|
|
|
)
|
|
|
|
|
2018-07-27 09:10:10 +00:00
|
|
|
func parseMultipart(r *http.Request) (
|
2018-12-22 21:11:07 +00:00
|
|
|
fileName string, data []byte, mimeType string, isGzipped bool, originalDataSize int, isChunkedFile bool, e error) {
|
2018-07-21 19:01:35 +00: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
data, e = ioutil.ReadAll(part)
|
|
|
|
if e != nil {
|
|
|
|
glog.V(0).Infoln("Reading Content [ERROR]", e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//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 != "" {
|
|
|
|
data2, fe2 := ioutil.ReadAll(part2)
|
|
|
|
if fe2 != nil {
|
|
|
|
glog.V(0).Infoln("Reading Content [ERROR]", fe2)
|
|
|
|
e = fe2
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//update
|
|
|
|
data = data2
|
|
|
|
fileName = path.Base(fName)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-22 21:11:07 +00:00
|
|
|
originalDataSize = len(data)
|
|
|
|
|
2018-07-27 09:10:10 +00:00
|
|
|
isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
|
|
|
|
|
2018-07-21 19:01:35 +00: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" {
|
2018-12-22 21:58:16 +00:00
|
|
|
if unzipped, e := operation.UnGzipData(data); e == nil {
|
|
|
|
originalDataSize = len(unzipped)
|
|
|
|
}
|
2018-07-21 19:01:35 +00:00
|
|
|
isGzipped = true
|
2018-12-22 23:05:31 +00:00
|
|
|
} else if operation.IsGzippable(ext, mtype, data) {
|
2019-04-06 07:10:52 +00:00
|
|
|
if compressedData, err := operation.GzipData(data); err == nil {
|
|
|
|
if len(data) > len(compressedData) {
|
|
|
|
data = compressedData
|
|
|
|
isGzipped = true
|
|
|
|
}
|
2018-07-21 19:01:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|