-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathencode.go
64 lines (55 loc) · 1.19 KB
/
encode.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
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by an MIT License.
* You may find a license copy in project root.
*/
package qrcode
import (
"io"
"github.com/pkg/errors"
qrc "github.com/skip2/go-qrcode"
)
const (
// TypePNG file as png
TypePNG = "png"
// TypeString QRCode
TypeString = "string"
// DefaultType default file type
DefaultType = TypePNG
)
// QREncoder holds info for QR code encoding
type QREncoder struct {
// content to encode
Content string
// desired encoding type
Type string
// desired image size in pixel, may not be honored
Size int
}
// Encode produces a QR code
func (q *QREncoder) Encode(dest io.Writer) (gotType string, err error) {
qrcode, err := qrc.New(q.Content, qrc.Medium)
if err != nil {
err = errors.Wrap(err, "cannot get a QR Code instance")
return
}
switch fileTypeCheck(q.Type) {
case TypePNG:
gotType = TypePNG
err = qrcode.Write(q.Size, dest)
case TypeString:
gotType = TypeString
_, err = dest.Write([]byte(qrcode.ToString(true)))
}
return
}
// fileTypeCheck checks incoming types
func fileTypeCheck(want string) string {
switch want {
case TypePNG, TypeString:
return want
default:
return DefaultType
}
}