wip: add handling for extensions
This commit is contained in:
parent
c64c63d333
commit
ff566a2ff5
@ -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{
|
||||||
|
".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"))
|
tmpl := template.Must(template.ParseFiles("templates/files.html"))
|
||||||
|
|
||||||
fileName := "19ad500a.pdf"
|
fileInfo := FileInfo{
|
||||||
|
Name: file,
|
||||||
file := FileInfo{
|
|
||||||
Name: fileName,
|
|
||||||
Path: app.url,
|
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())
|
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