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.

164 lines
4.4 KiB

6 years ago
12 years ago
12 years ago
5 years ago
5 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/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 Compressed:%v", formatNeedleIdCookie(n.Id, n.Cookie), n.Size, n.DataSize, n.Name, n.Mime, n.IsCompressed())
  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. // println(r.URL.Path, "is set to compressed", pu.FileName, pu.IsGzipped, "dataSize", pu.OriginalDataSize)
  75. n.SetIsCompressed()
  76. }
  77. if n.LastModified == 0 {
  78. n.LastModified = uint64(time.Now().Unix())
  79. }
  80. n.SetHasLastModifiedDate()
  81. if n.Ttl != EMPTY_TTL {
  82. n.SetHasTtl()
  83. }
  84. if pu.IsChunkedFile {
  85. n.SetIsChunkManifest()
  86. }
  87. if fixJpgOrientation {
  88. loweredName := strings.ToLower(pu.FileName)
  89. if pu.MimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
  90. n.Data = images.FixJpgOrientation(n.Data)
  91. }
  92. }
  93. n.Checksum = NewCRC(n.Data)
  94. commaSep := strings.LastIndex(r.URL.Path, ",")
  95. dotSep := strings.LastIndex(r.URL.Path, ".")
  96. fid := r.URL.Path[commaSep+1:]
  97. if dotSep > 0 {
  98. fid = r.URL.Path[commaSep+1 : dotSep]
  99. }
  100. e = n.ParsePath(fid)
  101. return
  102. }
  103. func (n *Needle) ParsePath(fid string) (err error) {
  104. length := len(fid)
  105. if length <= CookieSize*2 {
  106. return fmt.Errorf("Invalid fid: %s", fid)
  107. }
  108. delta := ""
  109. deltaIndex := strings.LastIndex(fid, "_")
  110. if deltaIndex > 0 {
  111. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  112. }
  113. n.Id, n.Cookie, err = ParseNeedleIdCookie(fid)
  114. if err != nil {
  115. return err
  116. }
  117. if delta != "" {
  118. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  119. n.Id += Uint64ToNeedleId(d)
  120. } else {
  121. return e
  122. }
  123. }
  124. return err
  125. }
  126. func ParseNeedleIdCookie(key_hash_string string) (NeedleId, Cookie, error) {
  127. if len(key_hash_string) <= CookieSize*2 {
  128. return NeedleIdEmpty, 0, fmt.Errorf("KeyHash is too short.")
  129. }
  130. if len(key_hash_string) > (NeedleIdSize+CookieSize)*2 {
  131. return NeedleIdEmpty, 0, fmt.Errorf("KeyHash is too long.")
  132. }
  133. split := len(key_hash_string) - CookieSize*2
  134. needleId, err := ParseNeedleId(key_hash_string[:split])
  135. if err != nil {
  136. return NeedleIdEmpty, 0, fmt.Errorf("Parse needleId error: %v", err)
  137. }
  138. cookie, err := ParseCookie(key_hash_string[split:])
  139. if err != nil {
  140. return NeedleIdEmpty, 0, fmt.Errorf("Parse cookie error: %v", err)
  141. }
  142. return needleId, cookie, nil
  143. }
  144. func (n *Needle) LastModifiedString() string {
  145. return time.Unix(int64(n.LastModified), 0).Format("2006-01-02T15:04:05")
  146. }