Skip to content
Permalink
Browse files

(qr): Add recovery level

  • Loading branch information
erbesharat committed Aug 18, 2019
1 parent 8644cbd commit 7a7c88ec3d3c0897833b69b0ce0dd57e214020c5
Showing with 37 additions and 3 deletions.
  1. +1 −1 go.mod
  2. +9 −2 qr.go
  3. +27 −0 setup_test.go
2 go.mod
@@ -13,7 +13,7 @@ require (
github.com/miekg/dns v1.1.15
github.com/prometheus/client_golang v1.0.0
github.com/quasoft/memstore v0.0.0-20180925164028-84a050167438 // indirect
github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9 // indirect
github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9
github.com/spf13/afero v1.2.2
github.com/steambap/captcha v1.3.0 // indirect
golang.org/x/image v0.0.0-20190424155947-59b11bec70c7 // indirect
11 qr.go
@@ -17,13 +17,14 @@ type Qr struct {
Size int
BackgroundColor string
ForegroundColor string
RecoveryLevel qrcode.RecoveryLevel

backgroundColor color.Color
foregroundColor color.Color
}

func (qr *Qr) Redirect(w http.ResponseWriter, r *http.Request) error {
Qr, err := qrcode.New(r.Host+r.URL.String(), qrcode.High)
Qr, err := qrcode.New(r.Host+r.URL.String(), qr.RecoveryLevel)
if err != nil {
return fmt.Errorf("Couldn't generate the Qr instance: %s", err.Error())
}
@@ -62,6 +63,12 @@ func (qr *Qr) ParseQr(c *caddy.Controller) error {
qr.BackgroundColor = c.RemainingArgs()[0]
case "foreground":
qr.ForegroundColor = c.RemainingArgs()[0]
case "recovery_level":
value, err := strconv.Atoi(c.RemainingArgs()[0])
if err != nil {
return fmt.Errorf("<QR>: Couldn't parse the recovery_level. It should be from 0 to 3")
}
qr.RecoveryLevel = qrcode.RecoveryLevel(value)
default:
return c.ArgErr() // unhandled option for QR config
}
@@ -76,7 +83,7 @@ func (qr *Qr) SetDefaults() {
qr.BackgroundColor = "ffffffff"
}
if qr.ForegroundColor == "" {
qr.ForegroundColor = "00000000"
qr.ForegroundColor = "000000ff"
}

if len(qr.BackgroundColor) == 7 {
@@ -365,6 +365,33 @@ func TestCaddyParse(t *testing.T) {
},
},
},
{
`
txtdirect {
enable host
redirect https://example.com
qr {
size 256
background "#ffffff"
foreground "#000000"
recovery_level 1
}
resolver 127.0.0.1
}
`,
false,
Config{
Redirect: "https://example.com",
Enable: []string{"host"},
Resolver: "127.0.0.1",
Qr: Qr{
Size: 256,
BackgroundColor: "ffffffff",
ForegroundColor: "000000ff",
RecoveryLevel: 1,
},
},
},
{
`
txtdirect {

0 comments on commit 7a7c88e

Please sign in to comment.
You can’t perform that action at this time.