From 49fe2cc787fe64eb54afe9247eb43b1cb1f2499f Mon Sep 17 00:00:00 2001 From: icanotc <44480394+icanotc@users.noreply.github.com> Date: Sun, 13 Aug 2023 05:12:50 -0400 Subject: [PATCH] added untested qrcode location function --- backend/app/api/handlers/v1/v1_ctrl_qrcode.go | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/backend/app/api/handlers/v1/v1_ctrl_qrcode.go b/backend/app/api/handlers/v1/v1_ctrl_qrcode.go index a4352df..6c729bc 100644 --- a/backend/app/api/handlers/v1/v1_ctrl_qrcode.go +++ b/backend/app/api/handlers/v1/v1_ctrl_qrcode.go @@ -2,6 +2,9 @@ package v1 import ( "bytes" + "github.com/google/uuid" + "github.com/hay-kot/homebox/backend/internal/core/services" + "github.com/rs/zerolog/log" "image/png" "io" "net/http" @@ -17,6 +20,15 @@ import ( //go:embed assets/QRIcon.png var qrcodeLogo []byte +type query struct { + // 4,296 characters is the maximum length of a QR code + Data string `schema:"data" validate:"required,max=4296"` +} + +type locationQuery struct { + UUID uuid.UUID +} + // HandleGenerateQRCode godoc // // @Summary Create QR Code @@ -27,10 +39,6 @@ var qrcodeLogo []byte // @Router /v1/qrcode [GET] // @Security Bearer func (ctrl *V1Controller) HandleGenerateQRCode() errchain.HandlerFunc { - type query struct { - // 4,296 characters is the maximum length of a QR code - Data string `schema:"data" validate:"required,max=4296"` - } return func(w http.ResponseWriter, r *http.Request) error { q, err := adapters.DecodeQuery[query](r) @@ -64,3 +72,54 @@ func (ctrl *V1Controller) HandleGenerateQRCode() errchain.HandlerFunc { return qrc.Save(qrwriter) } } + +func (ctrl *V1Controller) HandleGenerateQRCodeForLocations() errchain.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) error { + data, err := adapters.DecodeQuery[locationQuery](r) + if err != nil { + return err + } + + auth := services.NewContext(r.Context()) + locations, err := ctrl.repo.Locations.GetOneByGroup(auth, auth.GID, data.UUID) + if err != nil { + return err + } + + image, err := png.Decode(bytes.NewReader(qrcodeLogo)) + if err != nil { + panic(err) + } + + toWriteCloser := struct { + io.Writer + io.Closer + }{ + Writer: w, + Closer: io.NopCloser(nil), + } + + var qrCodeBuffer bytes.Buffer + + for _, location := range locations.Items { + encodeStr := r.URL.String() + location.ID.String() //concat the url and then UUID + log.Debug().Msg(encodeStr) + qrc, err := qrcode.New(encodeStr) + if err != nil { + return err + } + + qrWriter := standard.NewWithWriter(toWriteCloser, standard.WithLogoImage(image)) + err = qrc.Save(qrWriter) + if err != nil { + return err + } + } + + // Return the concatenated QR code images as a response + w.Header().Set("Content-Type", "image/png") + w.Header().Set("Content-Disposition", "attachment; filename=qrcodes.png") + _, err = w.Write(qrCodeBuffer.Bytes()) + return err + } +}