feat: add qrcode
All checks were successful
Deploy / deploy (push) Successful in 1m36s

This commit is contained in:
xuu
2023-10-14 07:54:05 -06:00
parent b5dfb72c30
commit b7f4d06731
6 changed files with 69 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"net/http"
@@ -19,6 +20,9 @@ import (
"sour.is/x/paste/src/pkg/readutil"
"sour.is/x/toolbox/httpsrv"
"sour.is/x/toolbox/log"
"github.com/yeqown/go-qrcode/v2"
"github.com/yeqown/go-qrcode/writer/standard"
)
func init() {
@@ -36,6 +40,8 @@ func init() {
{Name: "getPaste", Method: "GET", Pattern: "/api/{id}", HandlerFunc: p.getPaste},
{Name: "getPaste", Method: "GET", Pattern: "/api/get/{id}", HandlerFunc: p.getPaste},
{Name: "postPaste", Method: "POST", Pattern: "/api", HandlerFunc: p.postPaste},
{Name: "qrcode", Method: "GET", Pattern: "/qr", HandlerFunc: p.qrcode},
})
}
@@ -119,6 +125,25 @@ func chkGone(path string) bool {
return false
}
type noop struct {
io.Writer
}
func (noop) Close() error { return nil }
func (p *Paste) qrcode(w http.ResponseWriter, r *http.Request) {
qrc, err := qrcode.New(r.URL.Query().Get("u"))
if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity)
fmt.Fprintf(w, "could not generate QRCode: %v", err)
return
}
wr := standard.NewWithWriter(noop{w}, standard.WithCircleShape())
w.WriteHeader(http.StatusOK)
_ = qrc.Save(wr)
}
func (p *Paste) getRandom(w http.ResponseWriter, _ *http.Request) {
s := make([]byte, p.randBytes)
_, _ = rand.Read(s)