81 lines
1.5 KiB
Go
81 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func jsonError(c echo.Context, code int, msg string) error {
|
|
return c.JSON(code, resp_json{
|
|
Status: "Error",
|
|
Message: msg,
|
|
})
|
|
}
|
|
|
|
func jsonSuccess(c echo.Context, msg string) error {
|
|
return c.JSON(http.StatusOK, resp_json{
|
|
Status: "Success",
|
|
Message: msg,
|
|
})
|
|
}
|
|
|
|
func requireSession(c echo.Context) (map[string]string, error) {
|
|
session, err := ValidSession(c)
|
|
if err != nil {
|
|
jsonError(c, http.StatusUnauthorized, err.Error())
|
|
return nil, err
|
|
}
|
|
return session, nil
|
|
}
|
|
|
|
func anyEmpty(values ...string) bool {
|
|
for _, v := range values {
|
|
if strings.TrimSpace(v) == "" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func updateField(query string, args ...interface{}) error {
|
|
res, err := db.Exec(query, args...)
|
|
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
rows, _ := res.RowsAffected()
|
|
if rows == 0 {
|
|
return fmt.Errorf("erro ao rodar query")
|
|
}
|
|
return err
|
|
}
|
|
|
|
func readAndValidateImage(file *multipart.FileHeader) (string, error) {
|
|
if file.Size > 25*1024*1024 {
|
|
return "", fmt.Errorf("imagem excede 25MB")
|
|
}
|
|
ext := strings.ToLower(filepath.Ext(file.Filename))
|
|
if !formatFILES[ext] {
|
|
return "", fmt.Errorf("formato inválido")
|
|
}
|
|
open, err := file.Open()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer open.Close()
|
|
data, err := io.ReadAll(open)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return base64.StdEncoding.EncodeToString(data), nil
|
|
}
|