- commit
- 0365902
- parent
- 4702f58
- author
- Eric Bower
- date
- 2022-09-26 02:34:15 +0000 UTC
feat(imgs): only modify quality if file is large
2 files changed,
+10,
-6
+3,
-1
1@@ -20,7 +20,9 @@ import (
2 "golang.org/x/exp/slices"
3 )
4
5-var GB = 1024 * 1024 * 1024
6+var KB = 1024
7+var MB = KB * 1024
8+var GB = MB * 1024
9 var maxSize = 2 * GB
10
11 type PostMetaData struct {
+7,
-5
1@@ -19,7 +19,7 @@ func (h *UploadImgHandler) validateImg(data *PostMetaData) (bool, error) {
2 if err != nil {
3 return false, err
4 }
5- if fileSize+int(data.FileSize) > maxSize {
6+ if fileSize+data.FileSize > maxSize {
7 return false, fmt.Errorf("ERROR: user (%s) has exceeded (%d) max (%d)", data.User.Name, fileSize, maxSize)
8 }
9
10@@ -37,7 +37,6 @@ func (h *UploadImgHandler) validateImg(data *PostMetaData) (bool, error) {
11 }
12
13 func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
14- // create or get
15 bucket, err := h.Storage.UpsertBucket(data.User.ID)
16 if err != nil {
17 return err
18@@ -45,8 +44,6 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
19
20 reader := bytes.NewReader([]byte(data.Text))
21 tee := bytes.NewReader([]byte(data.Text))
22- // var buf bytes.Buffer
23- // tee := io.TeeReader(reader, &buf)
24
25 fname, err := h.Storage.PutFile(
26 bucket,
27@@ -58,16 +55,21 @@ func (h *UploadImgHandler) metaImg(data *PostMetaData) error {
28 }
29
30 opt := shared.NewImgOptimizer(h.Cfg.Logger, "")
31- // opt.Quality = 100
32+ if data.FileSize < 3*MB {
33+ opt.Quality = 100
34+ }
35+
36 contents := &bytes.Buffer{}
37 img, err := opt.GetImage(tee, data.MimeType)
38 if err != nil {
39 return err
40 }
41+
42 err = opt.EncodeWebp(contents, img)
43 if err != nil {
44 return err
45 }
46+
47 webpReader := bytes.NewReader(contents.Bytes())
48 _, err = h.Storage.PutFile(
49 bucket,