wip: add handling for extensions

This commit is contained in:
jabuxas 2024-10-16 20:42:59 -03:00
parent c64c63d333
commit ff566a2ff5

View File

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