Files
pages/internal/server/social_image.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

91 lines
3.0 KiB
Go

package server
import (
"bytes"
"image"
"image/color"
"image/draw"
"image/png"
"net/http"
"sort"
"sync"
"time"
)
var socialImageOnce sync.Once
var socialImagePNG []byte
// A fixed, application-owned book/academy emblem. No document text, external
// fetches, fonts, native decoders, or per-request image-generation work.
func defaultSocialImage() []byte {
socialImageOnce.Do(func() {
im := image.NewRGBA(image.Rect(0, 0, 1200, 630))
navy, blue, pale, paper, gold := color.RGBA{18, 32, 57, 255}, color.RGBA{49, 111, 202, 255}, color.RGBA{160, 197, 242, 255}, color.RGBA{239, 245, 255, 255}, color.RGBA{223, 188, 124, 255}
rect := func(x, y, w, h int, c color.RGBA) {
draw.Draw(im, image.Rect(x, y, x+w, y+h), image.NewUniform(c), image.Point{}, draw.Src)
}
polygon := func(points []image.Point, c color.RGBA) {
for y := 0; y < 630; y++ {
var xs []int
for i, a := range points {
b := points[(i+1)%len(points)]
if (a.Y <= y && b.Y > y) || (b.Y <= y && a.Y > y) {
xs = append(xs, a.X+(y-a.Y)*(b.X-a.X)/(b.Y-a.Y))
}
}
sort.Ints(xs)
for i := 0; i+1 < len(xs); i += 2 {
rect(xs[i], y, xs[i+1]-xs[i], 1, c)
}
}
}
rect(0, 0, 1200, 630, navy)
for x := 60; x < 1200; x += 60 {
rect(x, 0, 1, 630, color.RGBA{28, 44, 69, 255})
}
for y := 45; y < 630; y += 60 {
rect(0, y, 1200, 1, color.RGBA{28, 44, 69, 255})
}
rect(60, 48, 1080, 3, gold)
rect(60, 579, 1080, 3, gold)
polygon([]image.Point{{350, 173}, {575, 203}, {600, 230}, {625, 203}, {850, 173}, {850, 482}, {625, 504}, {600, 524}, {575, 504}, {350, 482}}, blue)
polygon([]image.Point{{373, 151}, {566, 177}, {589, 202}, {589, 490}, {566, 477}, {373, 454}}, paper)
polygon([]image.Point{{611, 202}, {634, 177}, {827, 151}, {827, 454}, {634, 477}, {611, 490}}, paper)
for y := 317; y <= 421; y += 26 {
polygon([]image.Point{{401, y}, {558, y + 20}, {558, y + 25}, {401, y + 5}}, pale)
polygon([]image.Point{{642, y + 20}, {799, y}, {799, y + 5}, {642, y + 25}}, pale)
}
polygon([]image.Point{{452, 263}, {600, 129}, {748, 263}}, gold)
polygon([]image.Point{{486, 251}, {600, 149}, {714, 251}}, navy)
rect(467, 264, 266, 12, navy)
for _, x := range []int{490, 550, 630, 690} {
rect(x, 283, 18, 82, navy)
}
rect(465, 368, 270, 14, navy)
rect(450, 389, 300, 7, gold)
var out bytes.Buffer
if png.Encode(&out, im) == nil {
socialImagePNG = out.Bytes()
}
})
return socialImagePNG
}
func handleSocialImage(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.Header().Set("Allow", "GET, HEAD")
http.Error(w, "method not allowed", 405)
return
}
data := defaultSocialImage()
if len(data) == 0 {
http.Error(w, "image unavailable", 503)
return
}
w.Header().Set("Content-Type", "image/png")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Cache-Control", "public, max-age=86400")
w.Header().Set("ETag", `"social-card-v1"`)
http.ServeContent(w, r, "social-card-v1.png", time.Time{}, bytes.NewReader(data))
}