| 1 | package attachment |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | func DataURL(mime string, raw []byte) string { |
| 10 | if mime == "" { |
| 11 | mime = "application/octet-stream" |
| 12 | } |
| 13 | return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(raw) |
| 14 | } |
| 15 | |
| 16 | func ParseDataURL(value string) (mime string, raw []byte, err error) { |
| 17 | raw, err = decodeDataURL(value, MaxSourceBytes) |
| 18 | if err != nil { |
| 19 | return "", nil, err |
| 20 | } |
| 21 | const prefix = "data:" |
| 22 | const marker = ";base64," |
| 23 | i := len(prefix) |
| 24 | j := strings.Index(value, marker) |
| 25 | if j <= i { |
| 26 | return "", nil, Error{Code: CodeUnsupported, Message: defaultDetail(CodeUnsupported)} |
| 27 | } |
| 28 | return normalizeDeclaredMIME(value[i:j]), raw, nil |
| 29 | } |
| 30 | |
| 31 | func FormatImageNote(ref AttachmentRef) string { |
| 32 | name := ref.DisplayName |
| 33 | if name == "" { |
| 34 | name = "image" |
| 35 | } |
| 36 | return fmt.Sprintf("[image attachment %s %dx%d %s]", name, ref.Width, ref.Height, ref.MIME()) |
| 37 | } |
| 38 |