~mendelmaleh/dummy

ref: 928a478eb6b4f0f7362d7453d11bcd6c54824dba dummy/cmd/server/options.go -rw-r--r-- 1.4 KiB
928a478eMendel E Update .gitignore 1 year, 2 months ago
                                                                                
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
package main

import (
	"net/url"
	"strconv"

	"git.sr.ht/~mendelmaleh/dummy"
)

// FromForm gets values from the form (which includes the url encoded query)
// and applies them to our Options. To ne used with r.Form after r.ParseForm().
func FromForm(opt dummy.Options, form url.Values) (dummy.Options, error) {
	// image
	if v, ok := form["width"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return opt, err
		}

		opt.Width = i
		opt.Height = i
	}

	if v, ok := form["height"]; ok {
		i, err := strconv.Atoi(v[0])
		if err != nil {
			return opt, err
		}

		opt.Height = i
	}

	if v, ok := form["mask"]; ok {
		opt.Mask = v[0]
	}

	if colors, ok := form["color"]; ok {
		for i, h := range colors {
			c, err := dummy.Hex(h)
			if err != nil {
				return opt, err
			}

			opt.Colors[i] = c
		}
	}

	// text
	if v, ok := form["text"]; ok {
		opt.Text = v[0]
	}

	if v, ok := form["font"]; ok {
		// TODO: less strict font matching
		if f, ok := fonts[v[0]]; ok {
			opt.Font = f
		}
	}

	if v, ok := form["size"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return opt, err
		}

		opt.Size = f
	}

	if v, ok := form["dpi"]; ok {
		f, err := strconv.ParseFloat(v[0], 64)
		if err != nil {
			return opt, err
		}

		opt.DPI = f
	}

	// web
	if v, ok := form["desc"]; ok {
		opt.Desc = v[0]
	}

	// apply limits and hacks
	opt.Normalize()

	return opt, nil
}