package picture

import (
	"fmt"
	"strings"

	"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)
	}

	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)
}
