forked from tangled.org/core
this repo has no description
at master 834 B view raw
1package markup 2 3import ( 4 "crypto/hmac" 5 "crypto/sha256" 6 "encoding/hex" 7 "fmt" 8 9 "github.com/yuin/goldmark/ast" 10) 11 12func GenerateCamoURL(baseURL, secret, imageURL string) string { 13 h := hmac.New(sha256.New, []byte(secret)) 14 h.Write([]byte(imageURL)) 15 signature := hex.EncodeToString(h.Sum(nil)) 16 hexURL := hex.EncodeToString([]byte(imageURL)) 17 return fmt.Sprintf("%s/%s/%s", baseURL, signature, hexURL) 18} 19 20func (rctx *RenderContext) camoImageLinkTransformer(dst string) string { 21 // don't camo on dev 22 if rctx.IsDev { 23 return dst 24 } 25 26 if rctx.CamoUrl != "" && rctx.CamoSecret != "" { 27 return GenerateCamoURL(rctx.CamoUrl, rctx.CamoSecret, dst) 28 } 29 30 return dst 31} 32 33func (rctx *RenderContext) camoImageLinkAstTransformer(img *ast.Image) { 34 dst := string(img.Destination) 35 img.Destination = []byte(rctx.camoImageLinkTransformer(dst)) 36}