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.

159 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/glog"
  4. "code.google.com/p/weed-fs/go/util"
  5. "encoding/hex"
  6. "io/ioutil"
  7. "mime"
  8. "net/http"
  9. "path"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. const (
  15. NeedleHeaderSize = 16 //should never change this
  16. NeedlePaddingSize = 8
  17. NeedleChecksumSize = 4
  18. )
  19. /*
  20. * Needle file size is limited to 4GB for now.
  21. */
  22. type Needle struct {
  23. Cookie uint32 `comment:"random number to mitigate brute force lookups"`
  24. Id uint64 `comment:"needle id"`
  25. Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
  26. DataSize uint32 `comment:"Data size"` //version2
  27. Data []byte `comment:"The actual file data"`
  28. Flags byte `comment:"boolean flags"` //version2
  29. NameSize uint8 //version2
  30. Name []byte `comment:"maximum 256 characters"` //version2
  31. MimeSize uint8 //version2
  32. Mime []byte `comment:"maximum 256 characters"` //version2
  33. LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk
  34. Checksum CRC `comment:"CRC32 to check integrity"`
  35. Padding []byte `comment:"Aligned to 8 bytes"`
  36. }
  37. func ParseUpload(r *http.Request) (fileName string, data []byte, mimeType string, isGzipped bool, modifiedTime uint64, e error) {
  38. form, fe := r.MultipartReader()
  39. if fe != nil {
  40. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  41. e = fe
  42. return
  43. }
  44. part, fe := form.NextPart()
  45. if fe != nil {
  46. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  47. e = fe
  48. return
  49. }
  50. fileName = part.FileName()
  51. if fileName != "" {
  52. fileName = path.Base(fileName)
  53. }
  54. data, e = ioutil.ReadAll(part)
  55. if e != nil {
  56. glog.V(0).Infoln("Reading Content [ERROR]", e)
  57. return
  58. }
  59. dotIndex := strings.LastIndex(fileName, ".")
  60. ext, mtype := "", ""
  61. if dotIndex > 0 {
  62. ext = strings.ToLower(fileName[dotIndex:])
  63. mtype = mime.TypeByExtension(ext)
  64. }
  65. contentType := part.Header.Get("Content-Type")
  66. if contentType != "" && mtype != contentType {
  67. mimeType = contentType //only return mime type if not deductable
  68. mtype = contentType
  69. }
  70. if part.Header.Get("Content-Encoding") == "gzip" {
  71. isGzipped = true
  72. } else if IsGzippable(ext, mtype) {
  73. if data, e = GzipData(data); e != nil {
  74. return
  75. }
  76. isGzipped = true
  77. }
  78. if ext == ".gz" {
  79. isGzipped = true
  80. }
  81. if strings.HasSuffix(fileName, ".gz") {
  82. fileName = fileName[:len(fileName)-3]
  83. }
  84. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  85. return
  86. }
  87. func NewNeedle(r *http.Request) (n *Needle, e error) {
  88. fname, mimeType, isGzipped := "", "", false
  89. n = new(Needle)
  90. fname, n.Data, mimeType, isGzipped, n.LastModified, e = ParseUpload(r)
  91. if e != nil {
  92. return
  93. }
  94. if len(fname) < 256 {
  95. n.Name = []byte(fname)
  96. n.SetHasName()
  97. }
  98. if len(mimeType) < 256 {
  99. n.Mime = []byte(mimeType)
  100. n.SetHasMime()
  101. }
  102. if isGzipped {
  103. n.SetGzipped()
  104. }
  105. if n.LastModified == 0 {
  106. n.LastModified = uint64(time.Now().Unix())
  107. }
  108. n.SetHasLastModifiedDate()
  109. n.Checksum = NewCRC(n.Data)
  110. commaSep := strings.LastIndex(r.URL.Path, ",")
  111. dotSep := strings.LastIndex(r.URL.Path, ".")
  112. fid := r.URL.Path[commaSep+1:]
  113. if dotSep > 0 {
  114. fid = r.URL.Path[commaSep+1 : dotSep]
  115. }
  116. n.ParsePath(fid)
  117. return
  118. }
  119. func (n *Needle) ParsePath(fid string) {
  120. length := len(fid)
  121. if length <= 8 {
  122. return
  123. }
  124. delta := ""
  125. deltaIndex := strings.LastIndex(fid, "_")
  126. if deltaIndex > 0 {
  127. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  128. }
  129. n.Id, n.Cookie = ParseKeyHash(fid)
  130. if delta != "" {
  131. d, e := strconv.ParseUint(delta, 10, 64)
  132. if e == nil {
  133. n.Id += d
  134. }
  135. }
  136. }
  137. func ParseKeyHash(key_hash_string string) (uint64, uint32) {
  138. key_hash_bytes, khe := hex.DecodeString(key_hash_string)
  139. key_hash_len := len(key_hash_bytes)
  140. if khe != nil || key_hash_len <= 4 {
  141. glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
  142. return 0, 0
  143. }
  144. key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
  145. hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
  146. return key, hash
  147. }