oficina / estilo Public

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

Plain git — no account needed to clone.

Download

Download this file

1// Package googleimport supplies the optional Google Drive file chooser. Tokens
2// stay in the browser; selected exports use the caller's normal upload form.
3package googleimport
4
5import (
6 "crypto/sha256"
7 "embed"
8 "errors"
9 "fmt"
10 "io/fs"
11 "regexp"
12)
13
14const MaxExportBytes = 10 << 20
15
16type Kind string
17
18const (
19 Docs Kind = "docs"
20 Sheets Kind = "sheets"
21)
22
23// Config is public browser configuration, all from the same Google project.
24// No client secret or access/refresh token belongs here.
25type Config struct{ ClientID, APIKey, AppID string }
26
27func (c Config) Enabled() bool { return c.ClientID != "" && c.APIKey != "" && c.AppID != "" }
28
29var clientID = regexp.MustCompile(`^[0-9]+-[a-zA-Z0-9_-]+\.apps\.googleusercontent\.com$`)
30var apiKey = regexp.MustCompile(`^[a-zA-Z0-9_-]{20,100}$`)
31var appID = regexp.MustCompile(`^[0-9]{1,30}$`)
32
33func (c Config) Validate() error {
34 if c == (Config{}) {
35 return nil
36 }
37 if !c.Enabled() {
38 return errors.New("googleimport: client ID, API key and project number must be configured together")
39 }
40 if len(c.ClientID) > 256 || !clientID.MatchString(c.ClientID) || !apiKey.MatchString(c.APIKey) || !appID.MatchString(c.AppID) {
41 return errors.New("googleimport: invalid public Google configuration")
42 }
43 return nil
44}
45
46// View attaches to an existing same-origin multipart form and its file input.
47// The app owns the surrounding fidelity note, upload handler and import report.
48type View struct {
49 Config Config
50 Kind Kind
51 FormID, InputID string
52 Labels Labels
53}
54
55func (v View) Enabled() bool {
56 return v.Config.Enabled() && v.Config.Validate() == nil && (v.Kind == Docs || v.Kind == Sheets)
57}
58
59//go:embed templates/*.html
60var templates embed.FS
61
62//go:embed google-import.mjs
63var javascript []byte
64
65//go:embed google-import.css
66var stylesheet []byte
67
68func Templates() fs.FS { return templates }
69func Template() string { b, _ := templates.ReadFile("templates/google-import.html"); return string(b) }
70func JS() []byte { return append([]byte(nil), javascript...) }
71
72// CSS is the chooser's stylesheet: its blocks on the spacing scale. An
73// app links it on the page that renders the chooser, beside JSName's
74// script, and drops any [estilo-google-import] rules of its own, which
75// would add their gap to these margins. Without it the chooser's
76// paragraphs keep the browser's margins. It has no hashed name: serve
77// it under whatever path and cache policy the app gives its other
78// estilo stylesheets.
79func CSS() []byte { return append([]byte(nil), stylesheet...) }
80func JSName() string {
81 h := sha256.Sum256(javascript)
82 return fmt.Sprintf("google-import.%x.mjs", h[:8])
83}
84