package megauploader import ( "bytes" "encoding/json" "fmt" "html/template" "net/http" "net/url" "strings" rice "github.com/GeertJohan/go.rice" "github.com/gorilla/mux" ) //go:generate rice embed-go // loadTmpl return the required template // It is provided to centralize handling of templates; it will care about // riceboxes and other quirks that templates might need func (mu *MegaUploader) loadTmpl(tmplname string) (*template.Template, error) { str := rice.MustFindBox("res/templates").MustString(tmplname) tmpl := template.Must( template.New(strings.Split(tmplname, ".")[0]).Funcs(template.FuncMap{ "pretty": func(data interface{}) string { var out bytes.Buffer enc := json.NewEncoder(&out) enc.SetIndent("", " ") err := enc.Encode(data) if err != nil { panic(err) } return out.String() }, }).Parse(str), ) str = rice.MustFindBox("res/templates").MustString("base.html.tmpl") tmpl = template.Must(tmpl.Parse(str)) return tmpl, nil } func (mu *MegaUploader) home(w http.ResponseWriter, r *http.Request) { tmpl, err := mu.loadTmpl("index.html.tmpl") if err != nil { http.Error(w, err.Error(), 500) return } user, _ := getUser(r) shares := mu.Conf.GetAuthShares(user) w.Header().Set("Content-Type", "text/html; charset=utf-8") tmpl.Execute(w, map[string]interface{}{ "Prefix": mu.Conf.Global.RoutePrefix, "Shares": shares, }) } func (mu *MegaUploader) uploadUI(w http.ResponseWriter, r *http.Request) { user, _ := getUser(r) // user is set: checked by middleware sharename := mux.Vars(r)["share"] share, err := mu.Conf.GetAuthShare(sharename, user) if err != nil { http.Error(w, fmt.Sprintf("Share '%s' not found", sharename), http.StatusNotFound) return } tmpl, err := mu.loadTmpl("upload.html.tmpl") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } viewurl, err := mu.Conf.GetShareURL(sharename) if err != nil { viewurl = url.URL{} } w.Header().Set("Content-Type", "text/html; charset=utf-8") tmpl.Execute(w, map[string]interface{}{ "Prefix": mu.Conf.Global.RoutePrefix, "Share": share, "ViewURL": viewurl.String(), }) }