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.

101 lines
2.2 KiB

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