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.

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