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.

127 lines
3.3 KiB

  1. package weed_server
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "encoding/base64"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "mime/multipart"
  10. "net/http"
  11. "net/textproto"
  12. "strings"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. )
  15. var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
  16. func escapeQuotes(s string) string {
  17. return quoteEscaper.Replace(s)
  18. }
  19. func createFormFile(writer *multipart.Writer, fieldname, filename, mime string) (io.Writer, error) {
  20. h := make(textproto.MIMEHeader)
  21. h.Set("Content-Disposition",
  22. fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
  23. escapeQuotes(fieldname), escapeQuotes(filename)))
  24. if len(mime) == 0 {
  25. mime = "application/octet-stream"
  26. }
  27. h.Set("Content-Type", mime)
  28. return writer.CreatePart(h)
  29. }
  30. func makeFormData(filename, mimeType string, content io.Reader) (formData io.Reader, contentType string, err error) {
  31. buf := new(bytes.Buffer)
  32. writer := multipart.NewWriter(buf)
  33. defer writer.Close()
  34. part, err := createFormFile(writer, "file", filename, mimeType)
  35. if err != nil {
  36. glog.V(0).Infoln(err)
  37. return
  38. }
  39. _, err = io.Copy(part, content)
  40. if err != nil {
  41. glog.V(0).Infoln(err)
  42. return
  43. }
  44. formData = buf
  45. contentType = writer.FormDataContentType()
  46. return
  47. }
  48. func checkContentMD5(w http.ResponseWriter, r *http.Request) (err error) {
  49. if contentMD5 := r.Header.Get("Content-MD5"); contentMD5 != "" {
  50. buf, _ := ioutil.ReadAll(r.Body)
  51. //checkMD5
  52. sum := md5.Sum(buf)
  53. fileDataMD5 := base64.StdEncoding.EncodeToString(sum[0:len(sum)])
  54. if strings.ToLower(fileDataMD5) != strings.ToLower(contentMD5) {
  55. glog.V(0).Infof("fileDataMD5 [%s] is not equal to Content-MD5 [%s]", fileDataMD5, contentMD5)
  56. err = fmt.Errorf("MD5 check failed")
  57. writeJsonError(w, r, http.StatusNotAcceptable, err)
  58. return
  59. }
  60. //reconstruct http request body for following new request to volume server
  61. r.Body = ioutil.NopCloser(bytes.NewBuffer(buf))
  62. }
  63. return
  64. }
  65. func (fs *FilerServer) monolithicUploadAnalyzer(w http.ResponseWriter, r *http.Request, replication, collection string, dataCenter string) (path string, err error) {
  66. lastPos := strings.LastIndex(r.URL.Path, "/")
  67. if lastPos == -1 || lastPos == 0 || lastPos == len(r.URL.Path)-1 {
  68. glog.V(0).Infof("URL Path [%s] is invalid, could not retrieve file name", r.URL.Path)
  69. err = fmt.Errorf("URL Path is invalid")
  70. writeJsonError(w, r, http.StatusInternalServerError, err)
  71. return
  72. }
  73. if err = checkContentMD5(w, r); err != nil {
  74. return
  75. }
  76. fileName := r.URL.Path[lastPos+1:]
  77. if err = multipartHttpBodyBuilder(w, r, fileName); err != nil {
  78. return
  79. }
  80. path = r.URL.Path
  81. return
  82. }
  83. func multipartHttpBodyBuilder(w http.ResponseWriter, r *http.Request, fileName string) (err error) {
  84. body, contentType, te := makeFormData(fileName, r.Header.Get("Content-Type"), r.Body)
  85. if te != nil {
  86. glog.V(0).Infoln("S3 protocol to raw seaweed protocol failed", te.Error())
  87. writeJsonError(w, r, http.StatusInternalServerError, te)
  88. err = te
  89. return
  90. }
  91. if body != nil {
  92. switch v := body.(type) {
  93. case *bytes.Buffer:
  94. r.ContentLength = int64(v.Len())
  95. case *bytes.Reader:
  96. r.ContentLength = int64(v.Len())
  97. case *strings.Reader:
  98. r.ContentLength = int64(v.Len())
  99. }
  100. }
  101. r.Header.Set("Content-Type", contentType)
  102. rc, ok := body.(io.ReadCloser)
  103. if !ok && body != nil {
  104. rc = ioutil.NopCloser(body)
  105. }
  106. r.Body = rc
  107. return
  108. }