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.

235 lines
5.8 KiB

12 years ago
12 years ago
12 years ago
12 years ago
9 years ago
9 years ago
12 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "mime"
  6. "net/http"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/images"
  13. "github.com/chrislusf/seaweedfs/weed/operation"
  14. )
  15. const (
  16. NeedleHeaderSize = 16 //should never change this
  17. NeedlePaddingSize = 8
  18. NeedleChecksumSize = 4
  19. MaxPossibleVolumeSize = 4 * 1024 * 1024 * 1024 * 8
  20. )
  21. /*
  22. * A Needle means a uploaded and stored file.
  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. Ttl *TTL
  38. Checksum CRC `comment:"CRC32 to check integrity"`
  39. Padding []byte `comment:"Aligned to 8 bytes"`
  40. rawBlock *Block // underlying supporing []byte, fetched and released into a pool
  41. }
  42. func (n *Needle) String() (str string) {
  43. 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)
  44. return
  45. }
  46. func ParseUpload(r *http.Request) (
  47. fileName string, data []byte, mimeType string, isGzipped bool,
  48. modifiedTime uint64, ttl *TTL, isChunkedFile bool, e error) {
  49. form, fe := r.MultipartReader()
  50. if fe != nil {
  51. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  52. e = fe
  53. return
  54. }
  55. //first multi-part item
  56. part, fe := form.NextPart()
  57. if fe != nil {
  58. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  59. e = fe
  60. return
  61. }
  62. fileName = part.FileName()
  63. if fileName != "" {
  64. fileName = path.Base(fileName)
  65. }
  66. data, e = ioutil.ReadAll(part)
  67. if e != nil {
  68. glog.V(0).Infoln("Reading Content [ERROR]", e)
  69. return
  70. }
  71. //if the filename is empty string, do a search on the other multi-part items
  72. for fileName == "" {
  73. part2, fe := form.NextPart()
  74. if fe != nil {
  75. break // no more or on error, just safely break
  76. }
  77. fName := part2.FileName()
  78. //found the first <file type> multi-part has filename
  79. if fName != "" {
  80. data2, fe2 := ioutil.ReadAll(part2)
  81. if fe2 != nil {
  82. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  83. e = fe2
  84. return
  85. }
  86. //update
  87. data = data2
  88. fileName = path.Base(fName)
  89. break
  90. }
  91. }
  92. isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  93. isGzipped = false
  94. if !isChunkedFile {
  95. dotIndex := strings.LastIndex(fileName, ".")
  96. ext, mtype := "", ""
  97. if dotIndex > 0 {
  98. ext = strings.ToLower(fileName[dotIndex:])
  99. mtype = mime.TypeByExtension(ext)
  100. }
  101. contentType := part.Header.Get("Content-Type")
  102. if contentType != "" && mtype != contentType {
  103. mimeType = contentType //only return mime type if not deductable
  104. mtype = contentType
  105. }
  106. if part.Header.Get("Content-Encoding") == "gzip" {
  107. isGzipped = true
  108. } else if operation.IsGzippable(ext, mtype) {
  109. if data, e = operation.GzipData(data); e != nil {
  110. return
  111. }
  112. isGzipped = true
  113. }
  114. if ext == ".gz" {
  115. isGzipped = true
  116. }
  117. if strings.HasSuffix(fileName, ".gz") &&
  118. !strings.HasSuffix(fileName, ".tar.gz") {
  119. fileName = fileName[:len(fileName)-3]
  120. }
  121. }
  122. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  123. ttl, _ = ReadTTL(r.FormValue("ttl"))
  124. return
  125. }
  126. func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) {
  127. fname, mimeType, isGzipped, isChunkedFile := "", "", false, false
  128. n = new(Needle)
  129. fname, n.Data, mimeType, isGzipped, n.LastModified, n.Ttl, isChunkedFile, e = ParseUpload(r)
  130. if e != nil {
  131. return
  132. }
  133. if len(fname) < 256 {
  134. n.Name = []byte(fname)
  135. n.SetHasName()
  136. }
  137. if len(mimeType) < 256 {
  138. n.Mime = []byte(mimeType)
  139. n.SetHasMime()
  140. }
  141. if isGzipped {
  142. n.SetGzipped()
  143. }
  144. if n.LastModified == 0 {
  145. n.LastModified = uint64(time.Now().Unix())
  146. }
  147. n.SetHasLastModifiedDate()
  148. if n.Ttl != EMPTY_TTL {
  149. n.SetHasTtl()
  150. }
  151. if isChunkedFile {
  152. n.SetIsChunkManifest()
  153. }
  154. if fixJpgOrientation {
  155. loweredName := strings.ToLower(fname)
  156. if mimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
  157. n.Data = images.FixJpgOrientation(n.Data)
  158. }
  159. }
  160. n.Checksum = NewCRC(n.Data)
  161. commaSep := strings.LastIndex(r.URL.Path, ",")
  162. dotSep := strings.LastIndex(r.URL.Path, ".")
  163. fid := r.URL.Path[commaSep+1:]
  164. if dotSep > 0 {
  165. fid = r.URL.Path[commaSep+1 : dotSep]
  166. }
  167. e = n.ParsePath(fid)
  168. return
  169. }
  170. func (n *Needle) ParsePath(fid string) (err error) {
  171. length := len(fid)
  172. if length <= 8 {
  173. return fmt.Errorf("Invalid fid: %s", fid)
  174. }
  175. delta := ""
  176. deltaIndex := strings.LastIndex(fid, "_")
  177. if deltaIndex > 0 {
  178. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  179. }
  180. n.Id, n.Cookie, err = ParseKeyHash(fid)
  181. if err != nil {
  182. return err
  183. }
  184. if delta != "" {
  185. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  186. n.Id += d
  187. } else {
  188. return e
  189. }
  190. }
  191. return err
  192. }
  193. func ParseKeyHash(key_hash_string string) (uint64, uint32, error) {
  194. if len(key_hash_string) <= 8 {
  195. return 0, 0, fmt.Errorf("KeyHash is too short.")
  196. }
  197. if len(key_hash_string) > 24 {
  198. return 0, 0, fmt.Errorf("KeyHash is too long.")
  199. }
  200. split := len(key_hash_string) - 8
  201. key, err := strconv.ParseUint(key_hash_string[:split], 16, 64)
  202. if err != nil {
  203. return 0, 0, fmt.Errorf("Parse key error: %v", err)
  204. }
  205. hash, err := strconv.ParseUint(key_hash_string[split:], 16, 32)
  206. if err != nil {
  207. return 0, 0, fmt.Errorf("Parse hash error: %v", err)
  208. }
  209. return key, uint32(hash), nil
  210. }