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.

145 lines
3.4 KiB

12 years ago
12 years ago
12 years ago
12 years ago
13 years ago
12 years ago
  1. package storage
  2. import (
  3. "code.google.com/p/weed-fs/go/util"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "io/ioutil"
  8. "mime"
  9. "net/http"
  10. "path"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. const (
  16. NeedleHeaderSize = 16 //should never change this
  17. NeedlePaddingSize = 8
  18. NeedleChecksumSize = 4
  19. )
  20. type Needle struct {
  21. Cookie uint32 `comment:"random number to mitigate brute force lookups"`
  22. Id uint64 `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. LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk
  32. Checksum CRC `comment:"CRC32 to check integrity"`
  33. Padding []byte `comment:"Aligned to 8 bytes"`
  34. }
  35. func NewNeedle(r *http.Request) (n *Needle, e error) {
  36. n = new(Needle)
  37. form, fe := r.MultipartReader()
  38. if fe != nil {
  39. fmt.Println("MultipartReader [ERROR]", fe)
  40. e = fe
  41. return
  42. }
  43. part, fe := form.NextPart()
  44. if fe != nil {
  45. fmt.Println("Reading Multi part [ERROR]", fe)
  46. e = fe
  47. return
  48. }
  49. fname := part.FileName()
  50. if fname != "" {
  51. fname = path.Base(part.FileName())
  52. } else {
  53. e = errors.New("No file found!")
  54. return
  55. }
  56. data, _ := ioutil.ReadAll(part)
  57. dotIndex := strings.LastIndex(fname, ".")
  58. ext, mtype := "", ""
  59. if dotIndex > 0 {
  60. ext = fname[dotIndex:]
  61. mtype = mime.TypeByExtension(ext)
  62. }
  63. contentType := part.Header.Get("Content-Type")
  64. if contentType != "" && mtype != contentType && len(contentType) < 256 {
  65. n.Mime = []byte(contentType)
  66. n.SetHasMime()
  67. mtype = contentType
  68. }
  69. if IsGzippable(ext, mtype) {
  70. if data, e = GzipData(data); e != nil {
  71. return
  72. }
  73. n.SetGzipped()
  74. }
  75. if ext == ".gz" {
  76. n.SetGzipped()
  77. }
  78. if len(fname) < 256 {
  79. if strings.HasSuffix(fname, ".gz") {
  80. n.Name = []byte(fname[:len(fname)-3])
  81. } else {
  82. n.Name = []byte(fname)
  83. }
  84. n.SetHasName()
  85. }
  86. var parseError error
  87. if n.LastModified, parseError = strconv.ParseUint(r.FormValue("ts"), 10, 64); parseError != nil {
  88. n.LastModified = uint64(time.Now().Unix())
  89. }
  90. n.SetHasLastModifiedDate()
  91. n.Data = data
  92. n.Checksum = NewCRC(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. n.ParsePath(fid)
  100. return
  101. }
  102. func (n *Needle) ParsePath(fid string) {
  103. length := len(fid)
  104. if length <= 8 {
  105. if length > 0 {
  106. println("Invalid fid", fid, "length", length)
  107. }
  108. return
  109. }
  110. delta := ""
  111. deltaIndex := strings.LastIndex(fid, "_")
  112. if deltaIndex > 0 {
  113. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  114. }
  115. n.Id, n.Cookie = ParseKeyHash(fid)
  116. if delta != "" {
  117. d, e := strconv.ParseUint(delta, 10, 64)
  118. if e == nil {
  119. n.Id += d
  120. }
  121. }
  122. }
  123. func ParseKeyHash(key_hash_string string) (uint64, uint32) {
  124. key_hash_bytes, khe := hex.DecodeString(key_hash_string)
  125. key_hash_len := len(key_hash_bytes)
  126. if khe != nil || key_hash_len <= 4 {
  127. println("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
  128. return 0, 0
  129. }
  130. key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
  131. hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
  132. return key, hash
  133. }