mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-03 08:10:28 +00:00
code reading changes
This commit is contained in:
parent
2ac62feb2b
commit
449650e72c
1 changed files with 27 additions and 7 deletions
|
@ -2,9 +2,9 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/hay-kot/homebox/backend/internal/core/services"
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/signintech/gopdf"
|
||||||
"image/png"
|
"image/png"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -25,8 +25,8 @@ type query struct {
|
||||||
Data string `schema:"data" validate:"required,max=4296"`
|
Data string `schema:"data" validate:"required,max=4296"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type locationQuery struct {
|
type nestedQuery struct {
|
||||||
UUID uuid.UUID
|
Nested bool `schema:"nested" validate:"required,"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleGenerateQRCode godoc
|
// HandleGenerateQRCode godoc
|
||||||
|
@ -73,15 +73,24 @@ func (ctrl *V1Controller) HandleGenerateQRCode() errchain.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleGenerateQRCodeForLocations godoc
|
||||||
|
//
|
||||||
|
// @Summary Create PDF of QR codes of a location
|
||||||
|
|
||||||
func (ctrl *V1Controller) HandleGenerateQRCodeForLocations() errchain.HandlerFunc {
|
func (ctrl *V1Controller) HandleGenerateQRCodeForLocations() errchain.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) error {
|
return func(w http.ResponseWriter, r *http.Request) error {
|
||||||
data, err := adapters.DecodeQuery[locationQuery](r)
|
data, err := adapters.DecodeQuery[nestedQuery](r)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
routeUUID, err := ctrl.routeUUID(r, "location_id")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
auth := services.NewContext(r.Context())
|
auth := services.NewContext(r.Context())
|
||||||
locations, err := ctrl.repo.Locations.GetOneByGroup(auth, auth.GID, data.UUID)
|
locations, err := ctrl.repo.Locations.GetOneByGroup(auth, auth.GID, routeUUID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -101,25 +110,36 @@ func (ctrl *V1Controller) HandleGenerateQRCodeForLocations() errchain.HandlerFun
|
||||||
|
|
||||||
var qrCodeBuffer bytes.Buffer
|
var qrCodeBuffer bytes.Buffer
|
||||||
|
|
||||||
|
pdf := gopdf.GoPdf{}
|
||||||
|
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
|
||||||
|
|
||||||
|
if data.Nested {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
for _, location := range locations.Items {
|
for _, location := range locations.Items {
|
||||||
encodeStr := r.URL.String() + location.ID.String() //concat the url and then UUID
|
encodeStr := r.URL.String() + location.ID.String() //concat the url and then UUID
|
||||||
log.Debug().Msg(encodeStr)
|
log.Debug().Msg(encodeStr)
|
||||||
qrc, err := qrcode.New(encodeStr)
|
|
||||||
|
qrc, err := qrcode.New(encodeStr) //create QR code obj from screen
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
qrWriter := standard.NewWithWriter(toWriteCloser, standard.WithLogoImage(image))
|
qrWriter := standard.NewWithWriter(toWriteCloser, standard.WithLogoImage(image))
|
||||||
|
|
||||||
err = qrc.Save(qrWriter)
|
err = qrc.Save(qrWriter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = w.Write(qrCodeBuffer.Bytes())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the concatenated QR code images as a response
|
// Return the concatenated QR code images as a response
|
||||||
w.Header().Set("Content-Type", "image/png")
|
w.Header().Set("Content-Type", "image/png")
|
||||||
w.Header().Set("Content-Disposition", "attachment; filename=qrcodes.png")
|
w.Header().Set("Content-Disposition", "attachment; filename=qrcodes.png")
|
||||||
_, err = w.Write(qrCodeBuffer.Bytes())
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue