/
qrCode.go
57 lines (46 loc) · 1.54 KB
/
qrCode.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
package listeningSession
import (
"bytes"
"image/jpeg"
"image/png"
"net/http"
"github.com/partyoffice/spotifete/config"
. "github.com/partyoffice/spotifete/shared"
"github.com/skip2/go-qrcode"
)
func QrCodeAsJpeg(joinId string, disableBorder bool, size int) (*bytes.Buffer, *SpotifeteError) {
qrCode, spotifeteError := QrCode(joinId, disableBorder)
if spotifeteError != nil {
return nil, spotifeteError
}
jpegBuffer := new(bytes.Buffer)
err := jpeg.Encode(jpegBuffer, qrCode.Image(size), nil)
if err != nil {
return nil, NewError("Could not encode qr code as image.", err, http.StatusInternalServerError)
}
return jpegBuffer, nil
}
func QrCodeAsPng(joinId string, disableBorder bool, size int) (*bytes.Buffer, *SpotifeteError) {
qrCode, spotifeteError := QrCode(joinId, disableBorder)
if spotifeteError != nil {
return nil, spotifeteError
}
pngBuffer := new(bytes.Buffer)
err := png.Encode(pngBuffer, qrCode.Image(size))
if err != nil {
return nil, NewError("Could not encode qr code as image.", err, http.StatusInternalServerError)
}
return pngBuffer, nil
}
func QrCode(joinId string, disableBorder bool) (qrcode.QRCode, *SpotifeteError) {
qrCode, err := qrcode.New(qrCodeContent(joinId), qrcode.Medium)
if err != nil {
return qrcode.QRCode{}, NewError("Could not create QR code.", err, http.StatusInternalServerError)
}
qrCode.DisableBorder = disableBorder
return *qrCode, nil
}
func qrCodeContent(joinId string) string {
baseUrl := config.Get().SpotifeteConfiguration.BaseUrl
return baseUrl + "/session/view/" + joinId
}