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.

260 lines
6.5 KiB

12 years ago
12 years ago
12 years ago
9 years ago
9 years ago
9 years ago
12 years ago
  1. package storage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "mime"
  7. "net/http"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/images"
  14. "github.com/chrislusf/seaweedfs/weed/operation"
  15. . "github.com/chrislusf/seaweedfs/weed/storage/types"
  16. )
  17. const (
  18. NeedleChecksumSize = 4
  19. PairNamePrefix = "Seaweed-"
  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 Cookie `comment:"random number to mitigate brute force lookups"`
  27. Id NeedleId `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. PairsSize uint16 //version2
  37. Pairs []byte `comment:"additional name value pairs, json format, maximum 64kB"`
  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) (
  48. fileName string, data []byte, mimeType string, pairMap map[string]string, isGzipped bool,
  49. modifiedTime uint64, ttl *TTL, isChunkedFile bool, e error) {
  50. pairMap = make(map[string]string)
  51. for k, v := range r.Header {
  52. if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) {
  53. pairMap[k] = v[0]
  54. }
  55. }
  56. form, fe := r.MultipartReader()
  57. if fe != nil {
  58. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  59. e = fe
  60. return
  61. }
  62. //first multi-part item
  63. part, fe := form.NextPart()
  64. if fe != nil {
  65. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  66. e = fe
  67. return
  68. }
  69. fileName = part.FileName()
  70. if fileName != "" {
  71. fileName = path.Base(fileName)
  72. }
  73. data, e = ioutil.ReadAll(part)
  74. if e != nil {
  75. glog.V(0).Infoln("Reading Content [ERROR]", e)
  76. return
  77. }
  78. //if the filename is empty string, do a search on the other multi-part items
  79. for fileName == "" {
  80. part2, fe := form.NextPart()
  81. if fe != nil {
  82. break // no more or on error, just safely break
  83. }
  84. fName := part2.FileName()
  85. //found the first <file type> multi-part has filename
  86. if fName != "" {
  87. data2, fe2 := ioutil.ReadAll(part2)
  88. if fe2 != nil {
  89. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  90. e = fe2
  91. return
  92. }
  93. //update
  94. data = data2
  95. fileName = path.Base(fName)
  96. break
  97. }
  98. }
  99. isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  100. if !isChunkedFile {
  101. dotIndex := strings.LastIndex(fileName, ".")
  102. ext, mtype := "", ""
  103. if dotIndex > 0 {
  104. ext = strings.ToLower(fileName[dotIndex:])
  105. mtype = mime.TypeByExtension(ext)
  106. }
  107. contentType := part.Header.Get("Content-Type")
  108. if contentType != "" && mtype != contentType {
  109. mimeType = contentType //only return mime type if not deductable
  110. mtype = contentType
  111. }
  112. if part.Header.Get("Content-Encoding") == "gzip" {
  113. isGzipped = true
  114. } else if operation.IsGzippable(ext, mtype) {
  115. if data, e = operation.GzipData(data); e != nil {
  116. return
  117. }
  118. isGzipped = true
  119. }
  120. if ext == ".gz" {
  121. if strings.HasSuffix(fileName, ".css.gz") ||
  122. strings.HasSuffix(fileName, ".html.gz") ||
  123. strings.HasSuffix(fileName, ".txt.gz") ||
  124. strings.HasSuffix(fileName, ".js.gz") {
  125. fileName = fileName[:len(fileName)-3]
  126. isGzipped = true
  127. }
  128. }
  129. }
  130. modifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  131. ttl, _ = ReadTTL(r.FormValue("ttl"))
  132. return
  133. }
  134. func NewNeedle(r *http.Request, fixJpgOrientation bool) (n *Needle, e error) {
  135. var pairMap map[string]string
  136. fname, mimeType, isGzipped, isChunkedFile := "", "", false, false
  137. n = new(Needle)
  138. fname, n.Data, mimeType, pairMap, isGzipped, n.LastModified, n.Ttl, isChunkedFile, e = ParseUpload(r)
  139. if e != nil {
  140. return
  141. }
  142. if len(fname) < 256 {
  143. n.Name = []byte(fname)
  144. n.SetHasName()
  145. }
  146. if len(mimeType) < 256 {
  147. n.Mime = []byte(mimeType)
  148. n.SetHasMime()
  149. }
  150. if len(pairMap) != 0 {
  151. trimmedPairMap := make(map[string]string)
  152. for k, v := range pairMap {
  153. trimmedPairMap[k[len(PairNamePrefix):]] = v
  154. }
  155. pairs, _ := json.Marshal(trimmedPairMap)
  156. if len(pairs) < 65536 {
  157. n.Pairs = pairs
  158. n.PairsSize = uint16(len(pairs))
  159. n.SetHasPairs()
  160. }
  161. }
  162. if isGzipped {
  163. n.SetGzipped()
  164. }
  165. if n.LastModified == 0 {
  166. n.LastModified = uint64(time.Now().Unix())
  167. }
  168. n.SetHasLastModifiedDate()
  169. if n.Ttl != EMPTY_TTL {
  170. n.SetHasTtl()
  171. }
  172. if isChunkedFile {
  173. n.SetIsChunkManifest()
  174. }
  175. if fixJpgOrientation {
  176. loweredName := strings.ToLower(fname)
  177. if mimeType == "image/jpeg" || strings.HasSuffix(loweredName, ".jpg") || strings.HasSuffix(loweredName, ".jpeg") {
  178. n.Data = images.FixJpgOrientation(n.Data)
  179. }
  180. }
  181. n.Checksum = NewCRC(n.Data)
  182. commaSep := strings.LastIndex(r.URL.Path, ",")
  183. dotSep := strings.LastIndex(r.URL.Path, ".")
  184. fid := r.URL.Path[commaSep+1:]
  185. if dotSep > 0 {
  186. fid = r.URL.Path[commaSep+1: dotSep]
  187. }
  188. e = n.ParsePath(fid)
  189. return
  190. }
  191. func (n *Needle) ParsePath(fid string) (err error) {
  192. length := len(fid)
  193. if length <= CookieSize*2 {
  194. return fmt.Errorf("Invalid fid: %s", fid)
  195. }
  196. delta := ""
  197. deltaIndex := strings.LastIndex(fid, "_")
  198. if deltaIndex > 0 {
  199. fid, delta = fid[0:deltaIndex], fid[deltaIndex+1:]
  200. }
  201. n.Id, n.Cookie, err = ParseNeedleIdCookie(fid)
  202. if err != nil {
  203. return err
  204. }
  205. if delta != "" {
  206. if d, e := strconv.ParseUint(delta, 10, 64); e == nil {
  207. n.Id += NeedleId(d)
  208. } else {
  209. return e
  210. }
  211. }
  212. return err
  213. }
  214. func ParseNeedleIdCookie(key_hash_string string) (NeedleId, Cookie, error) {
  215. if len(key_hash_string) <= CookieSize*2 {
  216. return 0, 0, fmt.Errorf("KeyHash is too short.")
  217. }
  218. if len(key_hash_string) > (NeedleIdSize+CookieSize)*2 {
  219. return 0, 0, fmt.Errorf("KeyHash is too long.")
  220. }
  221. split := len(key_hash_string) - CookieSize*2
  222. needleId, err := ParseNeedleId(key_hash_string[:split])
  223. if err != nil {
  224. return 0, 0, fmt.Errorf("Parse needleId error: %v", err)
  225. }
  226. cookie, err := ParseCookie(key_hash_string[split:])
  227. if err != nil {
  228. return 0, 0, fmt.Errorf("Parse cookie error: %v", err)
  229. }
  230. return needleId, cookie, nil
  231. }