/
image.go
57 lines (49 loc) · 1.47 KB
/
image.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
// Package image provides imageserver/http.Parser implementations for imageserver/image.
package image
import (
"net/http"
"github.com/pierrre/imageserver"
imageserver_http "github.com/pierrre/imageserver/http"
)
// FormatParser is a imageserver/http.Parser implementation for imageserver/image.
//
// It takes the string "format" param from the HTTP URL query.
type FormatParser struct{}
// Parse implements imageserver/http.Parser.
func (parser *FormatParser) Parse(req *http.Request, params imageserver.Params) error {
imageserver_http.ParseQueryString("format", req, params)
if !params.Has("format") {
return nil
}
format, err := params.GetString("format")
if err != nil {
return err
}
if format == "jpg" {
format = "jpeg"
}
params.Set("format", format)
return nil
}
// Resolve implements imageserver/http.Parser.
func (parser *FormatParser) Resolve(param string) string {
if param == "format" {
return "format"
}
return ""
}
// QualityParser is a imageserver/http.Parser implementation for imageserver/image.
//
// It takes the integer "quality" param from the HTTP URL query.
type QualityParser struct{}
// Parse implements imageserver/http.Parser.
func (parser *QualityParser) Parse(req *http.Request, params imageserver.Params) error {
return imageserver_http.ParseQueryInt("quality", req, params)
}
// Resolve implements imageserver/http.Parser.
func (parser *QualityParser) Resolve(param string) string {
if param == "quality" {
return "quality"
}
return ""
}