2011-12-16 14:51:26 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-09-02 06:58:21 +00:00
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
2013-02-26 22:56:18 +00:00
|
|
|
"code.google.com/p/weed-fs/go/util"
|
2012-06-29 07:53:47 +00:00
|
|
|
"encoding/hex"
|
2011-12-16 14:51:26 +00:00
|
|
|
"io/ioutil"
|
2012-07-30 08:37:10 +00:00
|
|
|
"mime"
|
2012-06-29 07:53:47 +00:00
|
|
|
"net/http"
|
2013-01-22 23:07:51 +00:00
|
|
|
"path"
|
2012-07-16 17:15:16 +00:00
|
|
|
"strconv"
|
2011-12-16 14:51:26 +00:00
|
|
|
"strings"
|
2013-07-09 06:38:38 +00:00
|
|
|
"time"
|
2011-12-16 14:51:26 +00:00
|
|
|
)
|
|
|
|
|
2012-12-21 08:36:55 +00:00
|
|
|
const (
|
2013-01-17 08:56:56 +00:00
|
|
|
NeedleHeaderSize = 16 //should never change this
|
|
|
|
NeedlePaddingSize = 8
|
2012-12-22 20:38:09 +00:00
|
|
|
NeedleChecksumSize = 4
|
2012-12-21 08:36:55 +00:00
|
|
|
)
|
|
|
|
|
2013-09-19 18:06:14 +00:00
|
|
|
/*
|
|
|
|
* Needle file size is limited to 4GB for now.
|
|
|
|
*/
|
2011-12-19 05:59:37 +00:00
|
|
|
type Needle struct {
|
2013-02-10 17:44:44 +00:00
|
|
|
Cookie uint32 `comment:"random number to mitigate brute force lookups"`
|
|
|
|
Id uint64 `comment:"needle id"`
|
|
|
|
Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
|
2012-12-21 10:13:02 +00:00
|
|
|
|
2013-07-09 06:38:38 +00:00
|
|
|
DataSize uint32 `comment:"Data size"` //version2
|
|
|
|
Data []byte `comment:"The actual file data"`
|
|
|
|
Flags byte `comment:"boolean flags"` //version2
|
|
|
|
NameSize uint8 //version2
|
|
|
|
Name []byte `comment:"maximum 256 characters"` //version2
|
|
|
|
MimeSize uint8 //version2
|
|
|
|
Mime []byte `comment:"maximum 256 characters"` //version2
|
|
|
|
LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk
|
2012-12-21 10:13:02 +00:00
|
|
|
|
2013-02-10 17:44:44 +00:00
|
|
|
Checksum CRC `comment:"CRC32 to check integrity"`
|
|
|
|
Padding []byte `comment:"Aligned to 8 bytes"`
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 18:23:24 +00:00
|
|
|
func ParseUpload(r *http.Request) (fileName string, data []byte, mimeType string, isGzipped bool, modifiedTime uint64, e error) {
|
2011-12-19 05:59:37 +00:00
|
|
|
form, fe := r.MultipartReader()
|
|
|
|
if fe != nil {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("MultipartReader [ERROR]", fe)
|
2012-07-03 06:46:26 +00:00
|
|
|
e = fe
|
|
|
|
return
|
2011-12-19 05:59:37 +00:00
|
|
|
}
|
2012-11-20 08:42:45 +00:00
|
|
|
part, fe := form.NextPart()
|
|
|
|
if fe != nil {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
|
2012-11-20 08:42:45 +00:00
|
|
|
e = fe
|
|
|
|
return
|
|
|
|
}
|
2013-08-06 18:23:24 +00:00
|
|
|
fileName = part.FileName()
|
|
|
|
if fileName != "" {
|
|
|
|
fileName = path.Base(fileName)
|
2013-02-26 22:56:18 +00:00
|
|
|
}
|
2013-08-14 07:31:02 +00:00
|
|
|
|
2013-08-06 18:23:24 +00:00
|
|
|
data, e = ioutil.ReadAll(part)
|
|
|
|
if e != nil {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("Reading Content [ERROR]", e)
|
2013-08-05 20:37:41 +00:00
|
|
|
return
|
|
|
|
}
|
2013-08-06 18:23:24 +00:00
|
|
|
dotIndex := strings.LastIndex(fileName, ".")
|
2012-12-22 10:10:45 +00:00
|
|
|
ext, mtype := "", ""
|
2012-07-30 08:37:10 +00:00
|
|
|
if dotIndex > 0 {
|
2013-08-06 18:23:24 +00:00
|
|
|
ext = strings.ToLower(fileName[dotIndex:])
|
2012-12-22 10:10:45 +00:00
|
|
|
mtype = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
contentType := part.Header.Get("Content-Type")
|
2013-08-06 18:23:24 +00:00
|
|
|
if contentType != "" && mtype != contentType {
|
|
|
|
mimeType = contentType //only return mime type if not deductable
|
2012-12-22 10:10:45 +00:00
|
|
|
mtype = contentType
|
|
|
|
}
|
2013-07-15 18:04:43 +00:00
|
|
|
if part.Header.Get("Content-Encoding") == "gzip" {
|
2013-08-06 18:23:24 +00:00
|
|
|
isGzipped = true
|
2013-07-15 18:04:43 +00:00
|
|
|
} else if IsGzippable(ext, mtype) {
|
2013-01-17 08:56:56 +00:00
|
|
|
if data, e = GzipData(data); e != nil {
|
|
|
|
return
|
|
|
|
}
|
2013-08-06 18:23:24 +00:00
|
|
|
isGzipped = true
|
2012-12-22 10:10:45 +00:00
|
|
|
}
|
|
|
|
if ext == ".gz" {
|
2013-08-06 18:23:24 +00:00
|
|
|
isGzipped = true
|
2012-12-22 10:10:45 +00:00
|
|
|
}
|
2013-08-06 18:23:24 +00:00
|
|
|
if strings.HasSuffix(fileName, ".gz") {
|
|
|
|
fileName = fileName[:len(fileName)-3]
|
|
|
|
}
|
|
|
|
modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
|
2013-09-02 06:58:21 +00:00
|
|
|
return
|
2013-08-06 18:23:24 +00:00
|
|
|
}
|
|
|
|
func NewNeedle(r *http.Request) (n *Needle, e error) {
|
2013-09-02 06:58:21 +00:00
|
|
|
fname, mimeType, isGzipped := "", "", false
|
2013-08-06 18:23:24 +00:00
|
|
|
n = new(Needle)
|
|
|
|
fname, n.Data, mimeType, isGzipped, n.LastModified, e = ParseUpload(r)
|
|
|
|
if e != nil {
|
2013-09-02 06:58:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(fname) < 256 {
|
|
|
|
n.Name = []byte(fname)
|
|
|
|
n.SetHasName()
|
2013-08-06 18:23:24 +00:00
|
|
|
}
|
|
|
|
if len(mimeType) < 256 {
|
|
|
|
n.Mime = []byte(mimeType)
|
|
|
|
n.SetHasMime()
|
|
|
|
}
|
|
|
|
if isGzipped {
|
|
|
|
n.SetGzipped()
|
2012-07-30 08:37:10 +00:00
|
|
|
}
|
2013-08-06 18:23:24 +00:00
|
|
|
if n.LastModified == 0 {
|
2013-07-10 07:25:14 +00:00
|
|
|
n.LastModified = uint64(time.Now().Unix())
|
2013-09-02 06:58:21 +00:00
|
|
|
n.SetHasLastModifiedDate()
|
2013-07-10 07:25:14 +00:00
|
|
|
}
|
2012-12-22 10:10:45 +00:00
|
|
|
|
2013-08-06 18:23:24 +00:00
|
|
|
n.Checksum = NewCRC(n.Data)
|
2011-12-16 14:51:26 +00:00
|
|
|
|
2012-06-29 07:53:47 +00:00
|
|
|
commaSep := strings.LastIndex(r.URL.Path, ",")
|
2011-12-22 04:04:47 +00:00
|
|
|
dotSep := strings.LastIndex(r.URL.Path, ".")
|
|
|
|
fid := r.URL.Path[commaSep+1:]
|
|
|
|
if dotSep > 0 {
|
2012-06-29 07:53:47 +00:00
|
|
|
fid = r.URL.Path[commaSep+1 : dotSep]
|
2011-12-22 04:04:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n.ParsePath(fid)
|
2011-12-19 05:59:37 +00:00
|
|
|
|
|
|
|
return
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2011-12-22 04:04:47 +00:00
|
|
|
func (n *Needle) ParsePath(fid string) {
|
|
|
|
length := len(fid)
|
|
|
|
if length <= 8 {
|
2011-12-20 09:00:01 +00:00
|
|
|
return
|
|
|
|
}
|
2012-07-16 17:15:16 +00:00
|
|
|
delta := ""
|
|
|
|
deltaIndex := strings.LastIndex(fid, "_")
|
|
|
|
if deltaIndex > 0 {
|
|
|
|
fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
|
|
|
|
}
|
2012-08-24 06:06:15 +00:00
|
|
|
n.Id, n.Cookie = ParseKeyHash(fid)
|
2012-07-16 17:15:16 +00:00
|
|
|
if delta != "" {
|
|
|
|
d, e := strconv.ParseUint(delta, 10, 64)
|
|
|
|
if e == nil {
|
2012-08-24 06:06:15 +00:00
|
|
|
n.Id += d
|
2012-07-16 17:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
|
2012-06-29 07:53:47 +00:00
|
|
|
func ParseKeyHash(key_hash_string string) (uint64, uint32) {
|
2012-12-21 08:36:55 +00:00
|
|
|
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
|
|
|
key_hash_len := len(key_hash_bytes)
|
|
|
|
if khe != nil || key_hash_len <= 4 {
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
|
2012-12-21 08:36:55 +00:00
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
|
|
|
|
hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
|
|
|
|
return key, hash
|
2011-12-22 04:04:47 +00:00
|
|
|
}
|