2019-04-19 04:43:36 +00:00
|
|
|
package needle
|
2011-12-16 14:51:26 +00:00
|
|
|
|
|
|
|
import (
|
2021-06-06 20:42:36 +00:00
|
|
|
"bytes"
|
2017-01-08 14:34:26 +00:00
|
|
|
"encoding/json"
|
2014-12-26 05:29:44 +00:00
|
|
|
"fmt"
|
2012-06-29 07:53:47 +00:00
|
|
|
"net/http"
|
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"
|
2014-10-26 18:34:55 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/images"
|
|
|
|
. "github.com/seaweedfs/seaweedfs/weed/storage/types"
|
2011-12-16 14:51:26 +00:00
|
|
|
)
|
|
|
|
|
2012-12-21 08:36:55 +00:00
|
|
|
const (
|
2018-07-08 09:28:04 +00:00
|
|
|
NeedleChecksumSize = 4
|
|
|
|
PairNamePrefix = "Seaweed-"
|
2012-12-21 08:36:55 +00:00
|
|
|
)
|
|
|
|
|
2013-09-19 18:06:14 +00:00
|
|
|
/*
|
2014-12-26 07:36:33 +00:00
|
|
|
* A Needle means a uploaded and stored file.
|
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 {
|
2018-07-08 09:28:04 +00:00
|
|
|
Cookie Cookie `comment:"random number to mitigate brute force lookups"`
|
|
|
|
Id NeedleId `comment:"needle id"`
|
2020-08-30 05:28:33 +00:00
|
|
|
Size Size `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"`
|
2019-01-17 01:17:19 +00:00
|
|
|
Flags byte `comment:"boolean flags"` //version2
|
|
|
|
NameSize uint8 //version2
|
2021-09-27 08:58:03 +00:00
|
|
|
Name []byte `comment:"maximum 255 characters"` //version2
|
2019-01-17 01:17:19 +00:00
|
|
|
MimeSize uint8 //version2
|
2021-09-27 08:58:03 +00:00
|
|
|
Mime []byte `comment:"maximum 255 characters"` //version2
|
2019-01-17 01:17:19 +00:00
|
|
|
PairsSize uint16 //version2
|
2017-01-08 01:16:29 +00:00
|
|
|
Pairs []byte `comment:"additional name value pairs, json format, maximum 64kB"`
|
2013-07-09 06:38:38 +00:00
|
|
|
LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk
|
2014-09-20 19:38:59 +00:00
|
|
|
Ttl *TTL
|
2012-12-21 10:13:02 +00:00
|
|
|
|
2018-07-24 08:36:04 +00:00
|
|
|
Checksum CRC `comment:"CRC32 to check integrity"`
|
|
|
|
AppendAtNs uint64 `comment:"append timestamp in nano seconds"` //version3
|
|
|
|
Padding []byte `comment:"Aligned to 8 bytes"`
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2014-12-26 05:29:44 +00:00
|
|
|
func (n *Needle) String() (str string) {
|
2020-08-01 18:16:16 +00:00
|
|
|
str = fmt.Sprintf("%s Size:%d, DataSize:%d, Name:%s, Mime:%s Compressed:%v", formatNeedleIdCookie(n.Id, n.Cookie), n.Size, n.DataSize, n.Name, n.Mime, n.IsCompressed())
|
2014-12-26 05:29:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-06 20:42:36 +00:00
|
|
|
func CreateNeedleFromRequest(r *http.Request, fixJpgOrientation bool, sizeLimit int64, bytesBuffer *bytes.Buffer) (n *Needle, originalSize int, contentMd5 string, e error) {
|
2013-08-06 18:23:24 +00:00
|
|
|
n = new(Needle)
|
2021-06-06 20:42:36 +00:00
|
|
|
pu, e := ParseUpload(r, sizeLimit, bytesBuffer)
|
2013-08-06 18:23:24 +00:00
|
|
|
if e != nil {
|
2013-09-02 06:58:21 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-07 14:06:58 +00:00
|
|
|
n.Data = pu.Data
|
|
|
|
originalSize = pu.OriginalDataSize
|
|
|
|
n.LastModified = pu.ModifiedTime
|
|
|
|
n.Ttl = pu.Ttl
|
2020-08-06 17:04:17 +00:00
|
|
|
contentMd5 = pu.ContentMd5
|
2020-03-07 14:06:58 +00:00
|
|
|
|
|
|
|
if len(pu.FileName) < 256 {
|
|
|
|
n.Name = []byte(pu.FileName)
|
2013-09-02 06:58:21 +00:00
|
|
|
n.SetHasName()
|
2013-08-06 18:23:24 +00:00
|
|
|
}
|
2020-03-07 14:06:58 +00:00
|
|
|
if len(pu.MimeType) < 256 {
|
|
|
|
n.Mime = []byte(pu.MimeType)
|
2013-08-06 18:23:24 +00:00
|
|
|
n.SetHasMime()
|
|
|
|
}
|
2020-03-07 14:06:58 +00:00
|
|
|
if len(pu.PairMap) != 0 {
|
2017-01-08 14:34:26 +00:00
|
|
|
trimmedPairMap := make(map[string]string)
|
2020-03-07 14:06:58 +00:00
|
|
|
for k, v := range pu.PairMap {
|
2017-01-08 14:34:26 +00:00
|
|
|
trimmedPairMap[k[len(PairNamePrefix):]] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
pairs, _ := json.Marshal(trimmedPairMap)
|
|
|
|
if len(pairs) < 65536 {
|
|
|
|
n.Pairs = pairs
|
|
|
|
n.PairsSize = uint16(len(pairs))
|
|
|
|
n.SetHasPairs()
|
|
|
|
}
|
2017-01-08 01:16:29 +00:00
|
|
|
}
|
2020-03-07 14:06:58 +00:00
|
|
|
if pu.IsGzipped {
|
2020-08-01 18:16:16 +00:00
|
|
|
// println(r.URL.Path, "is set to compressed", pu.FileName, pu.IsGzipped, "dataSize", pu.OriginalDataSize)
|
2020-06-20 05:45:27 +00:00
|
|
|
n.SetIsCompressed()
|
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-10-16 15:39:09 +00:00
|
|
|
n.SetHasLastModifiedDate()
|
2014-09-21 03:51:24 +00:00
|
|
|
if n.Ttl != EMPTY_TTL {
|
2014-09-20 19:38:59 +00:00
|
|
|
n.SetHasTtl()
|
|
|
|
}
|
2012-12-22 10:10:45 +00:00
|
|
|
|
2020-03-07 14:06:58 +00:00
|
|
|
if pu.IsChunkedFile {
|
2015-12-15 06:38:58 +00:00
|
|
|
n.SetIsChunkManifest()
|
2015-12-01 12:23:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 02:08:36 +00:00
|
|
|
if fixJpgOrientation {
|
|
|
|
loweredName := strings.ToLower(pu.FileName)
|
|
|
|
if pu.MimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
|
|
|
|
n.Data = images.FixJpgOrientation(n.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-07-24 08:38:08 +00:00
|
|
|
fid = r.URL.Path[commaSep+1 : dotSep]
|
2011-12-22 04:04:47 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 21:53:24 +00:00
|
|
|
e = n.ParsePath(fid)
|
2011-12-19 05:59:37 +00:00
|
|
|
|
|
|
|
return
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-12-09 21:53:24 +00:00
|
|
|
func (n *Needle) ParsePath(fid string) (err error) {
|
2011-12-22 04:04:47 +00:00
|
|
|
length := len(fid)
|
2018-07-08 09:28:04 +00:00
|
|
|
if length <= CookieSize*2 {
|
2016-04-10 08:50:58 +00:00
|
|
|
return fmt.Errorf("Invalid fid: %s", fid)
|
2011-12-20 09:00:01 +00:00
|
|
|
}
|
2012-07-16 17:15:16 +00:00
|
|
|
delta := ""
|
|
|
|
deltaIndex := strings.LastIndex(fid, "_")
|
|
|
|
if deltaIndex > 0 {
|
|
|
|
fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
|
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
n.Id, n.Cookie, err = ParseNeedleIdCookie(fid)
|
2013-12-09 21:53:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2012-07-16 17:15:16 +00:00
|
|
|
if delta != "" {
|
2013-12-09 21:53:24 +00:00
|
|
|
if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
|
2019-06-21 08:14:10 +00:00
|
|
|
n.Id += Uint64ToNeedleId(d)
|
2013-12-09 21:53:24 +00:00
|
|
|
} else {
|
|
|
|
return e
|
2012-07-16 17:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-09 21:53:24 +00:00
|
|
|
return err
|
2011-12-16 14:51:26 +00:00
|
|
|
}
|
2013-01-21 03:44:23 +00:00
|
|
|
|
2022-10-24 01:24:52 +00:00
|
|
|
func GetAppendAtNs(volumeLastAppendAtNs uint64) uint64 {
|
|
|
|
return max(uint64(time.Now().UnixNano()), volumeLastAppendAtNs+1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Needle) UpdateAppendAtNs(volumeLastAppendAtNs uint64) {
|
|
|
|
n.AppendAtNs = max(uint64(time.Now().UnixNano()), volumeLastAppendAtNs+1)
|
|
|
|
}
|
|
|
|
|
2018-07-08 09:28:04 +00:00
|
|
|
func ParseNeedleIdCookie(key_hash_string string) (NeedleId, Cookie, error) {
|
|
|
|
if len(key_hash_string) <= CookieSize*2 {
|
2018-08-01 06:25:26 +00:00
|
|
|
return NeedleIdEmpty, 0, fmt.Errorf("KeyHash is too short.")
|
2012-12-21 08:36:55 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
if len(key_hash_string) > (NeedleIdSize+CookieSize)*2 {
|
2018-08-01 06:25:26 +00:00
|
|
|
return NeedleIdEmpty, 0, fmt.Errorf("KeyHash is too long.")
|
2016-04-10 07:54:40 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
split := len(key_hash_string) - CookieSize*2
|
|
|
|
needleId, err := ParseNeedleId(key_hash_string[:split])
|
2016-04-10 07:54:40 +00:00
|
|
|
if err != nil {
|
2018-08-01 06:25:26 +00:00
|
|
|
return NeedleIdEmpty, 0, fmt.Errorf("Parse needleId error: %v", err)
|
2016-04-10 07:54:40 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
cookie, err := ParseCookie(key_hash_string[split:])
|
2016-04-10 07:54:40 +00:00
|
|
|
if err != nil {
|
2018-08-01 06:25:26 +00:00
|
|
|
return NeedleIdEmpty, 0, fmt.Errorf("Parse cookie error: %v", err)
|
2016-04-10 07:54:40 +00:00
|
|
|
}
|
2018-07-08 09:28:04 +00:00
|
|
|
return needleId, cookie, nil
|
2016-04-10 07:54:40 +00:00
|
|
|
}
|
2018-07-15 03:51:17 +00:00
|
|
|
|
|
|
|
func (n *Needle) LastModifiedString() string {
|
|
|
|
return time.Unix(int64(n.LastModified), 0).Format("2006-01-02T15:04:05")
|
|
|
|
}
|
2022-10-24 01:24:52 +00:00
|
|
|
|
|
|
|
func max(x, y uint64) uint64 {
|
|
|
|
if x <= y {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|