feat: some updates
This commit is contained in:
@@ -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"
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user