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.

139 lines
4.0 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) (fileId, urlLocation string, err error) {
  66. /*
  67. Amazon S3 ref link:[http://docs.aws.amazon.com/AmazonS3/latest/API/Welcome.html]
  68. There is a long way to provide a completely compatibility against all Amazon S3 API, I just made
  69. a simple data stream adapter between S3 PUT API and seaweedfs's volume storage Write API
  70. 1. The request url format should be http://$host:$port/$bucketName/$objectName
  71. 2. bucketName will be mapped to seaweedfs's collection name
  72. 3. You could customize and make your enhancement.
  73. */
  74. lastPos := strings.LastIndex(r.URL.Path, "/")
  75. if lastPos == -1 || lastPos == 0 || lastPos == len(r.URL.Path)-1 {
  76. glog.V(0).Infof("URL Path [%s] is invalid, could not retrieve file name", r.URL.Path)
  77. err = fmt.Errorf("URL Path is invalid")
  78. writeJsonError(w, r, http.StatusInternalServerError, err)
  79. return
  80. }
  81. if err = checkContentMD5(w, r); err != nil {
  82. return
  83. }
  84. fileName := r.URL.Path[lastPos+1:]
  85. if err = multipartHttpBodyBuilder(w, r, fileName); err != nil {
  86. return
  87. }
  88. secondPos := strings.Index(r.URL.Path[1:], "/") + 1
  89. collection = r.URL.Path[1:secondPos]
  90. path := r.URL.Path
  91. if fileId, urlLocation, err = fs.queryFileInfoByPath(w, r, path); err == nil && fileId == "" {
  92. fileId, urlLocation, err = fs.assignNewFileInfo(w, r, replication, collection)
  93. }
  94. return
  95. }
  96. func multipartHttpBodyBuilder(w http.ResponseWriter, r *http.Request, fileName string) (err error) {
  97. body, contentType, te := makeFormData(fileName, r.Header.Get("Content-Type"), r.Body)
  98. if te != nil {
  99. glog.V(0).Infoln("S3 protocol to raw seaweed protocol failed", te.Error())
  100. writeJsonError(w, r, http.StatusInternalServerError, te)
  101. err = te
  102. return
  103. }
  104. if body != nil {
  105. switch v := body.(type) {
  106. case *bytes.Buffer:
  107. r.ContentLength = int64(v.Len())
  108. case *bytes.Reader:
  109. r.ContentLength = int64(v.Len())
  110. case *strings.Reader:
  111. r.ContentLength = int64(v.Len())
  112. }
  113. }
  114. r.Header.Set("Content-Type", contentType)
  115. rc, ok := body.(io.ReadCloser)
  116. if !ok && body != nil {
  117. rc = ioutil.NopCloser(body)
  118. }
  119. r.Body = rc
  120. return
  121. }