tcp.ac/util.go

63 lines
1.4 KiB
Go
Raw Permalink Normal View History

2021-02-15 20:52:35 +00:00
package main
import (
2022-07-14 07:50:26 +00:00
"fmt"
"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-18 10:41:29 +00:00
func errThrow(c *gin.Context, respcode int, thrown error, msg []byte) error {
2022-01-21 12:58:22 +00:00
log.Error().
Str("IP", c.ClientIP()).
Str("User-Agent", c.GetHeader("User-Agent")).
2022-07-18 10:41:29 +00:00
Err(thrown).Msg(string(msg))
c.Data(respcode, "application/json", msg)
2022-07-14 07:50:26 +00:00
var err error
if thrown != nil {
2022-07-18 10:41:29 +00:00
err = fmt.Errorf("%s: %w", msg, thrown)
2022-07-14 07:50:26 +00:00
}
return err
}
2022-07-18 10:41:29 +00:00
// TODO: do we need this?
2022-07-14 07:50:26 +00:00
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
}
2022-07-18 10:41:29 +00:00
func getOldRef(p *Post) (*Post, error, bool) {
var oldRef []byte
oldRef, err := db.With("hsh").Get(p.Sum())
2022-07-14 07:50:26 +00:00
if err != nil {
2022-07-18 10:41:29 +00:00
return nil, err, false
2022-07-14 07:50:26 +00:00
}
2022-07-18 10:41:29 +00:00
p.Log().Trace().Caller().Msg("duplicate checksum in hash database, checking if file still exists...")
if db.With(p.TypeCode(true)).Has(oldRef) {
p.Log().Debug().Str("ogUid", string(oldRef)).
Msg("duplicate file found! returning original URL")
p.uid = string(oldRef)
p.key = ""
p.priv = false
return p, nil, true
}
p.Log().Trace().
Str("ogUid", string(oldRef)).
Msg("stale hash found, deleting entry...")
err = db.With("hsh").Delete(p.Sum())
if err != nil {
p.Log().Error().Err(err).Msg("failed to delete stale hash")
p = nil
}
return p, err, false
2021-02-15 20:52:35 +00:00
}