Correct content-type for attachments (#33)
* fixed error return value * content-type correctly set
This commit is contained in:
parent
5d938afef2
commit
2a76034a05
44
response.go
44
response.go
@ -7,8 +7,11 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/textproto"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AttachmentType is attachment type.
|
// AttachmentType is attachment type.
|
||||||
@ -119,32 +122,57 @@ func (r *Response) Attachment(dataType AttachmentType, url string) error {
|
|||||||
return r.DispatchMessage(&m)
|
return r.DispatchMessage(&m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// copied from multipart package
|
||||||
|
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
|
||||||
|
|
||||||
|
// copied from multipart package
|
||||||
|
func escapeQuotes(s string) string {
|
||||||
|
return quoteEscaper.Replace(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// copied from multipart package with slight changes due to fixed content-type there
|
||||||
|
func createFormFile(filename string, w *multipart.Writer, contentType string) (io.Writer, error) {
|
||||||
|
h := make(textproto.MIMEHeader)
|
||||||
|
h.Set("Content-Disposition",
|
||||||
|
fmt.Sprintf(`form-data; name="filedata"; filename="%s"`,
|
||||||
|
escapeQuotes(filename)))
|
||||||
|
h.Set("Content-Type", contentType)
|
||||||
|
return w.CreatePart(h)
|
||||||
|
}
|
||||||
|
|
||||||
// AttachmentData sends an image, sound, video or a regular file to a chat via an io.Reader.
|
// AttachmentData sends an image, sound, video or a regular file to a chat via an io.Reader.
|
||||||
func (r *Response) AttachmentData(dataType AttachmentType, filename string, filedata io.Reader) error {
|
func (r *Response) AttachmentData(dataType AttachmentType, filename string, filedata io.Reader) error {
|
||||||
var b bytes.Buffer
|
|
||||||
w := multipart.NewWriter(&b)
|
|
||||||
|
|
||||||
data, err := w.CreateFormFile("filedata", filename)
|
filedataBytes, err := ioutil.ReadAll(filedata)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
contentType := http.DetectContentType(filedataBytes[:512])
|
||||||
|
fmt.Println("Content-type detected:", contentType)
|
||||||
|
|
||||||
|
var body bytes.Buffer
|
||||||
|
multipartWriter := multipart.NewWriter(&body)
|
||||||
|
data, err := createFormFile(filename, multipartWriter, contentType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = io.Copy(data, filedata)
|
_, err = bytes.NewBuffer(filedataBytes).WriteTo(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w.WriteField("recipient", fmt.Sprintf(`{"id":"%v"}`, r.to.ID))
|
multipartWriter.WriteField("recipient", fmt.Sprintf(`{"id":"%v"}`, r.to.ID))
|
||||||
w.WriteField("message", fmt.Sprintf(`{"attachment":{"type":"%v", "payload":{}}}`, dataType))
|
multipartWriter.WriteField("message", fmt.Sprintf(`{"attachment":{"type":"%v", "payload":{}}}`, dataType))
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", SendMessageURL, &b)
|
req, err := http.NewRequest("POST", SendMessageURL, &body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.URL.RawQuery = "access_token=" + r.token
|
req.URL.RawQuery = "access_token=" + r.token
|
||||||
|
|
||||||
req.Header.Set("Content-Type", w.FormDataContentType())
|
req.Header.Set("Content-Type", multipartWriter.FormDataContentType())
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
Loading…
Reference in New Issue
Block a user