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.

138 lines
3.1 KiB

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