You've already forked gun-manager-backend
156 lines
2.8 KiB
Go
156 lines
2.8 KiB
Go
package Guns
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
sql2 "database/sql"
|
|
"git.siteworxpro.com/gun-manager/sql"
|
|
"git.siteworxpro.com/gun-manager/sql/db"
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/nfnt/resize"
|
|
"image/jpeg"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func GetPhotoResize(c echo.Context) error {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
err := c.JSON(http.StatusNotFound, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
size, err := strconv.ParseInt(c.Param("size"), 10, 64)
|
|
if err != nil {
|
|
err := c.JSON(http.StatusNotFound, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
getDb := sql.GetDb()
|
|
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
img, err := jpeg.Decode(bytes.NewBuffer(photo))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m := resize.Resize(uint(size), 0, img, resize.Bicubic)
|
|
image := bytes.NewBuffer(make([]byte, 0))
|
|
err = jpeg.Encode(image, m, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = c.String(http.StatusOK, image.String())
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func DeletePhoto(c echo.Context) error {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
getDb := sql.GetDb()
|
|
err = getDb.Queries.DeleteGunPhotosById(context.Background(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = c.JSON(http.StatusNoContent, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func PostPhoto(c echo.Context) error {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
err := c.JSON(http.StatusNotFound, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
files, err := c.MultipartForm()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
getDb := sql.GetDb()
|
|
byId, err := getDb.Queries.GetGunById(context.Background(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, handler := range files.File {
|
|
for _, file := range handler {
|
|
open, err := file.Open()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fileData := make([]byte, file.Size)
|
|
_, err = open.Read(fileData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = getDb.Queries.InsertGunPhoto(context.Background(), db.InsertGunPhotoParams{
|
|
GunID: sql2.NullInt64{
|
|
Int64: byId.ID,
|
|
Valid: true,
|
|
},
|
|
Filename: sql2.NullString{
|
|
String: file.Filename,
|
|
Valid: true,
|
|
},
|
|
Photo: fileData,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetPhoto(c echo.Context) error {
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
err := c.JSON(http.StatusNotFound, "")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
getDb := sql.GetDb()
|
|
photo, err := getDb.Queries.GetGunPhotoData(context.Background(), id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = c.String(http.StatusOK, string(photo))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|