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.

119 lines
3.2 KiB

12 years ago
  1. package operation
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "mime"
  10. "mime/multipart"
  11. "net/http"
  12. "net/textproto"
  13. "path/filepath"
  14. "strings"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/security"
  17. )
  18. type UploadResult struct {
  19. Name string `json:"name,omitempty"`
  20. Size uint32 `json:"size,omitempty"`
  21. Error string `json:"error,omitempty"`
  22. ETag string `json:"eTag,omitempty"`
  23. }
  24. var (
  25. client *http.Client
  26. )
  27. func init() {
  28. client = &http.Client{Transport: &http.Transport{
  29. MaxIdleConnsPerHost: 1024,
  30. }}
  31. }
  32. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  33. // Upload sends a POST request to a volume server to upload the content
  34. func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
  35. return upload_content(uploadUrl, func(w io.Writer) (err error) {
  36. _, err = io.Copy(w, reader)
  37. return
  38. }, filename, isGzipped, mtype, pairMap, jwt)
  39. }
  40. func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error, filename string, isGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
  41. body_buf := bytes.NewBufferString("")
  42. body_writer := multipart.NewWriter(body_buf)
  43. h := make(textproto.MIMEHeader)
  44. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename)))
  45. if mtype == "" {
  46. mtype = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
  47. }
  48. if mtype != "" {
  49. h.Set("Content-Type", mtype)
  50. }
  51. if isGzipped {
  52. h.Set("Content-Encoding", "gzip")
  53. }
  54. file_writer, cp_err := body_writer.CreatePart(h)
  55. if cp_err != nil {
  56. glog.V(0).Infoln("error creating form file", cp_err.Error())
  57. return nil, cp_err
  58. }
  59. if err := fillBufferFunction(file_writer); err != nil {
  60. glog.V(0).Infoln("error copying data", err)
  61. return nil, err
  62. }
  63. content_type := body_writer.FormDataContentType()
  64. if err := body_writer.Close(); err != nil {
  65. glog.V(0).Infoln("error closing body", err)
  66. return nil, err
  67. }
  68. req, postErr := http.NewRequest("POST", uploadUrl, body_buf)
  69. if postErr != nil {
  70. glog.V(0).Infoln("failing to upload to", uploadUrl, postErr.Error())
  71. return nil, postErr
  72. }
  73. req.Header.Set("Content-Type", content_type)
  74. for k, v := range pairMap {
  75. req.Header.Set(k, v)
  76. }
  77. if jwt != "" {
  78. req.Header.Set("Authorization", "BEARER "+string(jwt))
  79. }
  80. resp, post_err := client.Do(req)
  81. if post_err != nil {
  82. glog.V(0).Infoln("failing to upload to", uploadUrl, post_err.Error())
  83. return nil, post_err
  84. }
  85. defer resp.Body.Close()
  86. etag := getEtag(resp)
  87. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  88. if ra_err != nil {
  89. return nil, ra_err
  90. }
  91. var ret UploadResult
  92. unmarshal_err := json.Unmarshal(resp_body, &ret)
  93. if unmarshal_err != nil {
  94. glog.V(0).Infoln("failing to read upload response", uploadUrl, string(resp_body))
  95. return nil, unmarshal_err
  96. }
  97. if ret.Error != "" {
  98. return nil, errors.New(ret.Error)
  99. }
  100. ret.ETag = etag
  101. return &ret, nil
  102. }
  103. func getEtag(r *http.Response) (etag string) {
  104. etag = r.Header.Get("ETag")
  105. if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\"") {
  106. etag = etag[1 : len(etag)-1]
  107. }
  108. return
  109. }