oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1// Package brand serves the suite's app icons from Go, so a page can link
2// any sibling app's icon (the app switcher) without vendoring the file.
3// The artwork itself is brand/svg/, generated by generate.mjs; see README.md.
4package brand
5
6import (
7 "embed"
8 "net/http"
9 "strings"
10)
11
12//go:embed svg/*.svg
13var svgFS embed.FS
14
15// Prefix is the URL path Handler serves under.
16const Prefix = "/static/estilo/brand/"
17
18// files maps a public slug to its master. "meet" is Calls' name before
19// the rename, kept so a page still linking it gets the right icon.
20var files = map[string]string{
21 "oficina": "oficina.svg",
22 "docs": "docs.svg",
23 "sheets": "sheets.svg",
24 "slides": "slides.svg",
25 "calendar": "calendar.svg",
26 "calls": "calls.svg",
27 "meet": "calls.svg",
28 "memoria": "memoria.svg",
29 "equipo": "equipo.svg",
30 "fichas": "fichas.svg",
31 "mapa": "mapa.svg",
32 "mail": "mail.svg",
33 "files": "files.svg",
34 "contra": "contra.svg",
35}
36
37// SVG returns the icon for slug, or nil for an unknown slug.
38func SVG(slug string) []byte {
39 name, ok := files[slug]
40 if !ok {
41 return nil
42 }
43 body, err := svgFS.ReadFile("svg/" + name)
44 if err != nil {
45 return nil
46 }
47 return body
48}
49
50// Path is the URL Handler serves slug's icon at.
51func Path(slug string) string { return Prefix + slug + ".svg" }
52
53// Handler serves GET and HEAD for Prefix + "<slug>.svg". Mount it at
54// Prefix. Anything else is 404; the icons are public artwork and need no
55// session.
56func Handler() http.Handler {
57 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
58 slug, ok := strings.CutSuffix(strings.TrimPrefix(r.URL.Path, Prefix), ".svg")
59 body := SVG(slug)
60 if !ok || body == nil || !strings.HasPrefix(r.URL.Path, Prefix) {
61 http.NotFound(w, r)
62 return
63 }
64 if r.Method != http.MethodGet && r.Method != http.MethodHead {
65 w.Header().Set("Allow", "GET, HEAD")
66 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
67 return
68 }
69 w.Header().Set("Content-Type", "image/svg+xml")
70 w.Header().Set("Cache-Control", "public, max-age=86400")
71 w.Header().Set("X-Content-Type-Options", "nosniff")
72 if r.Method == http.MethodHead {
73 return
74 }
75 _, _ = w.Write(body)
76 })
77}
78