-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
75 lines (59 loc) · 1.28 KB
/
util.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
package imogiri
import (
"errors"
"fmt"
"github.com/rakyll/magicmime"
"strings"
)
func formatChecker(sourceFormats, targetFormats []string, source, target string) error {
err := formatValidator(source, sourceFormats)
if err != nil {
return err
}
err = formatValidator(target, targetFormats)
if err != nil {
return err
}
return nil
}
func formatValidator(str string, formats []string) error {
if str == "" {
return errors.New("Please specify the format of the image")
}
if !isSupported(str, formats) {
return fmt.Errorf("Format %q is not supported", str)
}
return nil
}
func isSupported(str string, formats []string) bool {
for _, f := range formats {
if f == str {
return true
}
}
return false
}
func mimeBuffer(b []byte) (string, error) {
m, err := magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_ERROR)
if err != nil {
return "", err
}
defer m.Close()
return m.TypeByBuffer(b)
}
func buildMatrix(sourceFormats, targetFormats []string) []string {
matrix := []string{}
for _, s := range sourceFormats {
for _, t := range targetFormats {
matrix = append(matrix, strings.Join([]string{s, t}, ":"))
}
}
return matrix
}
func actionString(action Action) string {
switch action {
case ResizeAction:
return "RESIZE"
}
return ""
}