Skip to content

Commit 2a839e1

Browse files
authored
fix(gallery): do not attempt to delete duplicate files (#3031)
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 610e1c0 commit 2a839e1

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

core/gallery/gallery.go

+14-15
Original file line numberDiff line numberDiff line change
@@ -204,34 +204,33 @@ func DeleteModelFromSystem(basePath string, name string, additionalFiles []strin
204204
log.Error().Err(err).Msgf("failed to read gallery file %s", configFile)
205205
}
206206

207+
var filesToRemove []string
208+
207209
// Remove additional files
208210
if galleryconfig != nil {
209211
for _, f := range galleryconfig.Files {
210212
fullPath := filepath.Join(basePath, f.Filename)
211-
log.Debug().Msgf("Removing file %s", fullPath)
212-
if e := os.Remove(fullPath); e != nil {
213-
err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", f.Filename, e))
214-
}
213+
filesToRemove = append(filesToRemove, fullPath)
215214
}
216215
}
217216

218217
for _, f := range additionalFiles {
219218
fullPath := filepath.Join(filepath.Join(basePath, f))
220-
log.Debug().Msgf("Removing additional file %s", fullPath)
221-
if e := os.Remove(fullPath); e != nil {
222-
err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", f, e))
223-
}
219+
filesToRemove = append(filesToRemove, fullPath)
224220
}
225221

226-
log.Debug().Msgf("Removing model config file %s", configFile)
222+
filesToRemove = append(filesToRemove, configFile)
223+
filesToRemove = append(filesToRemove, galleryFile)
227224

228-
// Delete the model config file
229-
if e := os.Remove(configFile); e != nil {
230-
err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", configFile, e))
231-
}
225+
// skip duplicates
226+
filesToRemove = utils.Unique(filesToRemove)
232227

233-
// Delete gallery config file
234-
os.Remove(galleryFile)
228+
// Removing files
229+
for _, f := range filesToRemove {
230+
if e := os.Remove(f); e != nil {
231+
err = errors.Join(err, fmt.Errorf("failed to remove file %s: %w", f, e))
232+
}
233+
}
235234

236235
return err
237236
}

pkg/utils/strings.go

+12
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,15 @@ func RandString(n int) string {
1818
}
1919
return string(b)
2020
}
21+
22+
func Unique(arr []string) []string {
23+
unique := make(map[string]bool)
24+
var result []string
25+
for _, item := range arr {
26+
if _, ok := unique[item]; !ok {
27+
unique[item] = true
28+
result = append(result, item)
29+
}
30+
}
31+
return result
32+
}

0 commit comments

Comments
 (0)