From 70e6308b731dacdf14b650954b19bf70bf58135a Mon Sep 17 00:00:00 2001 From: Ettore Di Giacinto Date: Sun, 28 Jul 2024 09:41:03 +0200 Subject: [PATCH] fix(gallery): do not attempt to delete duplicate files Signed-off-by: Ettore Di Giacinto --- core/gallery/gallery.go | 29 ++++++++++++++--------------- pkg/utils/strings.go | 12 ++++++++++++ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/core/gallery/gallery.go b/core/gallery/gallery.go index d102eac8a7d..9288c44f1ac 100644 --- a/core/gallery/gallery.go +++ b/core/gallery/gallery.go @@ -204,34 +204,33 @@ func DeleteModelFromSystem(basePath string, name string, additionalFiles []strin log.Error().Err(err).Msgf("failed to read gallery file %s", configFile) } + var filesToRemove []string + // Remove additional files if galleryconfig != nil { for _, f := range galleryconfig.Files { fullPath := filepath.Join(basePath, f.Filename) - log.Debug().Msgf("Removing file %s", fullPath) - if e := os.Remove(fullPath); e != nil { - err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", f.Filename, e)) - } + filesToRemove = append(filesToRemove, fullPath) } } for _, f := range additionalFiles { fullPath := filepath.Join(filepath.Join(basePath, f)) - log.Debug().Msgf("Removing additional file %s", fullPath) - if e := os.Remove(fullPath); e != nil { - err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", f, e)) - } + filesToRemove = append(filesToRemove, fullPath) } - log.Debug().Msgf("Removing model config file %s", configFile) + filesToRemove = append(filesToRemove, configFile) + filesToRemove = append(filesToRemove, galleryFile) - // Delete the model config file - if e := os.Remove(configFile); e != nil { - err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", configFile, e)) - } + // skip duplicates + filesToRemove = utils.Unique(filesToRemove) - // Delete gallery config file - os.Remove(galleryFile) + // Removing files + for _, f := range filesToRemove { + if e := os.Remove(f); e != nil { + err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", f, e)) + } + } return err } diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 2a782e033c6..4ac0458d49c 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -18,3 +18,15 @@ func RandString(n int) string { } return string(b) } + +func Unique(arr []string) []string { + unique := make(map[string]bool) + var result []string + for _, item := range arr { + if _, ok := unique[item]; !ok { + unique[item] = true + result = append(result, item) + } + } + return result +}