| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "os" |
| 6 | |
| 7 | "amadan.net/oficina/estilo.git/googleimport" |
| 8 | ) |
| 9 | |
| 10 | // Google Drive import is estilo's chooser (googleimport) on the import |
| 11 | // page: it exports one Google spreadsheet as XLSX in the browser and |
| 12 | // submits it through the page's own upload form. Sheets never talks to |
| 13 | // Google and never sees a Google token; everything after the upload is |
| 14 | // the ordinary import. |
| 15 | // |
| 16 | // Off unless all three public settings are set. They are browser |
| 17 | // configuration, not secrets. |
| 18 | |
| 19 | // LoadGoogleImport reads SHEETS_GOOGLE_* and validates them together: |
| 20 | // none set is off, all three valid is on, anything else refuses boot. |
| 21 | func LoadGoogleImport() (googleimport.Config, error) { |
| 22 | cfg := googleimport.Config{ |
| 23 | ClientID: os.Getenv("SHEETS_GOOGLE_CLIENT_ID"), |
| 24 | APIKey: os.Getenv("SHEETS_GOOGLE_API_KEY"), |
| 25 | AppID: os.Getenv("SHEETS_GOOGLE_APP_ID"), |
| 26 | } |
| 27 | if err := cfg.Validate(); err != nil { |
| 28 | return googleimport.Config{}, err |
| 29 | } |
| 30 | return cfg, nil |
| 31 | } |
| 32 | |
| 33 | // googleScriptPath serves the chooser's module. Its name carries the |
| 34 | // script's hash, so it may be cached for good. |
| 35 | var googleScriptPath = "/static/google/" + googleimport.JSName() |
| 36 | |
| 37 | func serveGoogleScript(w http.ResponseWriter, _ *http.Request) { |
| 38 | w.Header().Set("Content-Type", "text/javascript; charset=utf-8") |
| 39 | w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") |
| 40 | w.Write(googleimport.JS()) |
| 41 | } |
| 42 | |
| 43 | // The import form and its file input, named for the chooser. |
| 44 | const ( |
| 45 | importFormID = "import-form" |
| 46 | importInputID = "file" |
| 47 | ) |
| 48 | |
| 49 | // googleView is the chooser's view on the import page. Its Enabled is |
| 50 | // false with Google import off, and the template renders nothing. |
| 51 | // |
| 52 | // English labels, like the rest of Sheets: estilo's catalog has twelve |
| 53 | // locales, and Sheets has no locale plumbing to choose among them. |
| 54 | func (a *App) googleView() googleimport.View { |
| 55 | return googleimport.View{ |
| 56 | Config: a.google, Kind: googleimport.Sheets, |
| 57 | FormID: importFormID, InputID: importInputID, |
| 58 | Labels: googleimport.Text("en"), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // googleHeaders extends the page policy for Google's script, frame and |
| 63 | // connection origins on the import page alone, and sets no-store. It |
| 64 | // adds no Cross-Origin-Opener-Policy: none is set anywhere, and the |
| 65 | // sign-in popup needs none. With Google import off it does nothing. |
| 66 | func (a *App) googleHeaders(w http.ResponseWriter) error { |
| 67 | if !a.google.Enabled() { |
| 68 | return nil |
| 69 | } |
| 70 | return googleimport.Headers(w, ContentSecurityPolicy) |
| 71 | } |
| 72 | |