2024-10-16 14:05:49 -03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
2024-10-16 22:02:08 -03:00
|
|
|
"os"
|
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{
|
2024-10-16 21:38:20 -03:00
|
|
|
".mp4": "video", ".mkv": "video", ".webm": "video",
|
|
|
|
|
|
|
|
".pdf": "pdf",
|
|
|
|
|
|
|
|
".png": "image", ".jpg": "image", ".jpeg": "image", ".webp": "image",
|
|
|
|
|
|
|
|
".sh": "text", ".bash": "text", ".zsh": "text",
|
|
|
|
".bat": "text", ".cmd": "text", ".ps1": "text",
|
|
|
|
".ini": "text", ".cfg": "text", ".conf": "text",
|
|
|
|
".toml": "text", ".yml": "text", ".yaml": "text",
|
|
|
|
".c": "text", ".cpp": "text", ".h": "text",
|
|
|
|
".go": "text", ".py": "text", ".js": "text",
|
|
|
|
".ts": "text", ".html": "text", ".htm": "text",
|
|
|
|
".xml": "text", ".css": "text", ".java": "text",
|
|
|
|
".rs": "text", ".rb": "text", ".php": "text",
|
|
|
|
".pl": "text", ".sql": "text", ".md": "text",
|
|
|
|
".log": "text", ".txt": "text", ".csv": "text",
|
|
|
|
".json": "text", ".env": "text", ".sum": "text",
|
|
|
|
".gitignore": "text", ".dockerfile": "text", ".Makefile": "text",
|
|
|
|
".rst": "text",
|
2024-10-16 20:42:59 -03:00
|
|
|
}
|
2024-10-16 14:05:49 -03:00
|
|
|
|
2024-10-16 21:38:20 -03:00
|
|
|
func DisplayFile(app *Application, file string, w http.ResponseWriter) {
|
2024-10-16 20:42:59 -03:00
|
|
|
tmpl := template.Must(template.ParseFiles("templates/files.html"))
|
2024-10-16 14:05:49 -03:00
|
|
|
|
2024-10-16 22:17:17 -03:00
|
|
|
fileStat, _ := os.Stat("." + file)
|
2024-10-16 22:02:08 -03:00
|
|
|
fileContent, _ := os.ReadFile("." + file)
|
|
|
|
|
2024-10-16 20:42:59 -03:00
|
|
|
fileInfo := FileInfo{
|
2024-10-16 22:02:08 -03:00
|
|
|
Name: file,
|
|
|
|
Path: filepath.Join(app.url, file),
|
|
|
|
Type: getType(file),
|
|
|
|
Content: string(fileContent),
|
2024-10-16 22:17:17 -03:00
|
|
|
TimeUploaded: fileStat.ModTime().
|
|
|
|
UTC().
|
|
|
|
Format("2006-01-02 15:04:05 UTC"),
|
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"
|
|
|
|
}
|