diff --git a/cmd/ova-provider-server/ova-provider-server.go b/cmd/ova-provider-server/ova-provider-server.go index 1d5fab08d..895306a81 100644 --- a/cmd/ova-provider-server/ova-provider-server.go +++ b/cmd/ova-provider-server/ova-provider-server.go @@ -23,6 +23,8 @@ import ( const ( invalidRequestMethodMsg = "Invalid request method" errorProcessingOvfMsg = "Error processing OVF file" + OvaExt = ".ova" + OvfExt = ".ovf" ) // xml struct @@ -299,28 +301,56 @@ func findOVAFiles(directory string) ([]string, []string, error) { } var ovaFiles, ovfFiles []string + + // Check for .ova files in the main directory (zero level) + for _, child := range childs { + path := filepath.Join(directory, child.Name()) + if hasSuffixIgnoreCase(child.Name(), OvaExt) { + ovaFiles = append(ovaFiles, path) + } + } + + // Check for .ova and .ovf files in child directories (first level) for _, child := range childs { if !child.IsDir() { continue } - newDir := directory + "/" + child.Name() + newDir := filepath.Join(directory, child.Name()) files, err := os.ReadDir(newDir) if err != nil { return nil, nil, err } for _, file := range files { - path := filepath.Join(directory, child.Name(), file.Name()) + path := filepath.Join(newDir, file.Name()) switch { - case strings.HasSuffix(strings.ToLower(file.Name()), ".ova"): + case hasSuffixIgnoreCase(file.Name(), OvaExt): ovaFiles = append(ovaFiles, path) - case strings.HasSuffix(strings.ToLower(file.Name()), ".ovf"): + case hasSuffixIgnoreCase(file.Name(), OvfExt): ovfFiles = append(ovfFiles, path) + case file.IsDir(): + // Check for .ovf files in subdirectories (second level) + subDir := filepath.Join(newDir, file.Name()) + subFiles, err := os.ReadDir(subDir) + if err != nil { + continue + } + for _, subFile := range subFiles { + subPath := filepath.Join(subDir, subFile.Name()) + if hasSuffixIgnoreCase(subFile.Name(), OvfExt) { + ovfFiles = append(ovfFiles, subPath) + } + } } } } return ovaFiles, ovfFiles, nil } +// Checks if the given file has the desired extension +func hasSuffixIgnoreCase(fileName, suffix string) bool { + return strings.HasSuffix(strings.ToLower(fileName), strings.ToLower(suffix)) +} + func readOVFFromOVA(ovaFile string) (*Envelope, error) { var envelope Envelope file, err := os.Open(ovaFile)