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.

109 lines
2.3 KiB

6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
  1. package needle
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "io"
  6. "io/ioutil"
  7. "mime"
  8. "net/http"
  9. "path"
  10. "strconv"
  11. "strings"
  12. )
  13. func parseMultipart(r *http.Request) (
  14. fileName string, data []byte, mimeType string, isGzipped bool, originalDataSize int, isChunkedFile bool, e error) {
  15. defer func() {
  16. if e != nil && r.Body != nil {
  17. io.Copy(ioutil.Discard, r.Body)
  18. r.Body.Close()
  19. }
  20. }()
  21. form, fe := r.MultipartReader()
  22. if fe != nil {
  23. glog.V(0).Infoln("MultipartReader [ERROR]", fe)
  24. e = fe
  25. return
  26. }
  27. //first multi-part item
  28. part, fe := form.NextPart()
  29. if fe != nil {
  30. glog.V(0).Infoln("Reading Multi part [ERROR]", fe)
  31. e = fe
  32. return
  33. }
  34. fileName = part.FileName()
  35. if fileName != "" {
  36. fileName = path.Base(fileName)
  37. }
  38. data, e = ioutil.ReadAll(part)
  39. if e != nil {
  40. glog.V(0).Infoln("Reading Content [ERROR]", e)
  41. return
  42. }
  43. //if the filename is empty string, do a search on the other multi-part items
  44. for fileName == "" {
  45. part2, fe := form.NextPart()
  46. if fe != nil {
  47. break // no more or on error, just safely break
  48. }
  49. fName := part2.FileName()
  50. //found the first <file type> multi-part has filename
  51. if fName != "" {
  52. data2, fe2 := ioutil.ReadAll(part2)
  53. if fe2 != nil {
  54. glog.V(0).Infoln("Reading Content [ERROR]", fe2)
  55. e = fe2
  56. return
  57. }
  58. //update
  59. data = data2
  60. fileName = path.Base(fName)
  61. break
  62. }
  63. }
  64. originalDataSize = len(data)
  65. isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  66. if !isChunkedFile {
  67. dotIndex := strings.LastIndex(fileName, ".")
  68. ext, mtype := "", ""
  69. if dotIndex > 0 {
  70. ext = strings.ToLower(fileName[dotIndex:])
  71. mtype = mime.TypeByExtension(ext)
  72. }
  73. contentType := part.Header.Get("Content-Type")
  74. if contentType != "" && mtype != contentType {
  75. mimeType = contentType //only return mime type if not deductable
  76. mtype = contentType
  77. }
  78. if part.Header.Get("Content-Encoding") == "gzip" {
  79. if unzipped, e := util.UnGzipData(data); e == nil {
  80. originalDataSize = len(unzipped)
  81. }
  82. isGzipped = true
  83. } else if util.IsGzippable(ext, mtype, data) {
  84. if compressedData, err := util.GzipData(data); err == nil {
  85. if len(data) > len(compressedData) {
  86. data = compressedData
  87. isGzipped = true
  88. }
  89. }
  90. }
  91. }
  92. return
  93. }