2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2013-02-27 06:56:18 +08:00
|
|
|
"code.google.com/p/weed-fs/go/util"
|
2012-06-29 15:53:47 +08:00
|
|
|
"encoding/hex"
|
2013-02-27 06:56:18 +08:00
|
|
|
"errors"
|
2012-09-21 12:03:40 +08:00
|
|
|
"fmt"
|
2011-12-16 22:51:26 +08:00
|
|
|
"io/ioutil"
|
2012-07-30 16:37:10 +08:00
|
|
|
"mime"
|
2012-06-29 15:53:47 +08:00
|
|
|
"net/http"
|
2013-01-23 07:07:51 +08:00
|
|
|
"path"
|
2012-07-17 01:15:16 +08:00
|
|
|
"strconv"
|
2011-12-16 22:51:26 +08:00
|
|
|
"strings"
|
2013-07-09 14:38:38 +08:00
|
|
|
"time"
|
2011-12-16 22:51:26 +08:00
|
|
|
)
|
|
|
|
|
2012-12-21 16:36:55 +08:00
|
|
|
const (
|
2013-01-17 16:56:56 +08:00
|
|
|
NeedleHeaderSize = 16 //should never change this
|
|
|
|
NeedlePaddingSize = 8
|
2012-12-23 04:38:09 +08:00
|
|
|
NeedleChecksumSize = 4
|
2012-12-21 16:36:55 +08:00
|
|
|
)
|
|
|
|
|
2011-12-19 13:59:37 +08:00
|
|
|
type Needle struct {
|
2013-02-11 01:44:44 +08: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 18:13:02 +08:00
|
|
|
|
2013-07-09 14:38:38 +08: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 18:13:02 +08:00
|
|
|
|
2013-02-11 01:44:44 +08:00
|
|
|
Checksum CRC `comment:"CRC32 to check integrity"`
|
|
|
|
Padding []byte `comment:"Aligned to 8 bytes"`
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 15:10:21 +08:00
|
|
|
func NewNeedle(r *http.Request) (n *Needle, e error) {
|
2012-06-29 15:53:47 +08:00
|
|
|
|
2011-12-19 13:59:37 +08:00
|
|
|
n = new(Needle)
|
|
|
|
form, fe := r.MultipartReader()
|
|
|
|
if fe != nil {
|
2012-09-21 12:03:40 +08:00
|
|
|
fmt.Println("MultipartReader [ERROR]", fe)
|
2012-07-03 14:46:26 +08:00
|
|
|
e = fe
|
|
|
|
return
|
2011-12-19 13:59:37 +08:00
|
|
|
}
|
2012-11-20 16:42:45 +08:00
|
|
|
part, fe := form.NextPart()
|
|
|
|
if fe != nil {
|
|
|
|
fmt.Println("Reading Multi part [ERROR]", fe)
|
|
|
|
e = fe
|
|
|
|
return
|
|
|
|
}
|
2013-04-16 15:10:21 +08:00
|
|
|
fname := part.FileName()
|
2013-02-27 06:56:18 +08:00
|
|
|
if fname != "" {
|
|
|
|
fname = path.Base(part.FileName())
|
|
|
|
} else {
|
|
|
|
e = errors.New("No file found!")
|
|
|
|
return
|
|
|
|
}
|
2012-07-30 16:37:10 +08:00
|
|
|
data, _ := ioutil.ReadAll(part)
|
|
|
|
dotIndex := strings.LastIndex(fname, ".")
|
2012-12-22 18:10:45 +08:00
|
|
|
ext, mtype := "", ""
|
2012-07-30 16:37:10 +08:00
|
|
|
if dotIndex > 0 {
|
2012-12-22 18:10:45 +08:00
|
|
|
ext = fname[dotIndex:]
|
|
|
|
mtype = mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
contentType := part.Header.Get("Content-Type")
|
|
|
|
if contentType != "" && mtype != contentType && len(contentType) < 256 {
|
|
|
|
n.Mime = []byte(contentType)
|
2012-12-23 04:38:09 +08:00
|
|
|
n.SetHasMime()
|
2012-12-22 18:10:45 +08:00
|
|
|
mtype = contentType
|
|
|
|
}
|
|
|
|
if IsGzippable(ext, mtype) {
|
2013-01-17 16:56:56 +08:00
|
|
|
if data, e = GzipData(data); e != nil {
|
|
|
|
return
|
|
|
|
}
|
2012-12-22 18:10:45 +08:00
|
|
|
n.SetGzipped()
|
|
|
|
}
|
|
|
|
if ext == ".gz" {
|
|
|
|
n.SetGzipped()
|
|
|
|
}
|
|
|
|
if len(fname) < 256 {
|
|
|
|
if strings.HasSuffix(fname, ".gz") {
|
|
|
|
n.Name = []byte(fname[:len(fname)-3])
|
|
|
|
} else {
|
|
|
|
n.Name = []byte(fname)
|
2012-07-30 16:37:10 +08:00
|
|
|
}
|
2012-12-23 04:38:09 +08:00
|
|
|
n.SetHasName()
|
2012-07-30 16:37:10 +08:00
|
|
|
}
|
2013-07-09 14:38:38 +08:00
|
|
|
n.LastModified = uint64(time.Now().Unix())
|
|
|
|
n.SetHasLastModifiedDate()
|
2012-12-22 18:10:45 +08:00
|
|
|
|
2011-12-19 13:59:37 +08:00
|
|
|
n.Data = data
|
2012-09-26 06:37:13 +08:00
|
|
|
n.Checksum = NewCRC(data)
|
2011-12-16 22:51:26 +08:00
|
|
|
|
2012-06-29 15:53:47 +08:00
|
|
|
commaSep := strings.LastIndex(r.URL.Path, ",")
|
2011-12-22 12:04:47 +08:00
|
|
|
dotSep := strings.LastIndex(r.URL.Path, ".")
|
|
|
|
fid := r.URL.Path[commaSep+1:]
|
|
|
|
if dotSep > 0 {
|
2012-06-29 15:53:47 +08:00
|
|
|
fid = r.URL.Path[commaSep+1 : dotSep]
|
2011-12-22 12:04:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
n.ParsePath(fid)
|
2011-12-19 13:59:37 +08:00
|
|
|
|
|
|
|
return
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2011-12-22 12:04:47 +08:00
|
|
|
func (n *Needle) ParsePath(fid string) {
|
|
|
|
length := len(fid)
|
|
|
|
if length <= 8 {
|
2012-07-16 15:41:39 +08:00
|
|
|
if length > 0 {
|
2012-08-24 16:15:27 +08:00
|
|
|
println("Invalid fid", fid, "length", length)
|
2012-07-16 15:41:39 +08:00
|
|
|
}
|
2011-12-20 17:00:01 +08:00
|
|
|
return
|
|
|
|
}
|
2012-07-17 01:15:16 +08:00
|
|
|
delta := ""
|
|
|
|
deltaIndex := strings.LastIndex(fid, "_")
|
|
|
|
if deltaIndex > 0 {
|
|
|
|
fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
|
|
|
|
}
|
2012-08-24 14:06:15 +08:00
|
|
|
n.Id, n.Cookie = ParseKeyHash(fid)
|
2012-07-17 01:15:16 +08:00
|
|
|
if delta != "" {
|
|
|
|
d, e := strconv.ParseUint(delta, 10, 64)
|
|
|
|
if e == nil {
|
2012-08-24 14:06:15 +08:00
|
|
|
n.Id += d
|
2012-07-17 01:15:16 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2013-01-21 11:44:23 +08:00
|
|
|
|
2012-06-29 15:53:47 +08:00
|
|
|
func ParseKeyHash(key_hash_string string) (uint64, uint32) {
|
2012-12-21 16:36:55 +08:00
|
|
|
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
|
|
|
key_hash_len := len(key_hash_bytes)
|
|
|
|
if khe != nil || key_hash_len <= 4 {
|
|
|
|
println("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
|
|
|
|
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 12:04:47 +08:00
|
|
|
}
|