-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment.go
59 lines (46 loc) · 1.1 KB
/
attachment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package httpcord
import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/textproto"
"strings"
)
var QuoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
type DiscordFile struct {
*bytes.Buffer
Filename string
Description string
ContentType string
Spoiler bool
}
func (f *DiscordFile) MakeAttach(ID Snowflake, m *multipart.Writer) (*Attachment, error) {
if f.Spoiler && !strings.HasPrefix(f.Filename, "SPOILER_") {
f.Filename = "SPOILER_" + f.Filename
}
attach := &Attachment{
ID: ID,
Filename: f.Filename,
Description: f.Description,
}
contentType := "application/octet-stream"
if f.ContentType != "" {
contentType = f.ContentType
}
headers := make(textproto.MIMEHeader)
headers.Set("Content-Disposition", fmt.Sprintf("form-data; name=\"%s\"; filename=\"%s\"",
QuoteEscaper.Replace(fmt.Sprintf("files[%d]", ID.Uint64())),
QuoteEscaper.Replace(f.Filename),
))
headers.Set("Content-Type", contentType)
w, err := m.CreatePart(headers)
if err != nil {
return nil, err
}
_, err = io.Copy(w, f)
if err != nil {
return nil, err
}
return attach, nil
}