utils.go 662 B

12345678910111213141516171819202122232425262728
  1. package utils
  2. import (
  3. "path/filepath"
  4. "strings"
  5. )
  6. // IsFileInputValid returns true this is a valid file name.
  7. // This method must be used before joining a file name, generally provided as
  8. // user input, with a directory
  9. func IsFileInputValid(fileInput string) bool {
  10. cleanInput := filepath.Clean(fileInput)
  11. if cleanInput == "." || cleanInput == ".." {
  12. return false
  13. }
  14. return true
  15. }
  16. // IsStringPrefixInSlice searches a string prefix in a slice and returns true
  17. // if a matching prefix is found
  18. func IsStringPrefixInSlice(obj string, list []string) bool {
  19. for _, v := range list {
  20. if strings.HasPrefix(obj, v) {
  21. return true
  22. }
  23. }
  24. return false
  25. }