Added picture save
parent
b6fb581c59
commit
f26c3b37e0
|
@ -0,0 +1,62 @@
|
||||||
|
package picture
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.umbach.dev/app-idea/rest-api/modules/config"
|
||||||
|
"git.umbach.dev/app-idea/rest-api/modules/database"
|
||||||
|
"git.umbach.dev/app-idea/rest-api/modules/rabbitmq"
|
||||||
|
"git.umbach.dev/app-idea/rest-api/modules/structs"
|
||||||
|
"git.umbach.dev/app-idea/rest-api/modules/utils"
|
||||||
|
"git.umbach.dev/app-idea/rest-api/routers/api/v1/user"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Save(c *fiber.Ctx) error {
|
||||||
|
log.Infoln("header", string(c.Request().Header.ContentType()))
|
||||||
|
|
||||||
|
log.Infoln("formValue", c.FormValue("image"))
|
||||||
|
|
||||||
|
file, err := c.FormFile("image")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Infoln("err1", err)
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := utils.FormFileToBytes(file)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Info("formFileToBytes", err)
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info("b", len(bytes))
|
||||||
|
|
||||||
|
user, err := user.GetUserBySessionId(c.Cookies(config.Cfg.Settings.Cookies.SessionId))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Infoln("err004", err)
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infoln("userId", user.Id)
|
||||||
|
|
||||||
|
filename := strings.Replace(file.Filename, "-", "", -1)
|
||||||
|
|
||||||
|
rabbitmq.PublishPicture(structs.RabbitmqPictureMessage{Picture: bytes, UserId: user.Id, Filename: filename})
|
||||||
|
|
||||||
|
db := database.DB
|
||||||
|
|
||||||
|
res := db.Create(&structs.Picture{UserId: user.Id, PictureId: strings.Replace(uuid.New().String(), "-", "", -1), Url: filename, CreatedAt: time.Now()})
|
||||||
|
|
||||||
|
if res.Error != nil {
|
||||||
|
log.Warnln("Failed to insert user to db:", res.Error)
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.SendStatus(fiber.StatusOK)
|
||||||
|
}
|
Loading…
Reference in New Issue