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.

91 lines
2.4 KiB

12 years ago
  1. package operation
  2. import (
  3. "bytes"
  4. "github.com/chrislusf/weed-fs/go/glog"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "io"
  9. "io/ioutil"
  10. "mime"
  11. "mime/multipart"
  12. "net/http"
  13. "net/textproto"
  14. "path/filepath"
  15. "strings"
  16. )
  17. type UploadResult struct {
  18. Name string `json:"name,omitempty"`
  19. Size uint32 `json:"size,omitempty"`
  20. Error string `json:"error,omitempty"`
  21. }
  22. var (
  23. client *http.Client
  24. )
  25. func init() {
  26. client = &http.Client{Transport: &http.Transport{
  27. MaxIdleConnsPerHost: 1024,
  28. }}
  29. }
  30. var fileNameEscaper = strings.NewReplacer("\\", "\\\\", "\"", "\\\"")
  31. func Upload(uploadUrl string, filename string, reader io.Reader, isGzipped bool, mtype string) (*UploadResult, error) {
  32. return upload_content(uploadUrl, func(w io.Writer) (err error) {
  33. _, err = io.Copy(w, reader)
  34. return
  35. }, filename, isGzipped, mtype)
  36. }
  37. func upload_content(uploadUrl string, fillBufferFunction func(w io.Writer) error, filename string, isGzipped bool, mtype string) (*UploadResult, error) {
  38. body_buf := bytes.NewBufferString("")
  39. body_writer := multipart.NewWriter(body_buf)
  40. h := make(textproto.MIMEHeader)
  41. h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileNameEscaper.Replace(filename)))
  42. if mtype == "" {
  43. mtype = mime.TypeByExtension(strings.ToLower(filepath.Ext(filename)))
  44. }
  45. if mtype != "" {
  46. h.Set("Content-Type", mtype)
  47. }
  48. if isGzipped {
  49. h.Set("Content-Encoding", "gzip")
  50. }
  51. file_writer, cp_err := body_writer.CreatePart(h)
  52. if cp_err != nil {
  53. glog.V(0).Infoln("error creating form file", cp_err.Error())
  54. return nil, cp_err
  55. }
  56. if err := fillBufferFunction(file_writer); err != nil {
  57. glog.V(0).Infoln("error copying data", err)
  58. return nil, err
  59. }
  60. content_type := body_writer.FormDataContentType()
  61. if err := body_writer.Close(); err != nil {
  62. glog.V(0).Infoln("error closing body", err)
  63. return nil, err
  64. }
  65. resp, post_err := client.Post(uploadUrl, content_type, body_buf)
  66. if post_err != nil {
  67. glog.V(0).Infoln("failing to upload to", uploadUrl, post_err.Error())
  68. return nil, post_err
  69. }
  70. defer resp.Body.Close()
  71. resp_body, ra_err := ioutil.ReadAll(resp.Body)
  72. if ra_err != nil {
  73. return nil, ra_err
  74. }
  75. var ret UploadResult
  76. unmarshal_err := json.Unmarshal(resp_body, &ret)
  77. if unmarshal_err != nil {
  78. glog.V(0).Infoln("failing to read upload resonse", uploadUrl, string(resp_body))
  79. return nil, unmarshal_err
  80. }
  81. if ret.Error != "" {
  82. return nil, errors.New(ret.Error)
  83. }
  84. return &ret, nil
  85. }