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.

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