Skip to content

Commit bf3ba27

Browse files
committed
add ContentType support
1 parent a0c2c4b commit bf3ba27

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

files.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,21 @@ import (
55
"fmt"
66
"io"
77
"mime/multipart"
8+
"net/textproto"
89
"strings"
910
)
1011

12+
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
13+
14+
func escapeQuotes(s string) string {
15+
return quoteEscaper.Replace(s)
16+
}
17+
1118
type DiscordFile struct {
1219
*bytes.Buffer
1320
Filename string
1421
Description string
22+
ContentType string
1523
Spoiler bool
1624
}
1725

@@ -40,8 +48,18 @@ func (f *DiscordFile) GenerateAttachment(index Snowflake, m *multipart.Writer) (
4048
Filename: f.Filename,
4149
Description: f.Description,
4250
}
51+
contentType := "application/octet-stream"
52+
if f.ContentType != "" {
53+
contentType = f.ContentType
54+
}
55+
56+
h := make(textproto.MIMEHeader)
57+
h.Set("Content-Disposition",
58+
fmt.Sprintf(`form-data; name="%s"; filename="%s"`,
59+
escapeQuotes(fmt.Sprintf(formFieldNameFmt, index)), escapeQuotes(f.Filename)))
60+
h.Set("Content-Type", contentType)
4361

44-
w, err := m.CreateFormFile(fmt.Sprintf(formFieldNameFmt, index), f.Filename)
62+
w, err := m.CreatePart(h)
4563
if err != nil {
4664
return nil, err
4765
}

0 commit comments

Comments
 (0)