Skip to content

Commit ccd8c7d

Browse files
ashthespyIngmarStein
authored andcommitted
Centralise and use ValidateColorFilter across API, CLI, and library
1 parent 245e411 commit ccd8c7d

File tree

4 files changed

+22
-24
lines changed

4 files changed

+22
-24
lines changed

cmd/api.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,9 @@ func renderHandler(w http.ResponseWriter, req *http.Request) {
6666
}
6767

6868
// Default to "none" if color_filter is missing
69-
filterType := encode.ColorFilterType(r.ColorFilter)
70-
if r.ColorFilter == "" {
71-
filterType = encode.ColorNone
72-
} else if !filterType.IsValid() {
73-
http.Error(w, fmt.Sprintf("invalid color filter: %q\nSupported filters: %s",
74-
r.ColorFilter,
75-
strings.Join(encode.SupportedColorFilters(), ", "),
76-
), http.StatusBadRequest)
69+
filterType, err := encode.ValidateColorFilter(encode.ColorFilterType(r.ColorFilter))
70+
if err != nil {
71+
http.Error(w, err.Error(), http.StatusBadRequest)
7772
return
7873
}
7974

cmd/render.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,9 @@ func render(cmd *cobra.Command, args []string) error {
172172
runtime.InitHTTP(cache)
173173
runtime.InitCache(cache)
174174

175-
filterType := encode.ColorFilterType(colorFilter)
176-
if colorFilter == "" {
177-
filterType = encode.ColorNone
178-
} else if !filterType.IsValid() {
179-
return fmt.Errorf("invalid color filter: %q\nSupported filters: %s",
180-
colorFilter,
181-
strings.Join(encode.SupportedColorFilters(), ", "),
182-
)
175+
filterType, err := encode.ValidateColorFilter(encode.ColorFilterType(colorFilter))
176+
if err != nil {
177+
return err
183178
}
184179

185180
filters := &encode.RenderFilters{

encode/filter.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,23 @@ func (f *ColorFilterType) UnmarshalJSON(b []byte) error {
148148
return err
149149
}
150150
*f = ColorFilterType(s)
151-
if !f.IsValid() {
152-
return fmt.Errorf("invalid color filter: %q:\nSupported filters: %s", s, strings.Join(SupportedColorFilters(), ", "))
153-
}
154151
return nil
155152
}
156153

154+
func ValidateColorFilter(input ColorFilterType) (ColorFilterType, error) {
155+
if input == "" {
156+
return ColorNone, nil
157+
}
158+
filterType := ColorFilterType(input)
159+
if !filterType.IsValid() {
160+
return "", fmt.Errorf("invalid color filter: %q\nSupported filters: %s",
161+
input,
162+
strings.Join(SupportedColorFilters(), ", "),
163+
)
164+
}
165+
return filterType, nil
166+
}
167+
157168
func FromFilterType(f ColorFilterType) (ImageFilter, error) {
158169
if f == ColorNone {
159170
return nil, nil // explicit noop skip

library/library.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ func render_app(pathPtr *C.char, configPtr *C.char, width, height, magnify, maxD
6060
if err := json.Unmarshal([]byte(filtersStr), &parsed); err != nil {
6161
return nil, -3, nil, C.CString(fmt.Sprintf("invalid filters JSON: %v", err))
6262
}
63-
if parsed.ColorFilter != "" && !encode.ColorFilterType(parsed.ColorFilter).IsValid() {
64-
return nil, -4, nil, C.CString(fmt.Sprintf("invalid color filter: %q\nSupported filters: %s",
65-
parsed.ColorFilter,
66-
strings.Join(encode.SupportedColorFilters(), ", "),
67-
))
63+
if _, err := encode.ValidateColorFilter(parsed.ColorFilter); err != nil {
64+
return nil, -4, nil, C.CString(err.Error())
6865
}
6966
filters = &parsed
7067
}

0 commit comments

Comments
 (0)