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.

182 lines
4.5 KiB

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