2011-12-16 22:51:26 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2012-06-29 15:53:47 +08:00
|
|
|
"encoding/hex"
|
2011-12-19 13:59:37 +08:00
|
|
|
"io"
|
2011-12-16 22:51:26 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2012-07-30 16:37:10 +08:00
|
|
|
"mime"
|
2012-06-29 15:53:47 +08:00
|
|
|
"net/http"
|
2011-12-24 16:40:56 +08:00
|
|
|
"os"
|
2012-07-16 15:41:39 +08:00
|
|
|
"pkg/util"
|
2012-07-17 01:15:16 +08:00
|
|
|
"strconv"
|
2011-12-16 22:51:26 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2011-12-19 13:59:37 +08:00
|
|
|
type Needle struct {
|
2011-12-20 17:00:01 +08:00
|
|
|
Cookie uint32 "random number to mitigate brute force lookups"
|
2012-08-24 14:06:15 +08:00
|
|
|
Id uint64 "needle id"
|
2011-12-20 17:00:01 +08:00
|
|
|
Size uint32 "Data size"
|
|
|
|
Data []byte "The actual file data"
|
|
|
|
Checksum int32 "CRC32 to check integrity"
|
|
|
|
Padding []byte "Aligned to 8 bytes"
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
|
|
|
|
2012-07-03 14:46:26 +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-07-03 14:46:26 +08:00
|
|
|
log.Printf("MultipartReader [ERROR] %s\n", fe)
|
|
|
|
e = fe
|
|
|
|
return
|
2011-12-19 13:59:37 +08:00
|
|
|
}
|
|
|
|
part, _ := form.NextPart()
|
2012-07-30 16:37:10 +08:00
|
|
|
fname := part.FileName()
|
|
|
|
data, _ := ioutil.ReadAll(part)
|
|
|
|
//log.Println("uploading file " + part.FileName())
|
|
|
|
dotIndex := strings.LastIndex(fname, ".")
|
|
|
|
if dotIndex > 0 {
|
|
|
|
ext := fname[dotIndex:]
|
|
|
|
mtype := mime.TypeByExtension(ext)
|
|
|
|
if IsCompressable(ext, mtype) {
|
|
|
|
data = GzipData(data)
|
|
|
|
}
|
|
|
|
}
|
2011-12-19 13:59:37 +08:00
|
|
|
n.Data = 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 {
|
|
|
|
log.Println("Invalid fid", fid, "length", length)
|
|
|
|
}
|
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
|
|
|
}
|
2012-06-29 15:53:47 +08:00
|
|
|
func (n *Needle) Append(w io.Writer) uint32 {
|
2011-12-20 10:34:53 +08:00
|
|
|
header := make([]byte, 16)
|
2012-06-29 15:53:47 +08:00
|
|
|
util.Uint32toBytes(header[0:4], n.Cookie)
|
2012-08-24 14:06:15 +08:00
|
|
|
util.Uint64toBytes(header[4:12], n.Id)
|
2011-12-19 13:59:37 +08:00
|
|
|
n.Size = uint32(len(n.Data))
|
2012-06-29 15:53:47 +08:00
|
|
|
util.Uint32toBytes(header[12:16], n.Size)
|
2011-12-19 13:59:37 +08:00
|
|
|
w.Write(header)
|
|
|
|
w.Write(n.Data)
|
2011-12-20 10:34:53 +08:00
|
|
|
rest := 8 - ((n.Size + 16 + 4) % 8)
|
2012-06-29 15:53:47 +08:00
|
|
|
util.Uint32toBytes(header[0:4], uint32(n.Checksum))
|
2011-12-19 13:59:37 +08:00
|
|
|
w.Write(header[0 : rest+4])
|
2011-12-24 09:25:22 +08:00
|
|
|
return n.Size
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2012-06-29 15:53:47 +08:00
|
|
|
func (n *Needle) Read(r io.Reader, size uint32) (int, error) {
|
2011-12-20 10:34:53 +08:00
|
|
|
bytes := make([]byte, size+16+4)
|
2011-12-24 16:40:56 +08:00
|
|
|
ret, e := r.Read(bytes)
|
2012-06-29 15:53:47 +08:00
|
|
|
n.Cookie = util.BytesToUint32(bytes[0:4])
|
2012-08-24 14:06:15 +08:00
|
|
|
n.Id = util.BytesToUint64(bytes[4:12])
|
2012-06-29 15:53:47 +08:00
|
|
|
n.Size = util.BytesToUint32(bytes[12:16])
|
2011-12-20 10:34:53 +08:00
|
|
|
n.Data = bytes[16 : 16+size]
|
2012-06-29 15:53:47 +08:00
|
|
|
n.Checksum = int32(util.BytesToUint32(bytes[16+size : 16+size+4]))
|
2011-12-24 16:40:56 +08:00
|
|
|
return ret, e
|
|
|
|
}
|
|
|
|
func ReadNeedle(r *os.File) (*Needle, uint32) {
|
2012-06-29 15:53:47 +08:00
|
|
|
n := new(Needle)
|
|
|
|
bytes := make([]byte, 16)
|
|
|
|
count, e := r.Read(bytes)
|
|
|
|
if count <= 0 || e != nil {
|
|
|
|
return nil, 0
|
|
|
|
}
|
|
|
|
n.Cookie = util.BytesToUint32(bytes[0:4])
|
2012-08-24 14:06:15 +08:00
|
|
|
n.Id = util.BytesToUint64(bytes[4:12])
|
2012-06-29 15:53:47 +08:00
|
|
|
n.Size = util.BytesToUint32(bytes[12:16])
|
|
|
|
rest := 8 - ((n.Size + 16 + 4) % 8)
|
|
|
|
r.Seek(int64(n.Size+4+rest), 1)
|
|
|
|
return n, 16 + n.Size + 4 + rest
|
2011-12-16 22:51:26 +08:00
|
|
|
}
|
2012-06-29 15:53:47 +08:00
|
|
|
func ParseKeyHash(key_hash_string string) (uint64, uint32) {
|
|
|
|
key_hash_bytes, khe := hex.DecodeString(key_hash_string)
|
|
|
|
key_hash_len := len(key_hash_bytes)
|
|
|
|
if khe != nil || key_hash_len <= 4 {
|
|
|
|
log.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
|
|
|
}
|