forked from thoas/picfit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractors.go
51 lines (37 loc) · 919 Bytes
/
extractors.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
package extractors
import (
"fmt"
"github.com/thoas/muxer"
"github.com/thoas/picfit/image"
"mime"
"net/url"
)
func Operation(req *muxer.Request) (*image.Operation, error) {
operation, ok := image.Operations[req.QueryString["op"]]
if !ok {
return nil, fmt.Errorf("Invalid method %s or invalid parameters", operation)
}
return operation, nil
}
func Format(req *muxer.Request) (string, error) {
format, ok := req.QueryString["fmt"]
if !ok {
return "", nil
}
if _, ok := image.ContentTypes[format]; !ok {
return "", fmt.Errorf("Unknown format %s", format)
}
return format, nil
}
func URL(req *muxer.Request) (*url.URL, error) {
value, ok := req.QueryString["url"]
if ok {
url, err := url.Parse(value)
mimetype := mime.TypeByExtension(value)
_, ok := image.Formats[mimetype]
if ok || err == nil {
return url, nil
}
}
return nil, fmt.Errorf("URL %s is not valid", value)
}