diff options
| author | sparklxb <sparklxb@163.com> | 2017-01-08 09:16:29 +0800 |
|---|---|---|
| committer | sparklxb <sparklxb@163.com> | 2017-01-08 09:16:40 +0800 |
| commit | 86a7c562751fc89d52da30425f1513b4553dfa8c (patch) | |
| tree | 521bdcdf6e9791f0b58b9864f60be973f3923951 /weed/storage/needle.go | |
| parent | 13e7069eb9cd72f94e72acb8fbbc9dd0307da703 (diff) | |
| download | seaweedfs-86a7c562751fc89d52da30425f1513b4553dfa8c.tar.xz seaweedfs-86a7c562751fc89d52da30425f1513b4553dfa8c.zip | |
support additional header name-value pairs
Diffstat (limited to 'weed/storage/needle.go')
| -rw-r--r-- | weed/storage/needle.go | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/weed/storage/needle.go b/weed/storage/needle.go index 1d306395e..ea013e290 100644 --- a/weed/storage/needle.go +++ b/weed/storage/needle.go @@ -11,6 +11,8 @@ import ( "strings" "time" + "encoding/json" + "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/images" "github.com/chrislusf/seaweedfs/weed/operation" @@ -22,6 +24,7 @@ const ( NeedleChecksumSize = 4 MaxPossibleVolumeSize = 4 * 1024 * 1024 * 1024 * 8 TombstoneFileSize = math.MaxUint32 + PairNamePrefix = "Seaweed-" ) /* @@ -40,6 +43,8 @@ type Needle struct { Name []byte `comment:"maximum 256 characters"` //version2 MimeSize uint8 //version2 Mime []byte `comment:"maximum 256 characters"` //version2 + PairsSize uint16 //version2 + Pairs []byte `comment:"additional name value pairs, json format, maximum 64kB"` LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk Ttl *TTL @@ -55,8 +60,17 @@ func (n *Needle) String() (str string) { } func ParseUpload(r *http.Request) ( - fileName string, data []byte, mimeType string, isGzipped bool, + fileName string, data []byte, mimeType string, pairs []byte, isGzipped bool, modifiedTime uint64, ttl *TTL, isChunkedFile bool, e error) { + pairMap := make(map[string]string) + for k, v := range r.Header { + if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) { + pairMap[k] = v[0] + } + } + if len(pairMap) != 0 { + pairs, _ = json.Marshal(pairMap) + } form, fe := r.MultipartReader() if fe != nil { glog.V(0).Infoln("MultipartReader [ERROR]", fe) @@ -109,19 +123,19 @@ func ParseUpload(r *http.Request) ( } isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm")) - isGzipped = false + 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 !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) { @@ -144,9 +158,10 @@ func ParseUpload(r *http.Request) ( return } func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) { + var pair []byte fname, mimeType, isGzipped, isChunkedFile := "", "", false, false n = new(Needle) - fname, n.Data, mimeType, isGzipped, n.LastModified, n.Ttl, isChunkedFile, e = ParseUpload(r) + fname, n.Data, mimeType, pair, isGzipped, n.LastModified, n.Ttl, isChunkedFile, e = ParseUpload(r) if e != nil { return } @@ -158,6 +173,11 @@ func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) { n.Mime = []byte(mimeType) n.SetHasMime() } + if len(pair) < 65536 { + n.Pairs = pair + n.PairsSize = uint16(len(pair)) + n.SetHasPairs() + } if isGzipped { n.SetGzipped() } |
