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.

166 lines
3.8 KiB

  1. package needle
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "mime"
  7. "net/http"
  8. "path"
  9. "strconv"
  10. "strings"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. type ParsedUpload struct {
  15. FileName string
  16. Data []byte
  17. MimeType string
  18. PairMap map[string]string
  19. IsGzipped bool
  20. OriginalDataSize int
  21. ModifiedTime uint64
  22. Ttl *TTL
  23. IsChunkedFile bool
  24. UncompressedData []byte
  25. }
  26. func ParseUpload(r *http.Request, sizeLimit int64) (pu *ParsedUpload, e error) {
  27. pu = &ParsedUpload{}
  28. pu.PairMap = make(map[string]string)
  29. for k, v := range r.Header {
  30. if len(v) > 0 && strings.HasPrefix(k, PairNamePrefix) {
  31. pu.PairMap[k] = v[0]
  32. }
  33. }
  34. if r.Method == "POST" {
  35. e = parseMultipart(r, sizeLimit, pu)
  36. } else {
  37. e = parsePut(r, sizeLimit, pu)
  38. }
  39. if e != nil {
  40. return
  41. }
  42. pu.ModifiedTime, _ = strconv.ParseUint(r.FormValue("ts"), 10, 64)
  43. pu.Ttl, _ = ReadTTL(r.FormValue("ttl"))
  44. pu.OriginalDataSize = len(pu.Data)
  45. pu.UncompressedData = pu.Data
  46. if pu.IsGzipped {
  47. if unzipped, e := util.UnGzipData(pu.Data); e == nil {
  48. pu.OriginalDataSize = len(unzipped)
  49. pu.UncompressedData = unzipped
  50. }
  51. } else if shouldGzip, _ := util.IsGzippableFileType("", pu.MimeType); pu.MimeType == "" || shouldGzip {
  52. if compressedData, err := util.GzipData(pu.Data); err == nil {
  53. pu.Data = compressedData
  54. pu.IsGzipped = true
  55. }
  56. }
  57. return
  58. }
  59. func parsePut(r *http.Request, sizeLimit int64, pu *ParsedUpload) (e error) {
  60. pu.IsGzipped = r.Header.Get("Content-Encoding") == "gzip"
  61. pu.MimeType = r.Header.Get("Content-Type")
  62. pu.FileName = ""
  63. pu.Data, e = ioutil.ReadAll(io.LimitReader(r.Body, sizeLimit+1))
  64. if e == io.EOF || int64(pu.OriginalDataSize) == sizeLimit+1 {
  65. io.Copy(ioutil.Discard, r.Body)
  66. }
  67. r.Body.Close()
  68. return nil
  69. }
  70. func parseMultipart(r *http.Request, sizeLimit int64, pu *ParsedUpload) (e error) {
  71. defer func() {
  72. if e != nil && r.Body != nil {
  73. io.Copy(ioutil.Discard, r.Body)
  74. r.Body.Close()
  75. }
  76. }()
  77. form, fe := r.MultipartReader()
  78. if fe != nil {
  79. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  80. e = fe
  81. return
  82. }
  83. //first multi-part item
  84. part, fe := form.NextPart()
  85. if fe != nil {
  86. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  87. e = fe
  88. return
  89. }
  90. pu.FileName = part.FileName()
  91. if pu.FileName != "" {
  92. pu.FileName = path.Base(pu.FileName)
  93. }
  94. pu.Data, e = ioutil.ReadAll(io.LimitReader(part, sizeLimit+1))
  95. if e != nil {
  96. glog.V(0).Infoln("Reading Content [ERROR]", e)
  97. return
  98. }
  99. if len(pu.Data) == int(sizeLimit)+1 {
  100. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  101. return
  102. }
  103. //if the filename is empty string, do a search on the other multi-part items
  104. for pu.FileName == "" {
  105. part2, fe := form.NextPart()
  106. if fe != nil {
  107. break // no more or on error, just safely break
  108. }
  109. fName := part2.FileName()
  110. //found the first <file type> multi-part has filename
  111. if fName != "" {
  112. data2, fe2 := ioutil.ReadAll(io.LimitReader(part2, sizeLimit+1))
  113. if fe2 != nil {
  114. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  115. e = fe2
  116. return
  117. }
  118. if len(data2) == int(sizeLimit)+1 {
  119. e = fmt.Errorf("file over the limited %d bytes", sizeLimit)
  120. return
  121. }
  122. //update
  123. pu.Data = data2
  124. pu.FileName = path.Base(fName)
  125. break
  126. }
  127. }
  128. pu.IsChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  129. if !pu.IsChunkedFile {
  130. dotIndex := strings.LastIndex(pu.FileName, ".")
  131. ext, mtype := "", ""
  132. if dotIndex > 0 {
  133. ext = strings.ToLower(pu.FileName[dotIndex:])
  134. mtype = mime.TypeByExtension(ext)
  135. }
  136. contentType := part.Header.Get("Content-Type")
  137. if contentType != "" && contentType != "application/octet-stream" && mtype != contentType {
  138. pu.MimeType = contentType //only return mime type if not deductable
  139. mtype = contentType
  140. }
  141. pu.IsGzipped = part.Header.Get("Content-Encoding") == "gzip"
  142. }
  143. return
  144. }