it's working! even on twitter.

This commit is contained in:
fiatjaf
2023-05-10 23:05:31 -03:00
parent 48ed02dbd0
commit 00b824f98e
6 changed files with 52 additions and 8 deletions

View File

@@ -7,6 +7,7 @@
<meta property="og:title" content="{{.title}}" />
{{ if .metadata.Picture }}
<meta property="og:image" content="{{.metadata.Picture}}" />
<meta property="twitter:image" content="{{.proxy}}{{.metadata.Picture}}" />
{{end}} {{ if .metadata.About }}
<meta property="og:description" content="{{.metadata.About}}" />
{{end}}
@@ -29,7 +30,7 @@
<meta property="twitter:card" content="summary" />
{{ if .image }}
<meta property="og:image" content="{{.image}}" />
<meta name="twitter:image" content="{{.image}}" />
<meta name="twitter:image" content="{{.proxy}}{{.image}}" />
{{end}} {{ if .video }}
<meta property="og:video" content="{{.video}}" />
<meta property="og:video:secure_url" content="{{.video}}" />

View File

@@ -23,7 +23,8 @@ func generate(w http.ResponseWriter, r *http.Request) {
}
lines := normalizeText(event.Content)
img, err := drawImage(lines)
img, err := drawImage(lines, getPreviewStyle(r))
if err != nil {
log.Printf("error writing image: %s", err)
http.Error(w, "error writing image!", 500)

View File

@@ -8,6 +8,7 @@ import (
func main() {
http.HandleFunc("/image/", generate)
http.HandleFunc("/proxy/", proxy)
http.HandleFunc("/", render)
port := os.Getenv("PORT")

32
proxy.go Normal file
View File

@@ -0,0 +1,32 @@
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
)
func proxy(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
src := r.URL.Query().Get("src")
urlParsed, err := url.Parse(src)
if err != nil {
http.Error(w, "Invalid URL", http.StatusBadRequest)
return
}
if urlParsed.Scheme != "http" && urlParsed.Scheme != "https" {
http.Error(w, "The URL scheme is neither HTTP nor HTTPS", http.StatusBadRequest)
return
}
proxy := httputil.ReverseProxy{
Director: func(r *http.Request) {
r.URL = urlParsed
r.Host = urlParsed.Host
},
}
proxy.ServeHTTP(w, r)
}

View File

@@ -19,6 +19,7 @@ var eventHTML string
var tmpl = template.Must(template.New("event").Parse(eventHTML))
func render(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
w.Header().Set("Content-Type", "text/html")
code := r.URL.Path[1:]
@@ -65,7 +66,6 @@ func render(w http.ResponseWriter, r *http.Request) {
if len(imageMatch) > 0 {
image = imageMatch[0]
}
fmt.Println("IMAGE", image)
videoMatch := regexp.MustCompile(`https:\/\/[^ ]*\.(mp4|webm)`).FindStringSubmatch(event.Content)
var video string
@@ -170,6 +170,7 @@ func render(w http.ResponseWriter, r *http.Request) {
"videoType": videoType,
"image": image,
"video": video,
"proxy": "https://" + hostname + "/proxy?src=",
"eventJSON": string(eventJSON),
}
if err := tmpl.ExecuteTemplate(w, "event", params); err != nil {

18
text.go
View File

@@ -27,10 +27,18 @@ func normalizeText(t string) []string {
return lines
}
func drawImage(lines []string) (image.Image, error) {
func drawImage(lines []string, style string) (image.Image, error) {
width, height, paddingLeft := 700, 525, 0
switch style {
case "twitter":
height = 366
case "telegram":
paddingLeft = 15
}
// get the physical image ready with colors/size
fg, bg := image.Black, image.White
rgba := image.NewRGBA(image.Rect(0, 0, 700, 525))
rgba := image.NewRGBA(image.Rect(0, 0, width, height))
// draw the empty image
draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src)
@@ -51,7 +59,7 @@ func drawImage(lines []string) (image.Image, error) {
// draw each line separately
var count float64 = 1
for _, line := range lines {
if err := drawText(c, line, count); err != nil {
if err := drawText(c, line, count, paddingLeft); err != nil {
return nil, err
}
count++
@@ -60,12 +68,12 @@ func drawImage(lines []string) (image.Image, error) {
return rgba, nil
}
func drawText(c *freetype.Context, text string, line float64) error {
func drawText(c *freetype.Context, text string, line float64, paddingLeft int) error {
// We need an offset because we need to know where exactly on the
// image to place the text. The `line` is how much of an offset
// that we need to provide (which line the text is going on).
offsetY := 10 + int(c.PointToFix32(FONT_SIZE*line)>>8)
_, err := c.DrawString(text, freetype.Pt(10, offsetY))
_, err := c.DrawString(text, freetype.Pt(10+paddingLeft, offsetY))
return err
}