Skip to content

Commit

Permalink
UTILS: Trim filename to avoid quotation errors in determining file ex…
Browse files Browse the repository at this point in the history
…t. Add IsDir() function.
  • Loading branch information
Cian911 committed Dec 31, 2021
1 parent 1f5eaa4 commit e7a9d48
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package utils

import (
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)

// ExtractFileExt returns the file extension of a file
func ExtractFileExt(path string) string {
return filepath.Ext(path)
// If the path is a directory, returns empty string
if ValidatePath(path) && IsDir(path) {
return ""
}

return filepath.Ext(strings.Trim(path, "'"))
}

// ValidatePath checks if a path exists
Expand All @@ -18,6 +25,7 @@ func ValidatePath(path string) bool {
}

if _, err := os.Stat(path); os.IsNotExist(err) {
log.Printf("EVENT: Path does not exist: %s", path)
return false
}

Expand Down Expand Up @@ -52,3 +60,18 @@ func ScanFilesInDir(path string) (map[string]bool, error) {

return fileList, nil
}

// IsDir returns a boolean if the given path is a directory
func IsDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
log.Printf("Could not find path: %v", err)
return false
}

if fileInfo.IsDir() {
return true
}

return false
}

0 comments on commit e7a9d48

Please sign in to comment.