Skip to content

Commit

Permalink
feat: initial
Browse files Browse the repository at this point in the history
  • Loading branch information
AvineshTripathi committed Sep 22, 2023
1 parent 5473f76 commit 7c68aa7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
57 changes: 57 additions & 0 deletions handlers/feedback_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package handlers

import (
"bytes"
"io"
"mime/multipart"
"net/http"
"path/filepath"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

func (handler *ApiHandler) NewFeedbackHandler(c *gin.Context) {

err := c.Request.ParseMultipartForm(1000 << 20)
if err != nil {
logrus.WithError(err).Error("Unable to parse form")
c.JSON(http.StatusBadRequest, gin.H{"error": "Unable to parse form"})
return
}
email := c.PostForm("email")
description := c.PostForm("description")
imageFile, _, err := c.Request.FormFile("image")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Unable to get image file"})
return
}
defer imageFile.Close()

var requestBody bytes.Buffer
writer := multipart.NewWriter(&requestBody)

_ = writer.WriteField("Email", email)
_ = writer.WriteField("Description", description)

imagePart, err := writer.CreateFormFile("files[0]", filepath.Base("temp-image"))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create form file"})
return
}
_, _ = io.Copy(imagePart, imageFile)

writer.Close()

var url = ""

resp, err := http.Post(url, writer.FormDataContentType(), &requestBody)
if err != nil {
logrus.WithError(err).Error("scan failed")
c.JSON(http.StatusInternalServerError, gin.H{"error": "scan failed"})
return
}
defer resp.Body.Close()

c.JSON(http.StatusOK, resp.Body)
}
2 changes: 2 additions & 0 deletions internal/api/v1/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,7 @@ func Endpoints(ctx context.Context, telemetry bool, analytics utils.Analytics, d

router.NoRoute(gin.WrapH(http.FileServer(assetFS())))

router.POST("/feedback", api.NewFeedbackHandler)

return router
}

0 comments on commit 7c68aa7

Please sign in to comment.