server, post: fix social media images
Signed-off-by: Naman Sood <mail@nsood.in>
This commit is contained in:
parent
42b8bcc32a
commit
ab6c3af0b7
3 changed files with 27 additions and 18 deletions
10
post.go
10
post.go
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -31,7 +32,7 @@ type Post struct {
|
||||||
Slug string
|
Slug string
|
||||||
Metadata Metadata
|
Metadata Metadata
|
||||||
Contents string
|
Contents string
|
||||||
Image bytes.Buffer
|
Image []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func newPost(slug string) (*Post, error) {
|
func newPost(slug string) (*Post, error) {
|
||||||
|
@ -75,10 +76,15 @@ func newPost(slug string) (*Post, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
url := blogURL + "/" + slug
|
url := blogURL + "/" + slug
|
||||||
err = createImage(post.Metadata.Title, post.Metadata.Summary, url, &post.Image)
|
var buf bytes.Buffer
|
||||||
|
err = createImage(post.Metadata.Title, post.Metadata.Summary, url, &buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not create post image: %v", err)
|
return nil, fmt.Errorf("could not create post image: %v", err)
|
||||||
}
|
}
|
||||||
|
post.Image, err = io.ReadAll(&buf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return post, nil
|
return post, nil
|
||||||
}
|
}
|
||||||
|
|
33
server.go
33
server.go
|
@ -26,7 +26,7 @@ type server struct {
|
||||||
templates map[string]*raymond.Template
|
templates map[string]*raymond.Template
|
||||||
postList
|
postList
|
||||||
styles map[string]string
|
styles map[string]string
|
||||||
homeImage bytes.Buffer
|
homeImage []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func newServer() (*server, error) {
|
func newServer() (*server, error) {
|
||||||
|
@ -34,7 +34,12 @@ func newServer() (*server, error) {
|
||||||
staticHandler: http.FileServer(http.Dir("static/")),
|
staticHandler: http.FileServer(http.Dir("static/")),
|
||||||
}
|
}
|
||||||
|
|
||||||
err := createImage(blogTitle, blogSummary, blogURL, &s.homeImage)
|
var imgBuffer bytes.Buffer
|
||||||
|
err := createImage(blogTitle, blogSummary, blogURL, &imgBuffer)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.homeImage, err = io.ReadAll(&imgBuffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -109,7 +114,7 @@ func (s *server) router(res http.ResponseWriter, req *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if slug == "about.png" {
|
if slug == "about.png" {
|
||||||
s.renderImage(res, req, &s.homeImage)
|
s.renderImage(res, req, s.homeImage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,7 +123,7 @@ func (s *server) router(res http.ResponseWriter, req *http.Request) {
|
||||||
s.postPage(p, res, req)
|
s.postPage(p, res, req)
|
||||||
return
|
return
|
||||||
} else if slug == p.Slug+"/about.png" {
|
} else if slug == p.Slug+"/about.png" {
|
||||||
s.renderImage(res, req, &p.Image)
|
s.renderImage(res, req, p.Image)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,11 +145,12 @@ func (s *server) errorInRequest(res http.ResponseWriter, req *http.Request, err
|
||||||
log.Printf("ERR %s: %s", req.URL.Path, err)
|
log.Printf("ERR %s: %s", req.URL.Path, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) createWebPage(title, subtitle, contents string) (string, error) {
|
func (s *server) createWebPage(title, subtitle, contents, path string) (string, error) {
|
||||||
ctx := map[string]interface{}{
|
ctx := map[string]interface{}{
|
||||||
"title": title,
|
"title": title,
|
||||||
"subtitle": subtitle,
|
"subtitle": subtitle,
|
||||||
"contents": contents,
|
"contents": contents,
|
||||||
|
"path": blogURL + path,
|
||||||
}
|
}
|
||||||
return s.templates["page"].Exec(ctx)
|
return s.templates["page"].Exec(ctx)
|
||||||
}
|
}
|
||||||
|
@ -155,7 +161,7 @@ func (s *server) postPage(p *Post, res http.ResponseWriter, req *http.Request) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.errorInRequest(res, req, err)
|
s.errorInRequest(res, req, err)
|
||||||
}
|
}
|
||||||
page, err := s.createWebPage(p.Metadata.Title, p.Metadata.Summary, contents)
|
page, err := s.createWebPage(p.Metadata.Title, p.Metadata.Summary, contents, req.URL.Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.errorInRequest(res, req, err)
|
s.errorInRequest(res, req, err)
|
||||||
}
|
}
|
||||||
|
@ -175,7 +181,7 @@ func (s *server) homePage(res http.ResponseWriter, req *http.Request) {
|
||||||
posts = posts + summary
|
posts = posts + summary
|
||||||
}
|
}
|
||||||
|
|
||||||
page, err := s.createWebPage("Home", blogSummary, posts)
|
page, err := s.createWebPage("Home", blogSummary, posts, "")
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.errorInRequest(res, req, err)
|
s.errorInRequest(res, req, err)
|
||||||
|
@ -184,12 +190,9 @@ func (s *server) homePage(res http.ResponseWriter, req *http.Request) {
|
||||||
res.Write([]byte(page))
|
res.Write([]byte(page))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) renderImage(res http.ResponseWriter, req *http.Request, img io.Reader) {
|
func (s *server) renderImage(res http.ResponseWriter, req *http.Request, img []byte) {
|
||||||
res.Header().Add("content-type", "image/png")
|
res.Header().Add("content-type", "image/png")
|
||||||
_, err := io.Copy(res, img)
|
res.Write(img)
|
||||||
if err != nil {
|
|
||||||
s.errorInRequest(res, req, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) loadStylesheet(res http.ResponseWriter, req *http.Request, filename string) (ok bool) {
|
func (s *server) loadStylesheet(res http.ResponseWriter, req *http.Request, filename string) (ok bool) {
|
||||||
|
@ -203,8 +206,9 @@ func (s *server) loadStylesheet(res http.ResponseWriter, req *http.Request, file
|
||||||
}
|
}
|
||||||
|
|
||||||
func createImage(title, summary, url string, out io.Writer) error {
|
func createImage(title, summary, url string, out io.Writer) error {
|
||||||
imgWidth, imgPaddingX, imgPaddingY := 800, 30, 60
|
imgWidth, imgPaddingX, imgPaddingY := 1200, 45, 90
|
||||||
titleSize, summarySize, urlSize := 42.0, 28.0, 18.0
|
accentHeight := 7.5
|
||||||
|
titleSize, summarySize, urlSize := 63.0, 42.0, 27.0
|
||||||
lineHeight := 1.5
|
lineHeight := 1.5
|
||||||
textWidth := float64(imgWidth - 2*imgPaddingX)
|
textWidth := float64(imgWidth - 2*imgPaddingX)
|
||||||
|
|
||||||
|
@ -245,7 +249,6 @@ func createImage(title, summary, url string, out io.Writer) error {
|
||||||
draw.DrawRectangle(0, 0, float64(imgWidth), float64(imgHeight))
|
draw.DrawRectangle(0, 0, float64(imgWidth), float64(imgHeight))
|
||||||
draw.Fill()
|
draw.Fill()
|
||||||
draw.SetHexColor("#3498db")
|
draw.SetHexColor("#3498db")
|
||||||
accentHeight := 5.0
|
|
||||||
draw.DrawRectangle(0, float64(imgHeight)-accentHeight, float64(imgWidth), accentHeight)
|
draw.DrawRectangle(0, float64(imgHeight)-accentHeight, float64(imgWidth), accentHeight)
|
||||||
draw.Fill()
|
draw.Fill()
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
<meta property="og:type" content="website">
|
<meta property="og:type" content="website">
|
||||||
<meta property="og:title" content="{{title}} – Prose">
|
<meta property="og:title" content="{{title}} – Prose">
|
||||||
<meta property="og:description" content="{{subtitle}}">
|
<meta property="og:description" content="{{subtitle}}">
|
||||||
<meta property="og:image" content="./about.png">
|
<meta property="og:image" content="{{path}}/about.png">
|
||||||
<meta name="twitter:creator" content="@tendstofortytwo">
|
<meta name="twitter:creator" content="@tendstofortytwo">
|
||||||
<meta name="color-scheme" content="dark light">
|
<meta name="color-scheme" content="dark light">
|
||||||
<meta name="viewport" content="width=device-width">
|
<meta name="viewport" content="width=device-width">
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue