Files
book-comic-library/backend/internal/bookfile/bookfile.go
T

43 lines
672 B
Go

package bookfile
import (
"path/filepath"
"strings"
"unicode"
)
func FormatFromExt(name string) string {
switch strings.ToLower(filepath.Ext(name)) {
case ".cbz", ".zip":
return "cbz"
case ".pdf":
return "pdf"
case ".epub":
return "epub"
case ".txt":
return "txt"
case ".md":
return "md"
}
return ""
}
func SafeName(s string) string {
s = strings.Map(func(r rune) rune {
if unicode.IsControl(r) {
return -1
}
return r
}, s)
s = strings.ReplaceAll(s, "\\", "/")
s = filepath.Base(filepath.ToSlash(s))
if s == "." || s == "/" {
return ""
}
s = strings.TrimSpace(s)
if len(s) > 200 {
s = strings.TrimSpace(s[:200])
}
return s
}