wip: add custom template for displaying files

This commit is contained in:
jabuxas 2024-10-16 14:05:49 -03:00
parent 52fa208aab
commit ebe2c461f5
4 changed files with 136 additions and 0 deletions

View File

@ -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")

23
file_display.go Normal file
View File

@ -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())
}
}

View File

@ -31,6 +31,7 @@ type FileInfo struct {
Path string
Size int64
FormattedSize string
Type string
}
type TemplateData struct {

110
templates/files.html Normal file
View File

@ -0,0 +1,110 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{.Name}}</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #1e1e1e;
color: #fff;
font-family: "Arial", sans-serif;
display: flex;
flex-direction: column;
height: 100vh;
}
header {
background-color: #2e2e2e;
text-align: center;
font-size: 1rem;
font-weight: bold;
position: sticky;
top: 0;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-evenly;
}
.content {
flex-grow: 1;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
img,
video,
embed,
iframe {
max-width: 100%;
max-height: 100%;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
video {
background-color: #000;
}
.pdf-embed {
width: 100%;
height: 100%;
border: none;
}
.btn-download {
display: inline-block;
padding: 10px 20px;
background-color: #36f9f6;
color: #000;
border-radius: 5px;
text-decoration: none;
font-size: 1rem;
transition: background-color 0.3s ease;
}
.btn-download:hover {
background-color: #2bd3d0;
}
footer {
text-align: center;
padding: 10px;
background-color: #2e2e2e;
font-size: 0.8rem;
color: #aaa;
border-top: 1px solid #36f9f6;
}
</style>
</head>
<body>
<header>
{{.Path}} {{if eq .Type "pdf"}}
<a href="/{{.Name}}" class="btn-download" download>Download PDF</a>
{{end}}
</header>
<div class="content">
{{if eq .Type "image"}}
<img src="/{{.Name}}" alt="Image" />
{{else if eq .Type "pdf"}}
<embed src="/{{.Name}}" type="application/pdf" class="pdf-embed" />
{{else if eq .Type "video"}}
<video controls>
<source src="/{{.Name}}" type="video/mp4" />
Your browser does not support the video tag.
</video>
{{else if eq .Type "text"}}
<div class="text-content">{{.Content}}</div>
{{end}}
</div>
</body>
</html>