| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | |
| 6 | "amadan.net/oficina/estilo.git/googleimport" |
| 7 | |
| 8 | "github.com/go-chi/chi/v5" |
| 9 | ) |
| 10 | |
| 11 | // importRoutes mounts import: the page, the upload, and the report. |
| 12 | // |
| 13 | // The page is static before the {sheetID} pattern. chi prefers an |
| 14 | // exact segment over a wildcard whatever the order, so this is belt |
| 15 | // and braces rather than load-bearing — but a route that only works |
| 16 | // because of resolution order is a route one refactor away from |
| 17 | // parsing "import" as a sheet id. TestImportPathIsNotASheetID pins it. |
| 18 | // |
| 19 | // The upload: a whole workbook is too much work for a request, so it |
| 20 | // 303s to a job (spec §11) rather than holding the connection open — |
| 21 | // and the drop zone and the Google chooser both submit exactly this |
| 22 | // form, because each is an enhancement over the form, not a second way |
| 23 | // in. |
| 24 | func (a *App) importRoutes(r chi.Router) { |
| 25 | r.Get("/sheets/import", a.handleImportPage) |
| 26 | r.Post("/sheets/import", a.handleSheetImport) |
| 27 | r.Get("/sheets/{sheetID}/import-report", a.handleImportReport) |
| 28 | } |
| 29 | |
| 30 | // The import page. |
| 31 | // |
| 32 | // Separate from handlers_import.go, which holds the POST that does the |
| 33 | // work, for the dullest possible reason: together they were 305 lines |
| 34 | // and the file cap is 300 (AGENTS.md / Mechanical rules, enforced by |
| 35 | // TestFileLineCap). The split falls in a sensible place anyway — this |
| 36 | // file is the screen, that one is the upload. |
| 37 | |
| 38 | // importPageView is the import page's payload. |
| 39 | type importPageView struct { |
| 40 | // Error is a refusal to show above the form — one of the refusal |
| 41 | // constants in handlers_import.go, never a wrapped error, because |
| 42 | // everything shown here is read by somebody deciding what to do |
| 43 | // differently. |
| 44 | Error string |
| 45 | // Google is the Drive chooser; it renders nothing when off. |
| 46 | Google googleimport.View |
| 47 | // GoogleScript is the chooser's module path, set only when on. |
| 48 | GoogleScript string |
| 49 | } |
| 50 | |
| 51 | // handleImportPage draws the import form on a page of its own. |
| 52 | // |
| 53 | // Import used to be a second form on the sheet list, below the form |
| 54 | // that made a blank sheet. Both came off that page: a list of sheets |
| 55 | // should be a list of sheets, and the verbs moved into the chrome |
| 56 | // around it. |
| 57 | // |
| 58 | // A page rather than an item inside the File menu, which is where this |
| 59 | // nearly went. Three reasons, and the third is the one that decided it: |
| 60 | // a dropdown is a poor container for a file picker plus three limits; a |
| 61 | // hidden input behind a menu-shaped label never submits itself, so that |
| 62 | // menu item would have needed script to import anything at all, and |
| 63 | // import is meant to work with scripting off; and the File menu exists |
| 64 | // only on a sheet page, so somebody with no sheets yet — exactly the |
| 65 | // person most likely to be importing one — would have had no way in. |
| 66 | func (a *App) handleImportPage(w http.ResponseWriter, r *http.Request) { |
| 67 | // Behind requireSession already; this is the same belt-and-braces |
| 68 | // actor check the sibling handlers make, and it is what makes the |
| 69 | // handler safe to call from a test that does not mount the group. |
| 70 | if _, ok := a.actor(r); !ok { |
| 71 | a.forbidden(w, r) |
| 72 | return |
| 73 | } |
| 74 | a.renderImportPage(w, r, http.StatusOK, "") |
| 75 | } |
| 76 | |
| 77 | // renderImportPage draws that form, optionally carrying a refusal and |
| 78 | // the status that goes with it. |
| 79 | // |
| 80 | // A refused import re-renders this page rather than redirecting to it, |
| 81 | // so the refusal survives without a flash: the person lands back on the |
| 82 | // form they posted from, with the reason above it. No session storage, |
| 83 | // no cookie, no redirect, and it works with scripting off by |
| 84 | // construction. That reasoning arrived here from renderSheetList along |
| 85 | // with the form itself. |
| 86 | func (a *App) renderImportPage(w http.ResponseWriter, r *http.Request, status int, refusal string) { |
| 87 | // The refused POST's re-render too: it is the same page, and the |
| 88 | // person retries from it. |
| 89 | if err := a.googleHeaders(w); err != nil { |
| 90 | a.fail(w, r, err) |
| 91 | return |
| 92 | } |
| 93 | view := importPageView{Error: refusal, Google: a.googleView()} |
| 94 | if view.Google.Enabled() { |
| 95 | view.GoogleScript = googleScriptPath |
| 96 | } |
| 97 | a.render(w, r, "import", titled("Import a spreadsheet"), status, view) |
| 98 | } |
| 99 | |