abyss/file_display.go

43 lines
747 B
Go
Raw Normal View History

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