diff --git a/file_display.go b/file_display.go index 0e17a91..f3f5316 100644 --- a/file_display.go +++ b/file_display.go @@ -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" +}