wip: add handling for extensions
This commit is contained in:
parent
c64c63d333
commit
ff566a2ff5
@ -4,20 +4,39 @@ import (
|
||||
"html/template"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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",
|
||||
var extensions = map[string]string{
|
||||
".sh": "text",
|
||||
".mp4": "video",
|
||||
".pdf": "pdf",
|
||||
".txt": "text",
|
||||
".png": "image",
|
||||
".jpg": "image",
|
||||
".json": "text",
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
func getType(file string) string {
|
||||
extension := strings.ToLower(filepath.Ext(file))
|
||||
|
||||
if fileType, exists := extensions[extension]; exists {
|
||||
return fileType
|
||||
}
|
||||
return "text"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user