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.

179 lines
4.8 KiB

12 years ago
12 years ago
12 years ago
9 years ago
9 years ago
9 years ago
12 years ago
  1. package storage
  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. Padding []byte `comment:"Aligned to 8 bytes"`
  37. }
  38. func (n *Needle) String() (str string) {
  39. str = fmt.Sprintf("Cookie:%d, Id:%d, Size:%d, DataSize:%d, Name: %s, Mime: %s", n.Cookie, n.Id, n.Size, n.DataSize, n.Name, n.Mime)
  40. return
  41. }
  42. func ParseUpload(r *http.Request) (
  43. fileName string, data []byte, mimeType string, pairMap map[string]string, isGzipped bool,
  44. modifiedTime uint64, ttl *TTL, isChunkedFile bool, e error) {
  45. pairMap = make(map[string]string)
  46. for k, v := range r.Header {
  47. if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) {
  48. pairMap[k] = v[0]
  49. }
  50. }
  51. fileName, data, mimeType, isGzipped, isChunkedFile, e = parseMultipart(r)
  52. if e != nil {
  53. return
  54. }
  55. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  56. ttl, _ = ReadTTL(r.FormValue("ttl"))
  57. return
  58. }
  59. func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) {
  60. var pairMap map[string]string
  61. fname, mimeType, isGzipped, isChunkedFile := "", "", false, false
  62. n = new(Needle)
  63. fname, n.Data, mimeType, pairMap, isGzipped, n.LastModified, n.Ttl, isChunkedFile, e = ParseUpload(r)
  64. if e != nil {
  65. return
  66. }
  67. if len(fname) < 256 {
  68. n.Name = []byte(fname)
  69. n.SetHasName()
  70. }
  71. if len(mimeType) < 256 {
  72. n.Mime = []byte(mimeType)
  73. n.SetHasMime()
  74. }
  75. if len(pairMap) != 0 {
  76. trimmedPairMap := make(map[string]string)
  77. for k, v := range pairMap {
  78. trimmedPairMap[k[len(PairNamePrefix):]] = v
  79. }
  80. pairs, _ := json.Marshal(trimmedPairMap)
  81. if len(pairs) < 65536 {
  82. n.Pairs = pairs
  83. n.PairsSize = uint16(len(pairs))
  84. n.SetHasPairs()
  85. }
  86. }
  87. if isGzipped {
  88. n.SetGzipped()
  89. }
  90. if n.LastModified == 0 {
  91. n.LastModified = uint64(time.Now().Unix())
  92. }
  93. n.SetHasLastModifiedDate()
  94. if n.Ttl != EMPTY_TTL {
  95. n.SetHasTtl()
  96. }
  97. if isChunkedFile {
  98. n.SetIsChunkManifest()
  99. }
  100. if fixJpgOrientation {
  101. loweredName := strings.ToLower(fname)
  102. if mimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
  103. n.Data = images.FixJpgOrientation(n.Data)
  104. }
  105. }
  106. n.Checksum = NewCRC(n.Data)
  107. commaSep := strings.LastIndex(r.URL.Path, ",")
  108. dotSep := strings.LastIndex(r.URL.Path, ".")
  109. fid := r.URL.Path[commaSep+1:]
  110. if dotSep > 0 {
  111. fid = r.URL.Path[commaSep+1: dotSep]
  112. }
  113. e = n.ParsePath(fid)
  114. return
  115. }
  116. func (n *Needle) ParsePath(fid string) (err error) {
  117. length := len(fid)
  118. if length <= CookieSize*2 {
  119. return fmt.Errorf("Invalid fid: %s", fid)
  120. }
  121. delta := ""
  122. deltaIndex := strings.LastIndex(fid, "_")
  123. if deltaIndex > 0 {
  124. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  125. }
  126. n.Id, n.Cookie, err = ParseNeedleIdCookie(fid)
  127. if err != nil {
  128. return err
  129. }
  130. if delta != "" {
  131. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  132. n.Id += NeedleId(d)
  133. } else {
  134. return e
  135. }
  136. }
  137. return err
  138. }
  139. func ParseNeedleIdCookie(key_hash_string string) (NeedleId, Cookie, error) {
  140. if len(key_hash_string) <= CookieSize*2 {
  141. return 0, 0, fmt.Errorf("KeyHash is too short.")
  142. }
  143. if len(key_hash_string) > (NeedleIdSize+CookieSize)*2 {
  144. return 0, 0, fmt.Errorf("KeyHash is too long.")
  145. }
  146. split := len(key_hash_string) - CookieSize*2
  147. needleId, err := ParseNeedleId(key_hash_string[:split])
  148. if err != nil {
  149. return 0, 0, fmt.Errorf("Parse needleId error: %v", err)
  150. }
  151. cookie, err := ParseCookie(key_hash_string[split:])
  152. if err != nil {
  153. return 0, 0, fmt.Errorf("Parse cookie error: %v", err)
  154. }
  155. return needleId, cookie, nil
  156. }
  157. func (n *Needle) LastModifiedString() string {
  158. return time.Unix(int64(n.LastModified), 0).Format("2006-01-02T15:04:05")
  159. }