2021-02-15 20:52:35 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-07-14 07:50:26 +00:00
|
|
|
"fmt"
|
|
|
|
"image"
|
|
|
|
"io"
|
|
|
|
|
2021-04-24 05:31:18 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-09-01 07:19:10 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-02-15 20:52:35 +00:00
|
|
|
)
|
|
|
|
|
2022-07-14 07:50:26 +00:00
|
|
|
func errThrow(c *gin.Context, respcode int, thrown error, msg string) error {
|
2022-01-21 12:58:22 +00:00
|
|
|
log.Error().
|
|
|
|
Str("IP", c.ClientIP()).
|
|
|
|
Str("User-Agent", c.GetHeader("User-Agent")).
|
|
|
|
Err(thrown).Msg(msg)
|
|
|
|
c.String(respcode, msg)
|
2022-07-14 07:50:26 +00:00
|
|
|
var err error
|
|
|
|
if thrown != nil {
|
|
|
|
err = fmt.Errorf("%s: %s", msg, thrown)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSize(s io.Seeker) (size int64, err error) {
|
|
|
|
// get size of file
|
|
|
|
if _, err = s.Seek(0, 0); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 2 == from the end of the file
|
|
|
|
if size, err = s.Seek(0, 2); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = s.Seek(0, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkImage(r io.ReadSeeker) (fmt string, err error) {
|
|
|
|
// in theory this makes sure the file is an image via magic bytes
|
|
|
|
_, fmt, err = image.Decode(r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = r.Seek(0, 0)
|
|
|
|
return
|
2021-02-15 20:52:35 +00:00
|
|
|
}
|