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.

162 lines
3.9 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 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. "io/ioutil"
  7. "log"
  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 ParseUpload(r *http.Request) (fileName string, data []byte, mimeType string, isGzipped bool, modifiedTime uint64, e error) {
  36. form, fe := r.MultipartReader()
  37. if fe != nil {
  38. log.Println("MultipartReader [ERROR]", fe)
  39. e = fe
  40. return
  41. }
  42. part, fe := form.NextPart()
  43. if fe != nil {
  44. log.Println("Reading Multi part [ERROR]", fe)
  45. e = fe
  46. return
  47. }
  48. fileName = part.FileName()
  49. if fileName != "" {
  50. fileName = path.Base(fileName)
  51. } else {
  52. e = errors.New("No file found!")
  53. return
  54. }
  55. data, e = ioutil.ReadAll(part)
  56. if e != nil {
  57. log.Println("Reading Content [ERROR]", e)
  58. return
  59. }
  60. dotIndex := strings.LastIndex(fileName, ".")
  61. ext, mtype := "", ""
  62. if dotIndex > 0 {
  63. ext = strings.ToLower(fileName[dotIndex:])
  64. mtype = mime.TypeByExtension(ext)
  65. }
  66. contentType := part.Header.Get("Content-Type")
  67. if contentType != "" && mtype != contentType {
  68. mimeType = contentType //only return mime type if not deductable
  69. mtype = contentType
  70. }
  71. if part.Header.Get("Content-Encoding") == "gzip" {
  72. isGzipped = true
  73. } else if IsGzippable(ext, mtype) {
  74. if data, e = GzipData(data); e != nil {
  75. return
  76. }
  77. isGzipped = true
  78. }
  79. if ext == ".gz" {
  80. isGzipped = true
  81. }
  82. if strings.HasSuffix(fileName, ".gz") {
  83. fileName = fileName[:len(fileName)-3]
  84. }
  85. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  86. return
  87. }
  88. func NewNeedle(r *http.Request) (n *Needle, e error) {
  89. fname, mimeType, isGzipped := "", "", false
  90. n = new(Needle)
  91. fname, n.Data, mimeType, isGzipped, n.LastModified, e = ParseUpload(r)
  92. if e != nil {
  93. return
  94. }
  95. if len(fname) < 256 {
  96. n.Name = []byte(fname)
  97. n.SetHasName()
  98. }
  99. if len(mimeType) < 256 {
  100. n.Mime = []byte(mimeType)
  101. n.SetHasMime()
  102. }
  103. if isGzipped {
  104. n.SetGzipped()
  105. }
  106. if n.LastModified == 0 {
  107. n.LastModified = uint64(time.Now().Unix())
  108. n.SetHasLastModifiedDate()
  109. }
  110. n.Checksum = NewCRC(n.Data)
  111. commaSep := strings.LastIndex(r.URL.Path, ",")
  112. dotSep := strings.LastIndex(r.URL.Path, ".")
  113. fid := r.URL.Path[commaSep+1:]
  114. if dotSep > 0 {
  115. fid = r.URL.Path[commaSep+1 : dotSep]
  116. }
  117. n.ParsePath(fid)
  118. return
  119. }
  120. func (n *Needle) ParsePath(fid string) {
  121. length := len(fid)
  122. if length <= 8 {
  123. if length > 0 {
  124. log.Println("Invalid fid", fid, "length", length)
  125. }
  126. return
  127. }
  128. delta := ""
  129. deltaIndex := strings.LastIndex(fid, "_")
  130. if deltaIndex > 0 {
  131. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  132. }
  133. n.Id, n.Cookie = ParseKeyHash(fid)
  134. if delta != "" {
  135. d, e := strconv.ParseUint(delta, 10, 64)
  136. if e == nil {
  137. n.Id += d
  138. }
  139. }
  140. }
  141. func ParseKeyHash(key_hash_string string) (uint64, uint32) {
  142. key_hash_bytes, khe := hex.DecodeString(key_hash_string)
  143. key_hash_len := len(key_hash_bytes)
  144. if khe != nil || key_hash_len <= 4 {
  145. log.Println("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
  146. return 0, 0
  147. }
  148. key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
  149. hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
  150. return key, hash
  151. }