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.

118 lines
2.6 KiB

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