package configloader

import (
    "os"
    "io/fs"
    "errors"
    "path/filepath"
)

// Checks if a directory is the entered
// directory can be used as an source directory
// for export data. As such, it does need to
// exist, and it needs to not contain unexpected files.
// Returns the directory name or an error.
func ValidateUploadDir(path string) (string, error) {

    // Check the path actually exists
    pathInfo, err := os.Stat(path)
    if errors.Is(err, fs.ErrNotExist) {
        return "", err
    }

    // Check the input path is a directory
    if !pathInfo.IsDir() {
        return "", errors.New("The upload directory " + path + " is not a directory")
    }

    // Return absolute path
    absPath, absPathErr := filepath.Abs(path)
    if (absPathErr != nil) {
        return "", errors.New("Invalid path")
    }
    return absPath, nil

}
