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.

165 lines
4.1 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. "errors"
  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. /*
  21. * Needle file size is limited to 4GB for now.
  22. */
  23. type Needle struct {
  24. Cookie uint32 `comment:"random number to mitigate brute force lookups"`
  25. Id uint64 `comment:"needle id"`
  26. Size uint32 `comment:"sum of DataSize,Data,NameSize,Name,MimeSize,Mime"`
  27. DataSize uint32 `comment:"Data size"` //version2
  28. Data []byte `comment:"The actual file data"`
  29. Flags byte `comment:"boolean flags"` //version2
  30. NameSize uint8 //version2
  31. Name []byte `comment:"maximum 256 characters"` //version2
  32. MimeSize uint8 //version2
  33. Mime []byte `comment:"maximum 256 characters"` //version2
  34. LastModified uint64 //only store LastModifiedBytesLength bytes, which is 5 bytes to disk
  35. Checksum CRC `comment:"CRC32 to check integrity"`
  36. Padding []byte `comment:"Aligned to 8 bytes"`
  37. }
  38. func ParseUpload(r *http.Request) (fileName string, data []byte, mimeType string, isGzipped bool, modifiedTime uint64, e error) {
  39. form, fe := r.MultipartReader()
  40. if fe != nil {
  41. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  42. e = fe
  43. return
  44. }
  45. part, fe := form.NextPart()
  46. if fe != nil {
  47. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  48. e = fe
  49. return
  50. }
  51. fileName = part.FileName()
  52. if fileName != "" {
  53. fileName = path.Base(fileName)
  54. }
  55. data, e = ioutil.ReadAll(part)
  56. if e != nil {
  57. glog.V(0).Infoln("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. }
  109. n.SetHasLastModifiedDate()
  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. e = n.ParsePath(fid)
  118. return
  119. }
  120. func (n *Needle) ParsePath(fid string) (err error) {
  121. length := len(fid)
  122. if length <= 8 {
  123. return errors.New("Invalid fid:" + fid)
  124. }
  125. delta := ""
  126. deltaIndex := strings.LastIndex(fid, "_")
  127. if deltaIndex > 0 {
  128. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  129. }
  130. n.Id, n.Cookie, err = ParseKeyHash(fid)
  131. if err != nil {
  132. return err
  133. }
  134. if delta != "" {
  135. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  136. n.Id += d
  137. } else {
  138. return e
  139. }
  140. }
  141. return err
  142. }
  143. func ParseKeyHash(key_hash_string string) (uint64, uint32, error) {
  144. key_hash_bytes, khe := hex.DecodeString(key_hash_string)
  145. key_hash_len := len(key_hash_bytes)
  146. if khe != nil || key_hash_len <= 4 {
  147. glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
  148. return 0, 0, errors.New("Invalid key and hash:" + key_hash_string)
  149. }
  150. key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
  151. hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
  152. return key, hash, nil
  153. }