Skip to content

Commit

Permalink
fix filename correction
Browse files Browse the repository at this point in the history
  • Loading branch information
KusoKaihatsuSha committed Feb 7, 2024
1 parent c81a9f0 commit fe28fc9
Showing 1 changed file with 53 additions and 68 deletions.
121 changes: 53 additions & 68 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -48,21 +47,6 @@ func init() {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

// ToLog splits notifications into Error and Debug(text).
// func ToLog(err any) {
// if err == nil || err == "" {
// return
// }
// switch val := err.(type) {
// case error:
// if val.Error() != "" {
// log.Error().Msgf("%v", val)
// }
// default:
// log.Debug().Msgf("%v", val)
// }
// }

// ToLog splits notifications into Error and Debug(text).
func ToLog(err any, text ...string) {
if err == nil || err == "" {
Expand Down Expand Up @@ -123,28 +107,18 @@ func logger(typ int) zerolog.Logger {
}

// DeleteFile delete file in temp folder. Actually means delete any file by path
func DeleteFile(path string) {
ToLog(os.RemoveAll(path))
func DeleteFile(filename string) {
filename = separatorCorrect(filename)
ToLog(os.RemoveAll(filename))
}

// FileExist checking if file exists by path
func FileExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}

// SettingsFile return map[string]string from the setting file
func SettingsFile(filename string) (compare map[string]any) {
if runtime.GOOS != "windows" {
filename = fmt.Sprintf("/%s", filename)
}
if FileExist(filename) {
f, err := os.ReadFile(filename)
ToLog(err, fmt.Sprintf("settings file '%s' is not access", filename))
err = json.Unmarshal(f, &compare)
ToLog(err, fmt.Sprintf("settings file '%s' parse error", filename))
func FileExist(filename string) bool {
if filename == "." {
return false
}
return
_, err := os.Stat(filename)
return err == nil
}

// ValidUUID - validation on the UUID
Expand Down Expand Up @@ -194,71 +168,77 @@ func CloseFile(file *os.File) {
}
}

// FilepathElements - split filename using separate symbols
func FilepathElements(path string) []string {
return strings.FieldsFunc(path, func(symbols rune) bool {
return strings.ContainsAny(string(symbols), `\/`)
})
// SettingsFile return map[string]string from the setting file
func SettingsFile(filename string) (compare map[string]any) {
filename = separatorCorrect(filename)
if FileExist(filename) {
f, err := os.ReadFile(filename)
ToLog(err, fmt.Sprintf("settings file '%s' is not access", filename))
err = json.Unmarshal(f, &compare)
ToLog(err, fmt.Sprintf("settings file '%s' parse error", filename))
}
return
}

// CreateFile create file or temp file
func CreateFile(path string) string {
if path == "" {
func CreateFile(filename string) string {
filename = separatorCorrect(filename)
if filename == "" || filename == "." {
file, err := os.CreateTemp("", "*.tmpgo")
defer CloseFile(file)
ToLog(err, fmt.Sprintf("file '%s' not created", path))
ToLog(err, fmt.Sprintf("file '%s' not created", filename))
return file.Name()
} else {
file, err := os.Create(path)
file, err := os.Create(filename)
defer CloseFile(file)
ToLog(err, fmt.Sprintf("file '%s' not created", path))
ToLog(err, fmt.Sprintf("file '%s' not created", filename))
return file.Name()
}
}

// ValidTempFile - validation type.
// create same name file in Temp folder or create random temp file
func ValidTempFile(filename string) string {
filename = strings.TrimSpace(filename)
if filename == "" {
return CreateFile("")
filename = separatorCorrect(filename)
_, file := filepath.Split(filename)

if filename == "" || filename == "." {
return CreateFile(filename)
}
elementsOfPath := FilepathElements(filename)
newFilepath := filepath.Join(os.TempDir(), elementsOfPath[len(elementsOfPath)-1])
if !FileExist(newFilepath) {
CreateFile(newFilepath)

filename = filepath.Join(os.TempDir(), file)
if !FileExist(filename) {
return CreateFile(filename)
}
return newFilepath

return filename
}

// ValidConfigFile - validation type.
// create same name file in Temp folder or create random temp file
func ValidConfigFile(filename string) string {
filename = strings.TrimSpace(filename)
if filename == "" {
return ""
}
elementsOfPath := FilepathElements(filename)
newFilepath := filepath.Join(elementsOfPath...)
if !FileExist(newFilepath) {
filename = separatorCorrect(filename)

if !FileExist(filename) || filename == "" || filename == "." {
return ""
}
return newFilepath

return filename
}

// ValidFile - validation type.
// Create file in not exist.
func ValidFile(filename string) string {
filename = strings.TrimSpace(filename)
if filename == "" {
return CreateFile("")
filename = separatorCorrect(filename)

if filename == "" || filename == "." {
return CreateFile(filename)
}
elementsOfPath := FilepathElements(filename)
newFilepath := filepath.Join(elementsOfPath...)
if !FileExist(newFilepath) {
CreateFile(newFilepath)

if !FileExist(filename) {
CreateFile(filename)
}
return newFilepath
return filename
}

// ValidDuration - validation on the duration
Expand Down Expand Up @@ -317,3 +297,8 @@ func ValidURL(v string) string {
}
return address + ":" + correctPort
}

func separatorCorrect(filename string) string {
r := strings.NewReplacer("/", string(os.PathSeparator), "\\", string(os.PathSeparator))
return filepath.Clean(r.Replace(strings.TrimSpace(filename)))
}

0 comments on commit fe28fc9

Please sign in to comment.