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.

196 lines
4.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. "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. for {
  56. if fe != nil {
  57. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  58. e = fe
  59. return
  60. }
  61. if part.FileName() != "" {
  62. break //found the first <file type> multi-part
  63. }
  64. part, fe = form.NextPart()
  65. }
  66. fileName = part.FileName()
  67. if fileName != "" {
  68. fileName = path.Base(fileName)
  69. }
  70. data, e = ioutil.ReadAll(part)
  71. if e != nil {
  72. glog.V(0).Infoln("Reading Content [ERROR]", e)
  73. return
  74. }
  75. dotIndex := strings.LastIndex(fileName, ".")
  76. ext, mtype := "", ""
  77. if dotIndex > 0 {
  78. ext = strings.ToLower(fileName[dotIndex:])
  79. mtype = mime.TypeByExtension(ext)
  80. }
  81. contentType := part.Header.Get("Content-Type")
  82. if contentType != "" && mtype != contentType {
  83. mimeType = contentType //only return mime type if not deductable
  84. mtype = contentType
  85. }
  86. if part.Header.Get("Content-Encoding") == "gzip" {
  87. isGzipped = true
  88. } else if IsGzippable(ext, mtype) {
  89. if data, e = GzipData(data); e != nil {
  90. return
  91. }
  92. isGzipped = true
  93. }
  94. if ext == ".gz" {
  95. isGzipped = true
  96. }
  97. if strings.HasSuffix(fileName, ".gz") {
  98. fileName = fileName[:len(fileName)-3]
  99. }
  100. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  101. ttl, _ = ReadTTL(r.FormValue("ttl"))
  102. return
  103. }
  104. func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) {
  105. fname, mimeType, isGzipped := "", "", false
  106. n = new(Needle)
  107. fname, n.Data, mimeType, isGzipped, n.LastModified, n.Ttl, e = ParseUpload(r)
  108. if e != nil {
  109. return
  110. }
  111. if len(fname) < 256 {
  112. n.Name = []byte(fname)
  113. n.SetHasName()
  114. }
  115. if len(mimeType) < 256 {
  116. n.Mime = []byte(mimeType)
  117. n.SetHasMime()
  118. }
  119. if isGzipped {
  120. n.SetGzipped()
  121. }
  122. if n.LastModified == 0 {
  123. n.LastModified = uint64(time.Now().Unix())
  124. }
  125. n.SetHasLastModifiedDate()
  126. if n.Ttl != EMPTY_TTL {
  127. n.SetHasTtl()
  128. }
  129. if fixJpgOrientation {
  130. loweredName := strings.ToLower(fname)
  131. if mimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
  132. n.Data = images.FixJpgOrientation(n.Data)
  133. }
  134. }
  135. n.Checksum = NewCRC(n.Data)
  136. commaSep := strings.LastIndex(r.URL.Path, ",")
  137. dotSep := strings.LastIndex(r.URL.Path, ".")
  138. fid := r.URL.Path[commaSep+1:]
  139. if dotSep > 0 {
  140. fid = r.URL.Path[commaSep+1 : dotSep]
  141. }
  142. e = n.ParsePath(fid)
  143. return
  144. }
  145. func (n *Needle) ParsePath(fid string) (err error) {
  146. length := len(fid)
  147. if length <= 8 {
  148. return errors.New("Invalid fid:" + fid)
  149. }
  150. delta := ""
  151. deltaIndex := strings.LastIndex(fid, "_")
  152. if deltaIndex > 0 {
  153. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  154. }
  155. n.Id, n.Cookie, err = ParseKeyHash(fid)
  156. if err != nil {
  157. return err
  158. }
  159. if delta != "" {
  160. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  161. n.Id += d
  162. } else {
  163. return e
  164. }
  165. }
  166. return err
  167. }
  168. func ParseKeyHash(key_hash_string string) (uint64, uint32, error) {
  169. key_hash_bytes, khe := hex.DecodeString(key_hash_string)
  170. key_hash_len := len(key_hash_bytes)
  171. if khe != nil || key_hash_len <= 4 {
  172. glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
  173. return 0, 0, errors.New("Invalid key and hash:" + key_hash_string)
  174. }
  175. key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
  176. hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
  177. return key, hash, nil
  178. }