From e529bfa35366a41256ff02a53aa623dba53d5ec2 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 8 Feb 2022 04:23:38 +0400 Subject: [PATCH] Amber is replaced with Jade. --- internal/handlers/handlers.go | 52 ++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 059ef41..bd8bc84 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -16,10 +16,10 @@ import ( "path" "time" - "github.com/eknkc/amber" + "github.com/Joker/jade" ) -var defaultAmberOptions = amber.Options{PrettyPrint: false, LineNumbers: false} +//var defaultAmberOptions = amber.Options{PrettyPrint: false, LineNumbers: false} var compiledTemplates map[string]*template.Template @@ -54,8 +54,7 @@ type UploadHandlers struct { } func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger, uploadDirSize *int64) *UploadHandlers { - compiledTemplates = amber.MustCompileDir(conf.WebDir+"/templates", - amber.DefaultDirOptions, defaultAmberOptions) + compileTemplates(path.Join(conf.WebDir, "templates"), lErr) return &UploadHandlers{ conf: conf, @@ -160,6 +159,11 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusFound) } + scheme := r.Header.Get("X-Forwarded-Proto") + if scheme == "" { + scheme = "http" + } + downloadURL := path.Join("/f", fSaltedHash, fHandler.Filename) if err := compiledTemplates["uploaded"].Execute(w, &UploadedData{ @@ -208,3 +212,43 @@ func (h *UploadHandlers) NotFound(w http.ResponseWriter, r *http.Request) { h.logErr.Fatalln("failed to execute 404 template:", err) } } + +func compileTemplates(dir string, lErr *logging.Logger) { + compiledTemplates = make(map[string]*template.Template) + + indexStr, err := jade.ParseFile(path.Join(dir, "index.jade")) + if err != nil { + lErr.Fatalln(err) + } + + indexTpl, err := template.New("index").Parse(indexStr) + if err != nil { + lErr.Fatalln(err) + } + + compiledTemplates["index"] = indexTpl + + uploadedStr, err := jade.ParseFile(path.Join(dir, "uploaded.jade")) + if err != nil { + lErr.Fatalln(err) + } + + uploadedTpl, err := template.New("uploaded").Parse(uploadedStr) + if err != nil { + lErr.Fatalln(err) + } + + compiledTemplates["uploaded"] = uploadedTpl + + notfoundStr, err := jade.ParseFile(path.Join(dir, "404.jade")) + if err != nil { + lErr.Fatalln(err) + } + + notfoundTpl, err := template.New("404").Parse(notfoundStr) + if err != nil { + lErr.Fatalln(err) + } + + compiledTemplates["404"] = notfoundTpl +}