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.

165 lines
4.3 KiB

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