2024-06-21 18:06:09 +02:00
|
|
|
|
package files
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"slices"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"git.tswf.io/incredible-go/incredible-go-core/pkg/collections/array_list"
|
|
|
|
|
)
|
|
|
|
|
|
2024-06-25 12:24:58 +02:00
|
|
|
|
var RestrictedFilepathCharactersReplacements = map[string]string{
|
|
|
|
|
"<": "<",
|
|
|
|
|
">": ">",
|
|
|
|
|
":": ":",
|
|
|
|
|
"\"": """,
|
|
|
|
|
"/": "/",
|
|
|
|
|
"\\": "\",
|
|
|
|
|
"|": "|",
|
|
|
|
|
"?": "?",
|
|
|
|
|
"*": "*",
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 18:06:09 +02:00
|
|
|
|
func GetSimpleName(filePath string) string {
|
|
|
|
|
return SplitFileName(filePath).GetLast()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetExtension(filePath string) string {
|
|
|
|
|
split := strings.Split(filePath, ".")
|
2024-06-25 14:52:10 +02:00
|
|
|
|
if len(split) == 1 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 18:06:09 +02:00
|
|
|
|
return split[len(split)-1]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetSimpleNameWithoutExtension(filePath string) string {
|
|
|
|
|
simpleName := GetSimpleName(filePath)
|
|
|
|
|
split := slices.DeleteFunc(strings.Split(simpleName, "."), func(s string) bool {
|
|
|
|
|
return s == ""
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if len(split) == 1 {
|
|
|
|
|
return filePath
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-28 18:59:50 +02:00
|
|
|
|
return strings.Join(split[:len(split)-1], "")
|
2024-06-21 18:06:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SplitFileName(filePath string) *array_list.ArrayList[string] {
|
|
|
|
|
filePath = filepath.ToSlash(filePath)
|
|
|
|
|
split := strings.Split(filePath, "/")
|
|
|
|
|
|
|
|
|
|
return array_list.NewArrayListWithContent[string](split...)
|
|
|
|
|
}
|
2024-06-25 12:24:58 +02:00
|
|
|
|
|
|
|
|
|
func ReplaceAllRestrictedCharacters(filePath string) string {
|
|
|
|
|
for k, v := range RestrictedFilepathCharactersReplacements {
|
|
|
|
|
filePath = strings.ReplaceAll(filePath, k, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filePath
|
|
|
|
|
}
|
2024-06-29 23:51:26 +02:00
|
|
|
|
|
|
|
|
|
func TryAbs(filePath string) string {
|
|
|
|
|
if !filepath.IsAbs(filePath) {
|
|
|
|
|
abs, err := filepath.Abs(filePath)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return abs
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return filePath
|
|
|
|
|
}
|