incredible-go-core/pkg/io/files/file_name_utils.go

63 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package files
import (
"path/filepath"
"slices"
"strings"
"git.tswf.io/incredible-go/incredible-go-core/pkg/collections/array_list"
)
var RestrictedFilepathCharactersReplacements = map[string]string{
"<": "",
">": "",
":": "",
"\"": "",
"/": "",
"\\": "",
"|": "",
"?": "",
"*": "",
}
func GetSimpleName(filePath string) string {
return SplitFileName(filePath).GetLast()
}
func GetExtension(filePath string) string {
split := strings.Split(filePath, ".")
if len(split) == 1 {
return ""
}
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
}
return filepath.Join(split[:len(split)-1]...)
}
func SplitFileName(filePath string) *array_list.ArrayList[string] {
filePath = filepath.ToSlash(filePath)
split := strings.Split(filePath, "/")
return array_list.NewArrayListWithContent[string](split...)
}
func ReplaceAllRestrictedCharacters(filePath string) string {
for k, v := range RestrictedFilepathCharactersReplacements {
filePath = strings.ReplaceAll(filePath, k, v)
}
return filePath
}