2024-10-16 14:05:49 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
2024-10-16 20:42:59 -03:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-10-16 14:05:49 -03:00
|
|
|
)
|
|
|
|
|
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 14:05:49 -03:00
|
|
|
|
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 14:05:49 -03:00
|
|
|
|
2024-10-16 20:42:59 -03:00
|
|
|
fileInfo := FileInfo{
|
|
|
|
Name: file,
|
2024-10-16 14:05:49 -03:00
|
|
|
Path: app.url,
|
2024-10-16 20:42:59 -03:00
|
|
|
Type: getType(file),
|
2024-10-16 14:05:49 -03:00
|
|
|
}
|
|
|
|
|
2024-10-16 20:42:59 -03:00
|
|
|
if err := tmpl.Execute(w, fileInfo); err != nil {
|
2024-10-16 14:05:49 -03:00
|
|
|
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"
|
|
|
|
}
|