mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
refactor "content upload" out of needle creation
This commit is contained in:
parent
54906c48f3
commit
952974491b
|
@ -38,9 +38,7 @@ type Needle struct {
|
||||||
Padding []byte `comment:"Aligned to 8 bytes"`
|
Padding []byte `comment:"Aligned to 8 bytes"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNeedle(r *http.Request) (n *Needle, e error) {
|
func ParseUpload(r *http.Request) (fileName string, data []byte, mimeType string, isGzipped bool, modifiedTime uint64, e error) {
|
||||||
|
|
||||||
n = new(Needle)
|
|
||||||
form, fe := r.MultipartReader()
|
form, fe := r.MultipartReader()
|
||||||
if fe != nil {
|
if fe != nil {
|
||||||
log.Println("MultipartReader [ERROR]", fe)
|
log.Println("MultipartReader [ERROR]", fe)
|
||||||
|
@ -53,58 +51,70 @@ func NewNeedle(r *http.Request) (n *Needle, e error) {
|
||||||
e = fe
|
e = fe
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fname := part.FileName()
|
fileName = part.FileName()
|
||||||
if fname != "" {
|
if fileName != "" {
|
||||||
fname = path.Base(part.FileName())
|
fileName = path.Base(fileName)
|
||||||
} else {
|
} else {
|
||||||
e = errors.New("No file found!")
|
e = errors.New("No file found!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data, ioe := ioutil.ReadAll(part)
|
data, e = ioutil.ReadAll(part)
|
||||||
if ioe != nil {
|
if e != nil {
|
||||||
e = ioe
|
log.Println("Reading Content [ERROR]", e)
|
||||||
log.Println("Reading Content [ERROR]", ioe)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dotIndex := strings.LastIndex(fname, ".")
|
dotIndex := strings.LastIndex(fileName, ".")
|
||||||
ext, mtype := "", ""
|
ext, mtype := "", ""
|
||||||
if dotIndex > 0 {
|
if dotIndex > 0 {
|
||||||
ext = fname[dotIndex:]
|
ext = strings.ToLower(fileName[dotIndex:])
|
||||||
mtype = mime.TypeByExtension(ext)
|
mtype = mime.TypeByExtension(ext)
|
||||||
}
|
}
|
||||||
contentType := part.Header.Get("Content-Type")
|
contentType := part.Header.Get("Content-Type")
|
||||||
if contentType != "" && mtype != contentType && len(contentType) < 256 {
|
if contentType != "" && mtype != contentType {
|
||||||
n.Mime = []byte(contentType)
|
mimeType = contentType //only return mime type if not deductable
|
||||||
n.SetHasMime()
|
|
||||||
mtype = contentType
|
mtype = contentType
|
||||||
}
|
}
|
||||||
if part.Header.Get("Content-Encoding") == "gzip" {
|
if part.Header.Get("Content-Encoding") == "gzip" {
|
||||||
n.SetGzipped()
|
isGzipped = true
|
||||||
} else if IsGzippable(ext, mtype) {
|
} else if IsGzippable(ext, mtype) {
|
||||||
if data, e = GzipData(data); e != nil {
|
if data, e = GzipData(data); e != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
n.SetGzipped()
|
isGzipped = true
|
||||||
}
|
}
|
||||||
if ext == ".gz" {
|
if ext == ".gz" {
|
||||||
|
isGzipped = true
|
||||||
|
}
|
||||||
|
if strings.HasSuffix(fileName, ".gz") {
|
||||||
|
fileName = fileName[:len(fileName)-3]
|
||||||
|
}
|
||||||
|
modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func NewNeedle(r *http.Request) (n *Needle, e error) {
|
||||||
|
fname, mimeType, isGzipped := "", "", false
|
||||||
|
n = new(Needle)
|
||||||
|
fname, n.Data, mimeType, isGzipped, n.LastModified, e = ParseUpload(r)
|
||||||
|
if e != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(fname) < 256 {
|
||||||
|
n.Name = []byte(fname)
|
||||||
|
n.SetHasName()
|
||||||
|
}
|
||||||
|
if len(mimeType) < 256 {
|
||||||
|
n.Mime = []byte(mimeType)
|
||||||
|
n.SetHasMime()
|
||||||
|
}
|
||||||
|
if isGzipped {
|
||||||
n.SetGzipped()
|
n.SetGzipped()
|
||||||
}
|
}
|
||||||
if len(fname) < 256 {
|
if n.LastModified == 0 {
|
||||||
if strings.HasSuffix(fname, ".gz") {
|
|
||||||
n.Name = []byte(fname[:len(fname)-3])
|
|
||||||
} else {
|
|
||||||
n.Name = []byte(fname)
|
|
||||||
}
|
|
||||||
n.SetHasName()
|
|
||||||
}
|
|
||||||
var parseError error
|
|
||||||
if n.LastModified, parseError = strconv.ParseUint(r.FormValue("ts"), 10, 64); parseError != nil {
|
|
||||||
n.LastModified = uint64(time.Now().Unix())
|
n.LastModified = uint64(time.Now().Unix())
|
||||||
|
n.SetHasLastModifiedDate()
|
||||||
}
|
}
|
||||||
n.SetHasLastModifiedDate()
|
|
||||||
|
|
||||||
n.Data = data
|
n.Checksum = NewCRC(n.Data)
|
||||||
n.Checksum = NewCRC(data)
|
|
||||||
|
|
||||||
commaSep := strings.LastIndex(r.URL.Path, ",")
|
commaSep := strings.LastIndex(r.URL.Path, ",")
|
||||||
dotSep := strings.LastIndex(r.URL.Path, ".")
|
dotSep := strings.LastIndex(r.URL.Path, ".")
|
||||||
|
|
Loading…
Reference in a new issue