(f dataHandlerFunc, ul UserLevelFunc)
| 705 | } |
| 706 | |
| 707 | func (h *Handler) Download(f dataHandlerFunc, ul UserLevelFunc) http.HandlerFunc { |
| 708 | return func(w http.ResponseWriter, r *http.Request) { |
| 709 | h.handleHTTPError(w, r, func() error { |
| 710 | var status int |
| 711 | start := time.Now() |
| 712 | defer func() { |
| 713 | if e := recover(); e != nil { |
| 714 | log.Error("%s: %s", e, debug.Stack()) |
| 715 | h.errors.InternalServerError.ExecuteTemplate(w, "base", pageForReq(h.app.App(), r)) |
| 716 | status = 500 |
| 717 | } |
| 718 | |
| 719 | log.Info(h.app.ReqLog(r, status, time.Since(start))) |
| 720 | }() |
| 721 | |
| 722 | data, filename, err := f(h.app.App(), w, r) |
| 723 | if err != nil { |
| 724 | if err, ok := err.(impart.HTTPError); ok { |
| 725 | status = err.Status |
| 726 | } else { |
| 727 | status = 500 |
| 728 | } |
| 729 | return err |
| 730 | } |
| 731 | |
| 732 | ext := ".json" |
| 733 | ct := "application/json" |
| 734 | if strings.HasSuffix(r.URL.Path, ".csv") { |
| 735 | ext = ".csv" |
| 736 | ct = "text/csv" |
| 737 | } else if strings.HasSuffix(r.URL.Path, ".zip") { |
| 738 | ext = ".zip" |
| 739 | ct = "application/zip" |
| 740 | } |
| 741 | w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s%s", filename, ext)) |
| 742 | w.Header().Set("Content-Type", ct) |
| 743 | w.Header().Set("Content-Length", strconv.Itoa(len(data))) |
| 744 | fmt.Fprint(w, string(data)) |
| 745 | |
| 746 | status = 200 |
| 747 | return nil |
| 748 | }()) |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | func (h *Handler) Redirect(url string, ul UserLevelFunc) http.HandlerFunc { |
| 753 | return func(w http.ResponseWriter, r *http.Request) { |
no test coverage detected