auto-sign thumbnails
Some checks failed
docker / Docker (push) Has been cancelled
test / Test (push) Has been cancelled

This commit is contained in:
Failure 2024-07-30 03:57:31 -07:00
parent f348bb92e3
commit 6a88d9d0f4
3 changed files with 16 additions and 179 deletions

View file

@ -2,12 +2,14 @@ package imagorvideoextended
import (
"context"
"crypto/sha256"
"github.com/antchfx/htmlquery"
"github.com/cshum/imagor"
"github.com/cshum/imagor/imagorpath"
"github.com/gabriel-vasile/mimetype"
"go.uber.org/zap"
"io"
"os"
"strings"
)
@ -39,6 +41,18 @@ func (p *Processor) Shutdown(_ context.Context) error {
return nil
}
func subThumbnail(url string) string {
key := os.Getenv("IMAGOR_SECRET")
params := imagorpath.Params{
Image: url,
FitIn: true,
Width: 400,
Height: 400,
}
path := imagorpath.Generate(params, imagorpath.NewHMACSigner(sha256.New, 0, key))
return path
}
// Process implements imagor.Processor interface
func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorpath.Params, load imagor.LoadFunc) (out *imagor.Blob, err error) {
defer func() {
@ -57,6 +71,7 @@ func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorp
}
}
}()
var mime = mimetype.Detect(in.Sniff())
if typ := mime.String(); !strings.HasPrefix(typ, "text/html") {
@ -103,7 +118,7 @@ func (p *Processor) Process(ctx context.Context, in *imagor.Blob, params imagorp
case "og:image":
fallthrough
case "twitter:image:src":
meta.Image = val
meta.Image = subThumbnail(val)
break
case "og:title":