|
@ -4,12 +4,15 @@ import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
"encoding/json" |
|
|
"encoding/json" |
|
|
"errors" |
|
|
"errors" |
|
|
_ "fmt" |
|
|
|
|
|
|
|
|
"fmt" |
|
|
"io" |
|
|
"io" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"log" |
|
|
"log" |
|
|
|
|
|
"mime" |
|
|
"mime/multipart" |
|
|
"mime/multipart" |
|
|
"net/http" |
|
|
"net/http" |
|
|
|
|
|
"net/textproto" |
|
|
|
|
|
"path/filepath" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type UploadResult struct { |
|
|
type UploadResult struct { |
|
@ -20,7 +23,10 @@ type UploadResult struct { |
|
|
func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) { |
|
|
func Upload(uploadUrl string, filename string, reader io.Reader) (*UploadResult, error) { |
|
|
body_buf := bytes.NewBufferString("") |
|
|
body_buf := bytes.NewBufferString("") |
|
|
body_writer := multipart.NewWriter(body_buf) |
|
|
body_writer := multipart.NewWriter(body_buf) |
|
|
file_writer, err := body_writer.CreateFormFile("file", filename) |
|
|
|
|
|
|
|
|
h := make(textproto.MIMEHeader) |
|
|
|
|
|
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, filename)) |
|
|
|
|
|
h.Set("Content-Type", mime.TypeByExtension(filepath.Ext(filename))) |
|
|
|
|
|
file_writer, err := body_writer.CreatePart(h) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
log.Println("error creating form file", err) |
|
|
log.Println("error creating form file", err) |
|
|
return nil, err |
|
|
return nil, err |
|
|