diff --git a/directories_test.go b/directories_test.go new file mode 100644 index 0000000000..807415ecf4 --- /dev/null +++ b/directories_test.go @@ -0,0 +1,42 @@ + package koii + +import ( + "fmt" + "os" + "path/filepath" + "strings" +) + +// CreateDirectory creates a new directory with the given path +// It returns an error if the directory cannot be created +func CreateDirectory(path string) error { + path = filepath.Clean(path) + + if !isDirPathValid(path) { + return fmt.Errorf("invalid directory path: %s", path) + } + + err := os.MkdirAll(path, 0755) + if err != nil { + return fmt.Errorf("failed to create directory: %w", err) + } + + return nil +} + +// isDirPathValid checks if the given path is a valid directory path +func isDirPathValid(path string) bool { + if strings.ContainsAny(path, "\\/?%*:|\"<>") { + return false + } + + info, err := os.Stat(path) + if err != nil { + if os.IsNotExist(err) { + return true + } + return false + } + + return info.IsDir() +} \ No newline at end of file