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.

118 lines
3.1 KiB

10 years ago
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:"error,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. func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool, mtype string, pairMap map[string]string, jwt security.EncodedJwt) (*UploadResult, error) {
  34. return upload_content(uploadUrl, func(w io.Writer) (err error) {
  35. _, err = io.Copy(w, reader)
  36. return
  37. }, filename, isGzipped, mtype, pairMap, jwt)
  38. }
  39. 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) {
  40. body_buf := bytes.NewBufferString("")
  41. body_writer := multipart.NewWriter(body_buf)
  42. h := make(textproto.MIMEHeader)
  43. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename)))
  44. if mtype == "" {
  45. mtype = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
  46. }
  47. if mtype != "" {
  48. h.Set("Content-Type", mtype)
  49. }
  50. if isGzipped {
  51. h.Set("Content-Encoding", "gzip")
  52. }
  53. if jwt != "" {
  54. h.Set("Authorization", "BEARER "+string(jwt))
  55. }
  56. file_writer, cp_err := body_writer.CreatePart(h)
  57. if cp_err != nil {
  58. glog.V(0).Infoln("error creating form file", cp_err.Error())
  59. return nil, cp_err
  60. }
  61. if err := fillBufferFunction(file_writer); err != nil {
  62. glog.V(0).Infoln("error copying data", err)
  63. return nil, err
  64. }
  65. content_type := body_writer.FormDataContentType()
  66. if err := body_writer.Close(); err != nil {
  67. glog.V(0).Infoln("error closing body", err)
  68. return nil, err
  69. }
  70. req, postErr := http.NewRequest("POST", uploadUrl, body_buf)
  71. if postErr != nil {
  72. glog.V(0).Infoln("failing to upload to", uploadUrl, postErr.Error())
  73. return nil, postErr
  74. }
  75. req.Header.Set("Content-Type", content_type)
  76. for k, v := range pairMap {
  77. req.Header.Set(k, v)
  78. }
  79. resp, post_err := client.Do(req)
  80. if post_err != nil {
  81. glog.V(0).Infoln("failing to upload to", uploadUrl, post_err.Error())
  82. return nil, post_err
  83. }
  84. defer resp.Body.Close()
  85. etag := getEtag(resp)
  86. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  87. if ra_err != nil {
  88. return nil, ra_err
  89. }
  90. var ret UploadResult
  91. unmarshal_err := json.Unmarshal(resp_body, &ret)
  92. if unmarshal_err != nil {
  93. glog.V(0).Infoln("failing to read upload response", uploadUrl, string(resp_body))
  94. return nil, unmarshal_err
  95. }
  96. if ret.Error != "" {
  97. return nil, errors.New(ret.Error)
  98. }
  99. ret.ETag = etag
  100. return &ret, nil
  101. }
  102. func getEtag(r *http.Response) (etag string) {
  103. etag = r.Header.Get("ETag")
  104. if strings.HasPrefix(etag, "\"") && strings.HasSuffix(etag, "\"") {
  105. etag = etag[1 : len(etag)-1]
  106. }
  107. return
  108. }