From b250091a45c9b9bd458506aeaaac43e4b36a0f27 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Wed, 16 Oct 2024 14:05:49 -0300 Subject: [PATCH] wip: add custom template for displaying files --- abyss.go | 2 + file_display.go | 23 +++++++++ handlers.go | 1 + templates/files.html | 110 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 file_display.go create mode 100644 templates/files.html diff --git a/abyss.go b/abyss.go index 20dac63..9480508 100644 --- a/abyss.go +++ b/abyss.go @@ -93,6 +93,8 @@ func setupHandlers(mux *http.ServeMux, app *Application) { mux.HandleFunc("/last", app.lastUploadedHandler) + mux.HandleFunc("/test", app.displayFile) + if app.authText == "yes" { mux.HandleFunc("/upload", app.basicAuth(app.uploadHandler)) slog.Warn("text uploading through the browser will be restricted") diff --git a/file_display.go b/file_display.go new file mode 100644 index 0000000..0e17a91 --- /dev/null +++ b/file_display.go @@ -0,0 +1,23 @@ +package main + +import ( + "html/template" + "log/slog" + "net/http" +) + +func (app *Application) displayFile(w http.ResponseWriter, r *http.Request) { + tmpl := template.Must(template.ParseFiles("templates/files.html")) + + fileName := "19ad500a.pdf" + + file := FileInfo{ + Name: fileName, + Path: app.url, + Type: "pdf", + } + + if err := tmpl.Execute(w, file); err != nil { + slog.Warn(err.Error()) + } +} diff --git a/handlers.go b/handlers.go index 4bae77c..59f8331 100644 --- a/handlers.go +++ b/handlers.go @@ -31,6 +31,7 @@ type FileInfo struct { Path string Size int64 FormattedSize string + Type string } type TemplateData struct { diff --git a/templates/files.html b/templates/files.html new file mode 100644 index 0000000..a8a96de --- /dev/null +++ b/templates/files.html @@ -0,0 +1,110 @@ + + + + + + + {{.Name}} + + + + +
+ {{.Path}} {{if eq .Type "pdf"}} + Download PDF + {{end}} +
+
+ {{if eq .Type "image"}} + Image + {{else if eq .Type "pdf"}} + + {{else if eq .Type "video"}} + + {{else if eq .Type "text"}} +
{{.Content}}
+ {{end}} +
+ + +