feat: some updates

This commit is contained in:
Max Richter
2025-09-24 18:18:57 +02:00
parent b3c01bb43d
commit 26c303d9cf
32 changed files with 464 additions and 263 deletions

View File

@@ -6,6 +6,17 @@ import (
"strings"
)
var textPlainExtensions = map[string]bool{
".txt": true,
".log": true,
".json": true,
".yaml": true,
".yml": true,
".toml": true,
".xml": true,
".csv": true,
}
func ContentTypeFor(name string) string {
ext := strings.ToLower(filepath.Ext(name))
switch ext {
@@ -15,8 +26,7 @@ func ContentTypeFor(name string) string {
if ct := mime.TypeByExtension(ext); ct != "" {
return ct
}
switch ext {
case ".txt", ".log", ".json", ".yaml", ".yml", ".toml", ".xml", ".csv":
if textPlainExtensions[ext] {
return "text/plain; charset=utf-8"
}
return "application/octet-stream"

View File

@@ -12,7 +12,7 @@ func CleanURLLike(p string) string {
return "/"
}
parts := []string{}
for _, seg := range strings.Split(strings.ReplaceAll(p, "\\", "/"), "/") {
for seg := range strings.SplitSeq(strings.ReplaceAll(p, "/", "/"), "/") {
switch seg {
case "", ".":
continue
@@ -29,8 +29,8 @@ func CleanURLLike(p string) string {
func SafeRel(root, requested string) (string, error) {
s := CleanURLLike(requested)
if strings.HasPrefix(s, "/") {
s = strings.TrimPrefix(s, "/")
if after, ok := strings.CutPrefix(s, "/"); ok {
s = after
}
full := filepath.Join(root, filepath.FromSlash(s))
rel, err := filepath.Rel(root, full)