-
Notifications
You must be signed in to change notification settings - Fork 86
/
attachment.go
75 lines (67 loc) · 1.67 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"bytes"
"image"
"image/jpeg"
"io"
"log"
"mime"
"path"
"path/filepath"
"strings"
"github.com/la5nta/wl2k-go/fbb"
"github.com/nfnt/resize"
)
func addAttachment(msg *fbb.Message, filename string, contentType string, r io.Reader) error {
p, err := io.ReadAll(r)
if err != nil {
return err
}
if ok, mediaType := isConvertableImageMediaType(filename, contentType); ok {
log.Printf("Auto converting '%s' [%s]...", filename, mediaType)
if converted, err := convertImage(p); err != nil {
log.Printf("Error converting image: %s", err)
} else {
log.Printf("Done converting '%s'.", filename)
ext := filepath.Ext(filename)
filename = filename[:len(filename)-len(ext)] + ".jpg"
p = converted
}
}
msg.AddFile(fbb.NewFile(filename, p))
return nil
}
func isConvertableImageMediaType(filename, contentType string) (convertable bool, mediaType string) {
if contentType != "" {
mediaType, _, _ = mime.ParseMediaType(contentType)
}
if mediaType == "" {
mediaType = mime.TypeByExtension(path.Ext(filename))
}
switch mediaType {
case "image/svg+xml":
// This is a text file
return false, mediaType
default:
return strings.HasPrefix(mediaType, "image/"), mediaType
}
}
func convertImage(orig []byte) ([]byte, error) {
img, _, err := image.Decode(bytes.NewReader(orig))
if err != nil {
return nil, err
}
// Scale down
if img.Bounds().Dx() > 600 {
img = resize.Resize(600, 0, img, resize.NearestNeighbor)
}
// Re-encode as low quality jpeg
var buf bytes.Buffer
if err := jpeg.Encode(&buf, img, &jpeg.Options{Quality: 40}); err != nil {
return orig, err
}
if buf.Len() >= len(orig) {
return orig, nil
}
return buf.Bytes(), nil
}