-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset.go
169 lines (144 loc) · 3.86 KB
/
asset.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package handlers
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/alexferl/air/asset"
"github.com/alexferl/air/util"
)
const (
maxWidth = 5000
maxHeight = 5000
maxQuality = 100
)
func (h *Handler) Asset(c echo.Context) error {
id := c.Param("id")
format := c.QueryParam("format")
crop := c.QueryParam("crop")
if len(crop) > 0 {
if _, ok := asset.StringToInterestingTypes[crop]; !ok {
m := fmt.Sprintf("Unknown crop algorithm '%s'", crop)
return c.JSON(http.StatusBadRequest, ErrorResponse{m})
}
}
width, height, quality, err := parseParams(c.QueryParams())
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{err.Error()})
}
path, err := util.GetFullPathFromSha256(id)
if err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{"Invalid id"})
}
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("file-upload-timeout"))
defer cancel()
f, err := h.Storage.Get(ctx, path)
if err != nil {
return c.JSON(http.StatusNotFound, ErrorResponse{"File not found"})
}
defer f.Close()
a, err := asset.New(f)
if err != nil {
log.Error().Msgf("Failed to create asset: %v", err)
return c.JSON(http.StatusInternalServerError, ErrorResponse{"Error sending file"})
}
defer util.CleanupTempFile(a.File)
var reader io.Reader
if a.Type == "image" {
out, err := util.CreateTempFile()
if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{"Error creating temporary file"})
}
defer util.CleanupTempFile(out)
rp := asset.NewResizeParams()
rp.Width = width
rp.Height = height
rp.Quality = quality
if len(format) == 0 {
format = strings.Split(a.Ext, ".")[1]
}
if val, ok := asset.ImageTypes[format]; ok {
rp.ImageType = val
} else {
m := fmt.Sprintf("Unknown image format '%s'", format)
return c.JSON(http.StatusBadRequest, ErrorResponse{m})
}
b, err := a.Resize(rp)
if err != nil {
return c.JSON(http.StatusInternalServerError, ErrorResponse{"Error resizing image"})
}
reader = bytes.NewReader(b)
}
c.Response().Header().Set("Cache-Control", "public, max-age=604800")
return c.Stream(http.StatusOK, a.ContentType, reader)
}
func parseParams(params url.Values) (int, int, int, error) {
w := params.Get("width")
h := params.Get("height")
s := params.Get("size")
q := params.Get("quality")
var width, height, quality int
var err error
if w != "" {
width, err = strconv.Atoi(w)
if err != nil {
return 0, 0, 0, errors.New("width must be a number")
}
}
if h != "" {
height, err = strconv.Atoi(h)
if err != nil {
return 0, 0, 0, errors.New("height must be a number")
}
}
if q != "" {
quality, err = strconv.Atoi(q)
if err != nil {
return 0, 0, 0, errors.New("quality must be a number")
}
}
if len(s) > 0 {
r := regexp.MustCompile(`(\d+)[x](\d+)$`)
size := r.FindStringSubmatch(s)
if len(size) == 3 {
width, _ = strconv.Atoi(size[1])
height, _ = strconv.Atoi(size[2])
} else {
r := regexp.MustCompile(`(\d+)$`)
m := r.FindString(s)
if m == "" {
return 0, 0, 0, errors.New("incorrect format for size")
} else {
width, _ = strconv.Atoi(s)
}
}
}
if width < 0 {
return 0, 0, 0, errors.New("width cannot be less than 0")
}
if height < 0 {
return 0, 0, 0, errors.New("height cannot be less than 0")
}
if quality < 0 {
return 0, 0, 0, errors.New("quality cannot be less than 0")
}
if width > maxWidth {
return 0, 0, 0, errors.New(fmt.Sprintf("width cannot be above %d", maxWidth))
}
if height > maxHeight {
return 0, 0, 0, errors.New(fmt.Sprintf("height cannot be above %d", maxHeight))
}
if quality > maxQuality {
return 0, 0, 0, errors.New(fmt.Sprintf("quality cannot be above %d", maxQuality))
}
return width, height, quality, nil
}