78 lines
1.7 KiB
Go
78 lines
1.7 KiB
Go
package picture
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"git.umbach.dev/app-idea/rest-api/modules/config"
|
|
"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/routers/api/v1/user"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/google/uuid"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Test(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)
|
|
}
|
|
|
|
f, err := file.Open()
|
|
|
|
if err != nil {
|
|
log.Info("err001")
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
if _, err := io.Copy(buf, f); err != nil {
|
|
log.Info("err002")
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
}
|
|
|
|
log.Infoln("bytes", len(buf.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)
|
|
|
|
rabbitmq.PublishPicture(structs.RabbitmqPictureMessage{Picture: buf.Bytes(), UserId: user.Id, Filename: file.Filename})
|
|
|
|
filename := strings.Replace(uuid.New().String(), "-", "", -1)
|
|
fileExt := strings.Split(file.Filename, ".")[1]
|
|
image := fmt.Sprintf("%s.%s", filename, fileExt)
|
|
|
|
err = c.SaveFile(file, "./img/"+image)
|
|
|
|
if err != nil {
|
|
log.Infoln("err2", err)
|
|
return c.SendStatus(fiber.StatusInternalServerError)
|
|
}
|
|
|
|
if err != nil {
|
|
log.Infoln("err fileUpload:", err)
|
|
}
|
|
|
|
log.Infoln("no error")
|
|
|
|
return c.SendStatus(fiber.StatusCreated)
|
|
}
|