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.

103 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, 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. isChunkedFile, _ = strconv.ParseBool(r.FormValue("cm"))
  58. if !isChunkedFile {
  59. dotIndex := strings.LastIndex(fileName, ".")
  60. ext, mtype := "", ""
  61. if dotIndex > 0 {
  62. ext = strings.ToLower(fileName[dotIndex:])
  63. mtype = mime.TypeByExtension(ext)
  64. }
  65. contentType := part.Header.Get("Content-Type")
  66. if contentType != "" && mtype != contentType {
  67. mimeType = contentType //only return mime type if not deductable
  68. mtype = contentType
  69. }
  70. if part.Header.Get("Content-Encoding") == "gzip" {
  71. isGzipped = true
  72. } else if operation.IsGzippable(ext, mtype) {
  73. if data, e = operation.GzipData(data); e != nil {
  74. return
  75. }
  76. isGzipped = true
  77. }
  78. if ext == ".gz" {
  79. if strings.HasSuffix(fileName, ".css.gz") ||
  80. strings.HasSuffix(fileName, ".html.gz") ||
  81. strings.HasSuffix(fileName, ".txt.gz") ||
  82. strings.HasSuffix(fileName, ".js.gz") {
  83. fileName = fileName[:len(fileName)-3]
  84. isGzipped = true
  85. }
  86. }
  87. }
  88. return
  89. }