Files
LuPhoto/internal/site/site.go
T
lucioandClaude Opus 5.5 c87aad0254 Risk-based robot check with server-issued photo keys
The main page and direct photo links open without a prompt. Each
photo's pool is sealed with its own key, handed out by POST /k
(served by the daemon in serve and export mode). The hold-and-solve
card appears only for low-entropy clicks into a collection, too many
low-entropy landings from one IP, or more than photo_limit photos per
window, with escalating proof-of-work and a 429 cap. Build ids now
sort chronologically within the same second.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-04 22:45:50 -08:00

274 lines
6.4 KiB
Go

package site
import (
"embed"
"fmt"
"html/template"
"io"
"net/url"
"unicode"
"github.com/luciochen233/luphoto/internal/process"
)
//go:embed templates/*.html
var templateFiles embed.FS
var templates = template.Must(template.New("site").Funcs(template.FuncMap{"lang": Lang}).ParseFS(templateFiles, "templates/*.html"))
func Lang(text string) string {
for _, r := range text {
if unicode.Is(unicode.Han, r) {
return "zh"
}
}
return ""
}
const (
SiteName = "Photos · Lucio"
SiteDescription = "Photographs by Guoliang Chen · 陈国梁 · Lucio."
)
type Tiles struct {
Format string `json:"format"`
Path string `json:"path"`
Scrambled bool `json:"scrambled,omitempty"`
Cell int `json:"cell,omitempty"`
}
type Level struct {
Width int `json:"width"`
Height int `json:"height"`
TileSize int `json:"tile_size"`
Overlap int `json:"overlap,omitempty"`
Cols int `json:"cols"`
Rows int `json:"rows"`
}
type Pyramid struct {
Hash string `json:"hash"`
Width int `json:"width"`
Height int `json:"height"`
Levels []Level `json:"levels"`
}
func Layout(p *process.Pyramid) *Pyramid {
out := &Pyramid{Hash: p.Hash, Width: p.Width, Height: p.Height, Levels: make([]Level, len(p.Levels))}
for i, l := range p.Levels {
out.Levels[i] = Level{Width: l.Width, Height: l.Height, TileSize: l.TileSize, Overlap: l.Overlap, Cols: l.Cols, Rows: l.Rows}
}
return out
}
const (
GateServer = "server"
GateOff = "off"
KeyPath = "/k"
)
type Gate struct {
Build string `json:"build"`
Mode string `json:"mode"`
Cover string `json:"cover"`
MinEntropy int `json:"min_entropy"`
Keys map[string]string `json:"keys,omitempty"`
}
type Pool struct {
Levels []string `json:"levels"`
Sealed string `json:"sealed"`
}
type SealedLevel struct {
Map []int32 `json:"map"`
Bytes []int64 `json:"bytes"`
}
type Sealed struct {
Seed string `json:"seed"`
Scramble string `json:"scramble,omitempty"`
Levels []SealedLevel `json:"levels"`
}
type Photo struct {
Pyramid
Title string `json:"title,omitempty"`
Caption string `json:"caption,omitempty"`
Alt string `json:"alt,omitempty"`
Slides int `json:"slides"`
EXIF *process.EXIF `json:"exif,omitempty"`
}
type Manifest struct {
Build string `json:"build"`
Slug string `json:"slug"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
Date string `json:"date,omitempty"`
Order *int `json:"order,omitempty"`
Hidden bool `json:"hidden,omitempty"`
CollapsedFocusY float64 `json:"collapsed_focus_y"`
ShowEXIF bool `json:"show_exif"`
Tiles Tiles `json:"tiles"`
Gate *Gate `json:"gate,omitempty"`
Cover *Pyramid `json:"cover"`
OG string `json:"og,omitempty"`
Photos []Photo `json:"photos"`
}
type IndexAlbum struct {
Slug string `json:"slug"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
Date string `json:"date,omitempty"`
CollapsedFocusY float64 `json:"collapsed_focus_y"`
Photos int `json:"photos"`
Manifest string `json:"manifest"`
Cover *Pyramid `json:"cover"`
OG string `json:"og,omitempty"`
}
type Index struct {
Build string `json:"build"`
Tiles Tiles `json:"tiles"`
Gate *Gate `json:"gate,omitempty"`
Albums []IndexAlbum `json:"albums"`
}
func IndexURL(build string) string {
return "/index." + build + ".json"
}
func AlbumPath(slug string) string {
return "/a/" + url.PathEscape(slug) + "/"
}
func ManifestURL(build, slug string) string {
return AlbumPath(slug) + "manifest." + build + ".json"
}
func ManifestName(build string) string {
return "manifest." + build + ".json"
}
func IndexName(build string) string {
return "index." + build + ".json"
}
const PoolDir = "p"
func PoolName(build, hash string) string {
return hash + "." + build + ".json"
}
const Robots = "User-agent: *\nDisallow: /t/\nDisallow: /a/*/p/\n"
func OGPath(hash string) string {
if hash == "" {
return ""
}
return "/og/" + hash + ".jpg"
}
type Page struct {
Assets Assets
BudgetMS int
MaxDPR float64
Concurrency int
BlockContextMenu bool
Gate string
MinEntropy int
BaseURL string
}
type head struct {
Page
Build string
Title string
Description string
Path string
Image string
IndexURL string
}
func (h head) Absolute(path string) string {
if path == "" {
return ""
}
return h.BaseURL + path
}
type indexPage struct {
head
Albums []IndexAlbum
}
type albumPage struct {
head
AlbumTitle string
Slug string
Subtitle string
Date string
Photos int
ManifestURL string
}
func RenderIndex(w io.Writer, index *Index, page Page) error {
image := ""
if len(index.Albums) > 0 {
image = OGPath(index.Albums[0].OG)
}
return templates.ExecuteTemplate(w, "index.html", indexPage{
head: head{
Page: page,
Build: index.Build,
Title: SiteName,
Description: SiteDescription,
Path: "/",
Image: image,
IndexURL: IndexURL(index.Build),
},
Albums: index.Albums,
})
}
func RenderAlbum(w io.Writer, m *Manifest, page Page) error {
description := m.Subtitle
if description == "" {
description = fmt.Sprintf("%d photographs by Lucio", len(m.Photos))
if m.Date != "" {
description += ", " + m.Date
}
description += "."
}
return templates.ExecuteTemplate(w, "album.html", albumPage{
head: head{
Page: page,
Build: m.Build,
Title: m.Title + " · Lucio",
Description: description,
Path: AlbumPath(m.Slug),
Image: OGPath(m.OG),
IndexURL: IndexURL(m.Build),
},
AlbumTitle: m.Title,
Slug: m.Slug,
Subtitle: m.Subtitle,
Date: m.Date,
Photos: len(m.Photos),
ManifestURL: ManifestURL(m.Build, m.Slug),
})
}
func RenderNotFound(w io.Writer, build string, page Page) error {
return templates.ExecuteTemplate(w, "404.html", head{
Page: page,
Build: build,
Title: "Not found · Lucio",
Description: SiteDescription,
Path: "/404.html",
IndexURL: IndexURL(build),
})
}