wip: add handling for extensions

This commit is contained in:
jabuxas 2024-10-16 20:42:59 -03:00
parent 86314aa45a
commit 44902c63b1

View File

@ -4,20 +4,39 @@ import (
"html/template" "html/template"
"log/slog" "log/slog"
"net/http" "net/http"
"path/filepath"
"strings"
) )
func (app *Application) displayFile(w http.ResponseWriter, r *http.Request) { var extensions = map[string]string{
tmpl := template.Must(template.ParseFiles("templates/files.html")) ".sh": "text",
".mp4": "video",
fileName := "19ad500a.pdf" ".pdf": "pdf",
".txt": "text",
file := FileInfo{ ".png": "image",
Name: fileName, ".jpg": "image",
Path: app.url, ".json": "text",
Type: "pdf",
} }
if err := tmpl.Execute(w, file); err != nil { func displayFile(app *Application, file string, w http.ResponseWriter) {
tmpl := template.Must(template.ParseFiles("templates/files.html"))
fileInfo := FileInfo{
Name: file,
Path: app.url,
Type: getType(file),
}
if err := tmpl.Execute(w, fileInfo); err != nil {
slog.Warn(err.Error()) slog.Warn(err.Error())
} }
} }
func getType(file string) string {
extension := strings.ToLower(filepath.Ext(file))
if fileType, exists := extensions[extension]; exists {
return fileType
}
return "text"
}