mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactor: separate multipart parsing file
This commit is contained in:
parent
13e5541e17
commit
feb8eeb830
|
@ -3,17 +3,12 @@ package storage
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"mime"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
||||||
"github.com/chrislusf/seaweedfs/weed/images"
|
"github.com/chrislusf/seaweedfs/weed/images"
|
||||||
"github.com/chrislusf/seaweedfs/weed/operation"
|
|
||||||
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
. "github.com/chrislusf/seaweedfs/weed/storage/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,91 +57,11 @@ func ParseUpload(r *http.Request) (
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
form, fe := r.MultipartReader()
|
fileName, data, mimeType, isGzipped, isChunkedFile, e = parseMultipart(r)
|
||||||
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 {
|
if e != nil {
|
||||||
glog.V(0).Infoln("Reading Content [ERROR]", e)
|
|
||||||
return
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
|
|
||||||
|
|
||||||
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" {
|
|
||||||
isGzipped = true
|
|
||||||
} else if operation.IsGzippable(ext, mtype) {
|
|
||||||
if data, e = operation.GzipData(data); e != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
isGzipped = true
|
|
||||||
}
|
|
||||||
if ext == ".gz" {
|
|
||||||
if strings.HasSuffix(fileName, ".css.gz") ||
|
|
||||||
strings.HasSuffix(fileName, ".html.gz") ||
|
|
||||||
strings.HasSuffix(fileName, ".txt.gz") ||
|
|
||||||
strings.HasSuffix(fileName, ".js.gz") {
|
|
||||||
fileName = fileName[:len(fileName)-3]
|
|
||||||
isGzipped = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
|
modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
|
||||||
ttl, _ = ReadTTL(r.FormValue("ttl"))
|
ttl, _ = ReadTTL(r.FormValue("ttl"))
|
||||||
|
|
||||||
|
|
103
weed/storage/needle_parse_multipart.go
Normal file
103
weed/storage/needle_parse_multipart.go
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
package storage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"mime"
|
||||||
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
||||||
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
||||||
|
"net/http"
|
||||||
|
"path"
|
||||||
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseMultipart(r *http.Request) (
|
||||||
|
fileName string, data []byte, mimeType string, isGzipped bool, isChunkedFile bool, e error){
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
|
||||||
|
|
||||||
|
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" {
|
||||||
|
isGzipped = true
|
||||||
|
} else if operation.IsGzippable(ext, mtype) {
|
||||||
|
if data, e = operation.GzipData(data); e != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
isGzipped = true
|
||||||
|
}
|
||||||
|
if ext == ".gz" {
|
||||||
|
if strings.HasSuffix(fileName, ".css.gz") ||
|
||||||
|
strings.HasSuffix(fileName, ".html.gz") ||
|
||||||
|
strings.HasSuffix(fileName, ".txt.gz") ||
|
||||||
|
strings.HasSuffix(fileName, ".js.gz") {
|
||||||
|
fileName = fileName[:len(fileName)-3]
|
||||||
|
isGzipped = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
Loading…
Reference in a new issue