You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.0 KiB

6 years ago
12 years ago
12 years ago
5 years ago
9 years ago
7 years ago
12 years ago
  1. package needle
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  10. )
  11. const (
  12. NeedleChecksumSize = 4
  13. PairNamePrefix = "Seaweed-"
  14. )
  15. /*
  16. * A Needle means a uploaded and stored file.
  17. * Needle file size is limited to 4GB for now.
  18. */
  19. type Needle struct {
  20. Cookie Cookie `comment:"random number to mitigate brute force lookups"`
  21. Id NeedleId `comment:"needle id"`
  22. Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
  23. DataSize uint32 `comment:"Data size"` //version2
  24. Data []byte `comment:"The actual file data"`
  25. Flags byte `comment:"boolean flags"` //version2
  26. NameSize uint8 //version2
  27. Name []byte `comment:"maximum 256 characters"` //version2
  28. MimeSize uint8 //version2
  29. Mime []byte `comment:"maximum 256 characters"` //version2
  30. PairsSize uint16 //version2
  31. Pairs []byte `comment:"additional name value pairs, json format, maximum 64kB"`
  32. LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk
  33. Ttl *TTL
  34. Checksum CRC `comment:"CRC32 to check integrity"`
  35. AppendAtNs uint64 `comment:"append timestamp in nano seconds"` //version3
  36. Padding []byte `comment:"Aligned to 8 bytes"`
  37. }
  38. func (n *Needle) String() (str string) {
  39. str = fmt.Sprintf("%s Size:%d, DataSize:%d, Name:%s, Mime:%s", formatNeedleIdCookie(n.Id, n.Cookie), n.Size, n.DataSize, n.Name, n.Mime)
  40. return
  41. }
  42. func CreateNeedleFromRequest(r *http.Request, sizeLimit int64) (n *Needle, originalSize int, e error) {
  43. n = new(Needle)
  44. pu, e := ParseUpload(r, sizeLimit)
  45. if e != nil {
  46. return
  47. }
  48. n.Data = pu.Data
  49. originalSize = pu.OriginalDataSize
  50. n.LastModified = pu.ModifiedTime
  51. n.Ttl = pu.Ttl
  52. if len(pu.FileName) < 256 {
  53. n.Name = []byte(pu.FileName)
  54. n.SetHasName()
  55. }
  56. if len(pu.MimeType) < 256 {
  57. n.Mime = []byte(pu.MimeType)
  58. n.SetHasMime()
  59. }
  60. if len(pu.PairMap) != 0 {
  61. trimmedPairMap := make(map[string]string)
  62. for k, v := range pu.PairMap {
  63. trimmedPairMap[k[len(PairNamePrefix):]] = v
  64. }
  65. pairs, _ := json.Marshal(trimmedPairMap)
  66. if len(pairs) < 65536 {
  67. n.Pairs = pairs
  68. n.PairsSize = uint16(len(pairs))
  69. n.SetHasPairs()
  70. }
  71. }
  72. if pu.IsGzipped {
  73. n.SetIsCompressed()
  74. }
  75. if n.LastModified == 0 {
  76. n.LastModified = uint64(time.Now().Unix())
  77. }
  78. n.SetHasLastModifiedDate()
  79. if n.Ttl != EMPTY_TTL {
  80. n.SetHasTtl()
  81. }
  82. if pu.IsChunkedFile {
  83. n.SetIsChunkManifest()
  84. }
  85. n.Checksum = NewCRC(n.Data)
  86. commaSep := strings.LastIndex(r.URL.Path, ",")
  87. dotSep := strings.LastIndex(r.URL.Path, ".")
  88. fid := r.URL.Path[commaSep+1:]
  89. if dotSep > 0 {
  90. fid = r.URL.Path[commaSep+1 : dotSep]
  91. }
  92. e = n.ParsePath(fid)
  93. return
  94. }
  95. func (n *Needle) ParsePath(fid string) (err error) {
  96. length := len(fid)
  97. if length <= CookieSize*2 {
  98. return fmt.Errorf("Invalid fid: %s", fid)
  99. }
  100. delta := ""
  101. deltaIndex := strings.LastIndex(fid, "_")
  102. if deltaIndex > 0 {
  103. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  104. }
  105. n.Id, n.Cookie, err = ParseNeedleIdCookie(fid)
  106. if err != nil {
  107. return err
  108. }
  109. if delta != "" {
  110. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  111. n.Id += Uint64ToNeedleId(d)
  112. } else {
  113. return e
  114. }
  115. }
  116. return err
  117. }
  118. func ParseNeedleIdCookie(key_hash_string string) (NeedleId, Cookie, error) {
  119. if len(key_hash_string) <= CookieSize*2 {
  120. return NeedleIdEmpty, 0, fmt.Errorf("KeyHash is too short.")
  121. }
  122. if len(key_hash_string) > (NeedleIdSize+CookieSize)*2 {
  123. return NeedleIdEmpty, 0, fmt.Errorf("KeyHash is too long.")
  124. }
  125. split := len(key_hash_string) - CookieSize*2
  126. needleId, err := ParseNeedleId(key_hash_string[:split])
  127. if err != nil {
  128. return NeedleIdEmpty, 0, fmt.Errorf("Parse needleId error: %v", err)
  129. }
  130. cookie, err := ParseCookie(key_hash_string[split:])
  131. if err != nil {
  132. return NeedleIdEmpty, 0, fmt.Errorf("Parse cookie error: %v", err)
  133. }
  134. return needleId, cookie, nil
  135. }
  136. func (n *Needle) LastModifiedString() string {
  137. return time.Unix(int64(n.LastModified), 0).Format("2006-01-02T15:04:05")
  138. }