forked from fiatjaf/njump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
47 lines (39 loc) · 1001 Bytes
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"image/png"
"net/http"
)
func generate(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path, ":~", r.Header.Get("user-agent"))
code := r.URL.Path[1+len("njump/image/"):]
if code == "" {
fmt.Fprintf(w, "call /njump/image/<nip19 code>")
return
}
event, err := getEvent(r.Context(), code)
if err != nil {
http.Error(w, "error fetching event: "+err.Error(), 404)
return
}
lines := normalizeText(
replaceUserReferencesWithNames(r.Context(),
renderQuotesAsArrowPrefixedText(r.Context(),
event.Content,
),
),
)
img, err := drawImage(lines, getPreviewStyle(r))
if err != nil {
log.Printf("error writing image: %s", err)
http.Error(w, "error writing image!", 500)
return
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Cache-Control", "max-age=604800")
if err := png.Encode(w, img); err != nil {
log.Printf("error encoding image: %s", err)
http.Error(w, "error encoding image!", 500)
return
}
}