oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "io"
8 "regexp"
9 "sync"
10)
11
12// fontstore.go is where a deck's uploaded font files are read from:
13// the object store (Config.Blobs), and nowhere else — never a
14// repository, a bundle, or a cache that outlives one render.
15
16// maxFontFileBytes is the most one uploaded font file may be: 10 MiB,
17// the upload limit. A stored object bigger than that is not read.
18const maxFontFileBytes = 10 << 20
19
20// fontRefPattern is a face's ref as meta.fontFiles carries it: the
21// file's SHA-256 in lowercase hex.
22var fontRefPattern = regexp.MustCompile(`^[0-9a-f]{64}$`)
23
24// errNoFontStore is every font read on a deployment with no object
25// store; the text falls back to the rest of its stack.
26var errNoFontStore = errors.New("slides: no object store to read a font from")
27
28// fontObjectKey is the object-store key an uploaded font file lives at:
29// its ref itself. The store is content-addressed — Put keeps bytes under
30// their own SHA-256 and Get takes nothing else — and a face's ref is
31// that SHA-256, so an upload stored with Put is found by its ref. One
32// function all the same, so storing and reading cannot disagree.
33func fontObjectKey(ref string) string { return ref }
34
35// fontLoader reads uploaded font files for one render (a thumbnail, an
36// export): layout.Options.Fonts and ExportOptions.Fonts. It reads, and
37// keeps nothing; the render parses each face once and drops it after.
38//
39// It reads only a ref the deck recorded (a DeckFont row for deckID,
40// deckfonts.go): meta.fontFiles is a list any member or agent can write
41// with any ref, so the list alone is never a way to read a file. The
42// deck's rows are read once, on the first face asked for.
43func (a *app) fontLoader(ctx context.Context, deckID int64) func(ref string) ([]byte, error) {
44 store := a.cfg.Blobs
45 var (
46 once sync.Once
47 recorded map[string]bool
48 readErr error
49 )
50 return func(ref string) ([]byte, error) {
51 if store == nil {
52 return nil, errNoFontStore
53 }
54 if !fontRefPattern.MatchString(ref) {
55 return nil, fmt.Errorf("slides: %q is not a font ref", ref)
56 }
57 once.Do(func() { recorded, readErr = a.deckFontRefs(ctx, deckID) })
58 if readErr != nil {
59 return nil, readErr
60 }
61 if !recorded[ref] {
62 return nil, fmt.Errorf("slides: font %s is not this deck's", ref)
63 }
64 rc, err := store.Get(ctx, fontObjectKey(ref))
65 if err != nil {
66 return nil, err
67 }
68 defer rc.Close()
69 raw, err := io.ReadAll(io.LimitReader(rc, maxFontFileBytes+1))
70 if err != nil {
71 return nil, err
72 }
73 if len(raw) > maxFontFileBytes {
74 return nil, fmt.Errorf("slides: font %s is over %d bytes", ref, maxFontFileBytes)
75 }
76 return raw, nil
77 }
78}
79