-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5473f76
commit 7c68aa7
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters