| 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. |
| 3 | package googleimport |
| 4 | |
| 5 | import ( |
| 6 | "crypto/sha256" |
| 7 | "embed" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "io/fs" |
| 11 | "regexp" |
| 12 | ) |
| 13 | |
| 14 | const MaxExportBytes = 10 << 20 |
| 15 | |
| 16 | type Kind string |
| 17 | |
| 18 | const ( |
| 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. |
| 25 | type Config struct{ ClientID, APIKey, AppID string } |
| 26 | |
| 27 | func (c Config) Enabled() bool { return c.ClientID != "" && c.APIKey != "" && c.AppID != "" } |
| 28 | |
| 29 | var clientID = regexp.MustCompile(`^[0-9]+-[a-zA-Z0-9_-]+\.apps\.googleusercontent\.com$`) |
| 30 | var apiKey = regexp.MustCompile(`^[a-zA-Z0-9_-]{20,100}$`) |
| 31 | var appID = regexp.MustCompile(`^[0-9]{1,30}$`) |
| 32 | |
| 33 | func (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. |
| 48 | type View struct { |
| 49 | Config Config |
| 50 | Kind Kind |
| 51 | FormID, InputID string |
| 52 | Labels Labels |
| 53 | } |
| 54 | |
| 55 | func (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 |
| 60 | var templates embed.FS |
| 61 | |
| 62 | //go:embed google-import.mjs |
| 63 | var javascript []byte |
| 64 | |
| 65 | //go:embed google-import.css |
| 66 | var stylesheet []byte |
| 67 | |
| 68 | func Templates() fs.FS { return templates } |
| 69 | func Template() string { b, _ := templates.ReadFile("templates/google-import.html"); return string(b) } |
| 70 | func 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. |
| 79 | func CSS() []byte { return append([]byte(nil), stylesheet...) } |
| 80 | func JSName() string { |
| 81 | h := sha256.Sum256(javascript) |
| 82 | return fmt.Sprintf("google-import.%x.mjs", h[:8]) |
| 83 | } |
| 84 | |