2020-03-07 14:06:58 +00:00
|
|
|
package needle
|
|
|
|
|
|
|
|
import (
|
2021-06-06 20:42:36 +00:00
|
|
|
"bytes"
|
2020-06-23 14:24:11 +00:00
|
|
|
"crypto/md5"
|
2020-06-28 14:25:50 +00:00
|
|
|
"encoding/base64"
|
2020-03-07 14:06:58 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
2020-06-20 05:11:36 +00:00
|
|
|
"path/filepath"
|
2020-03-07 14:06:58 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2020-03-07 14:06:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ParsedUpload struct {
|
2021-06-06 20:42:36 +00:00
|
|
|
FileName string
|
|
|
|
Data []byte
|
|
|
|
bytesBuffer *bytes.Buffer
|
|
|
|
MimeType string
|
|
|
|
PairMap map[string]string
|
|
|
|
IsGzipped bool
|
2020-11-21 21:06:35 +00:00
|
|
|
// IsZstd bool
|
2020-03-07 14:06:58 +00:00
|
|
|
OriginalDataSize int
|
|
|
|
ModifiedTime uint64
|
|
|
|
Ttl *TTL
|
|
|
|
IsChunkedFile bool
|
|
|
|
UncompressedData []byte
|
2020-08-06 12:22:53 +00:00
|
|
|
ContentMd5 string
|
2020-03-07 14:06:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 20:42:36 +00:00
|
|
|
func ParseUpload(r *http.Request, sizeLimit int64, bytesBuffer *bytes.Buffer) (pu *ParsedUpload, e error) {
|
|
|
|
bytesBuffer.Reset()
|
|
|
|
pu = &ParsedUpload{bytesBuffer: bytesBuffer}
|
2020-03-07 14:06:58 +00:00
|
|
|
pu.PairMap = make(map[string]string)
|
|
|
|
for k, v := range r.Header {
|
|
|
|
if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) {
|
|
|
|
pu.PairMap[k] = v[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
e = parseMultipart(r, sizeLimit, pu)
|
|
|
|
} else {
|
|
|
|
e = parsePut(r, sizeLimit, pu)
|
|
|
|
}
|
|
|
|
if e != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pu.ModifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
|
|
|
|
pu.Ttl, _ = ReadTTL(r.FormValue("ttl"))
|
|
|
|
|
|
|
|
pu.OriginalDataSize = len(pu.Data)
|
|
|
|
pu.UncompressedData = pu.Data
|
2020-08-01 18:16:16 +00:00
|
|
|
// println("received data", len(pu.Data), "isGzipped", pu.IsGzipped, "mime", pu.MimeType, "name", pu.FileName)
|
2020-03-07 14:06:58 +00:00
|
|
|
if pu.IsGzipped {
|
2020-06-20 15:16:16 +00:00
|
|
|
if unzipped, e := util.DecompressData(pu.Data); e == nil {
|
2020-03-07 14:06:58 +00:00
|
|
|
pu.OriginalDataSize = len(unzipped)
|
|
|
|
pu.UncompressedData = unzipped
|
2020-06-20 05:11:36 +00:00
|
|
|
// println("ungzipped data size", len(unzipped))
|
2020-03-07 14:06:58 +00:00
|
|
|
}
|
2020-06-20 05:11:36 +00:00
|
|
|
} else {
|
|
|
|
ext := filepath.Base(pu.FileName)
|
2020-06-24 18:38:59 +00:00
|
|
|
mimeType := pu.MimeType
|
|
|
|
if mimeType == "" {
|
|
|
|
mimeType = http.DetectContentType(pu.Data)
|
|
|
|
}
|
|
|
|
// println("detected mimetype to", pu.MimeType)
|
|
|
|
if mimeType == "application/octet-stream" {
|
|
|
|
mimeType = ""
|
|
|
|
}
|
2021-10-07 20:29:00 +00:00
|
|
|
if shouldBeCompressed, iAmSure := util.IsCompressableFileType(ext, mimeType); shouldBeCompressed && iAmSure {
|
2021-10-07 20:24:16 +00:00
|
|
|
// println("ext", ext, "iAmSure", iAmSure, "shouldBeCompressed", shouldBeCompressed, "mimeType", pu.MimeType)
|
|
|
|
if compressedData, err := util.GzipData(pu.Data); err == nil {
|
|
|
|
if len(compressedData)*10 < len(pu.Data)*9 {
|
|
|
|
pu.Data = compressedData
|
|
|
|
pu.IsGzipped = true
|
2020-06-20 05:11:36 +00:00
|
|
|
}
|
2021-10-07 20:24:16 +00:00
|
|
|
// println("gzipped data size", len(compressedData))
|
2020-06-20 05:11:36 +00:00
|
|
|
}
|
2020-03-07 14:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 11:35:13 +00:00
|
|
|
|
2020-08-06 12:22:53 +00:00
|
|
|
// md5
|
|
|
|
h := md5.New()
|
|
|
|
h.Write(pu.UncompressedData)
|
|
|
|
pu.ContentMd5 = base64.StdEncoding.EncodeToString(h.Sum(nil))
|
2020-06-24 11:35:13 +00:00
|
|
|
if expectedChecksum := r.Header.Get("Content-MD5"); expectedChecksum != "" {
|
2020-08-06 12:22:53 +00:00
|
|
|
if expectedChecksum != pu.ContentMd5 {
|
2020-08-08 17:18:32 +00:00
|
|
|
e = fmt.Errorf("Content-MD5 did not match md5 of file data expected [%s] received [%s] size %d", expectedChecksum, pu.ContentMd5, len(pu.UncompressedData))
|
2020-06-24 11:35:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:06:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 20:42:36 +00:00
|
|
|
func parsePut(r *http.Request, sizeLimit int64, pu *ParsedUpload) error {
|
2020-03-07 14:06:58 +00:00
|
|
|
pu.IsGzipped = r.Header.Get("Content-Encoding") == "gzip"
|
2020-11-21 21:06:35 +00:00
|
|
|
// pu.IsZstd = r.Header.Get("Content-Encoding") == "zstd"
|
2020-03-07 14:06:58 +00:00
|
|
|
pu.MimeType = r.Header.Get("Content-Type")
|
|
|
|
pu.FileName = ""
|
2021-06-06 20:42:36 +00:00
|
|
|
dataSize, err := pu.bytesBuffer.ReadFrom(io.LimitReader(r.Body, sizeLimit+1))
|
|
|
|
if err == io.EOF || dataSize == sizeLimit+1 {
|
2021-10-14 04:27:58 +00:00
|
|
|
io.Copy(io.Discard, r.Body)
|
2020-03-07 14:06:58 +00:00
|
|
|
}
|
2021-06-06 20:42:36 +00:00
|
|
|
pu.Data = pu.bytesBuffer.Bytes()
|
2020-03-07 14:06:58 +00:00
|
|
|
r.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMultipart(r *http.Request, sizeLimit int64, pu *ParsedUpload) (e error) {
|
|
|
|
defer func() {
|
|
|
|
if e != nil && r.Body != nil {
|
2021-10-14 04:27:58 +00:00
|
|
|
io.Copy(io.Discard, r.Body)
|
2020-03-07 14:06:58 +00:00
|
|
|
r.Body.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
form, fe := r.MultipartReader()
|
|
|
|
if fe != nil {
|
|
|
|
glog.V(0).Infoln("MultipartReader [ERROR]", fe)
|
|
|
|
e = fe
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-20 05:11:36 +00:00
|
|
|
// first multi-part item
|
2020-03-07 14:06:58 +00:00
|
|
|
part, fe := form.NextPart()
|
|
|
|
if fe != nil {
|
|
|
|
glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
|
|
|
|
e = fe
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pu.FileName = part.FileName()
|
|
|
|
if pu.FileName != "" {
|
|
|
|
pu.FileName = path.Base(pu.FileName)
|
|
|
|
}
|
|
|
|
|
2021-06-06 20:42:36 +00:00
|
|
|
var dataSize int64
|
|
|
|
dataSize, e = pu.bytesBuffer.ReadFrom(io.LimitReader(part, sizeLimit+1))
|
2020-03-07 14:06:58 +00:00
|
|
|
if e != nil {
|
|
|
|
glog.V(0).Infoln("Reading Content [ERROR]", e)
|
|
|
|
return
|
|
|
|
}
|
2021-06-06 20:42:36 +00:00
|
|
|
if dataSize == sizeLimit+1 {
|
2020-03-07 14:06:58 +00:00
|
|
|
e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
|
|
|
|
return
|
|
|
|
}
|
2021-06-06 20:42:36 +00:00
|
|
|
pu.Data = pu.bytesBuffer.Bytes()
|
2020-03-07 14:06:58 +00:00
|
|
|
|
2020-06-20 05:11:36 +00:00
|
|
|
// if the filename is empty string, do a search on the other multi-part items
|
2020-03-07 14:06:58 +00:00
|
|
|
for pu.FileName == "" {
|
|
|
|
part2, fe := form.NextPart()
|
|
|
|
if fe != nil {
|
|
|
|
break // no more or on error, just safely break
|
|
|
|
}
|
|
|
|
|
|
|
|
fName := part2.FileName()
|
|
|
|
|
2020-06-20 05:11:36 +00:00
|
|
|
// found the first <file type> multi-part has filename
|
2020-03-07 14:06:58 +00:00
|
|
|
if fName != "" {
|
2021-06-06 20:42:36 +00:00
|
|
|
pu.bytesBuffer.Reset()
|
|
|
|
dataSize2, fe2 := pu.bytesBuffer.ReadFrom(io.LimitReader(part2, sizeLimit+1))
|
2020-03-07 14:06:58 +00:00
|
|
|
if fe2 != nil {
|
|
|
|
glog.V(0).Infoln("Reading Content [ERROR]", fe2)
|
|
|
|
e = fe2
|
|
|
|
return
|
|
|
|
}
|
2021-06-06 20:42:36 +00:00
|
|
|
if dataSize2 == sizeLimit+1 {
|
2020-03-07 14:06:58 +00:00
|
|
|
e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-20 05:11:36 +00:00
|
|
|
// update
|
2021-06-06 20:42:36 +00:00
|
|
|
pu.Data = pu.bytesBuffer.Bytes()
|
2020-03-07 14:06:58 +00:00
|
|
|
pu.FileName = path.Base(fName)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pu.IsChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
|
|
|
|
|
|
|
|
if !pu.IsChunkedFile {
|
|
|
|
|
|
|
|
dotIndex := strings.LastIndex(pu.FileName, ".")
|
|
|
|
ext, mtype := "", ""
|
|
|
|
if dotIndex > 0 {
|
|
|
|
ext = strings.ToLower(pu.FileName[dotIndex:])
|
|
|
|
mtype = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
contentType := part.Header.Get("Content-Type")
|
|
|
|
if contentType != "" && contentType != "application/octet-stream" && mtype != contentType {
|
2020-06-20 05:11:36 +00:00
|
|
|
pu.MimeType = contentType // only return mime type if not deductable
|
2020-03-07 14:06:58 +00:00
|
|
|
mtype = contentType
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-01-03 09:42:21 +00:00
|
|
|
pu.IsGzipped = part.Header.Get("Content-Encoding") == "gzip"
|
|
|
|
// pu.IsZstd = part.Header.Get("Content-Encoding") == "zstd"
|
2020-03-07 14:06:58 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|