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.

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