Skip to content

Commit eb0f501

Browse files
committed
WIP
Signed-off-by: Ettore Di Giacinto <[email protected]>
1 parent 5fed9c6 commit eb0f501

File tree

1 file changed

+72
-21
lines changed

1 file changed

+72
-21
lines changed

core/gallery/models.go

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"slices"
910
"strings"
1011

1112
"dario.cat/mergo"
@@ -293,21 +294,32 @@ func GetLocalModelConfiguration(basePath string, name string) (*ModelConfig, err
293294
return ReadConfigFile[ModelConfig](galleryFile)
294295
}
295296

296-
func DeleteModelFromSystem(systemState *system.SystemState, name string) error {
297-
additionalFiles := []string{}
297+
func listModelFiles(systemState *system.SystemState, name string) ([]string, error) {
298298

299299
configFile := filepath.Join(systemState.Model.ModelsPath, fmt.Sprintf("%s.yaml", name))
300300
if err := utils.VerifyPath(configFile, systemState.Model.ModelsPath); err != nil {
301-
return fmt.Errorf("failed to verify path %s: %w", configFile, err)
301+
return nil, fmt.Errorf("failed to verify path %s: %w", configFile, err)
302+
}
303+
304+
// os.PathSeparator is not allowed in model names. Replace them with "__" to avoid conflicts with file paths.
305+
name = strings.ReplaceAll(name, string(os.PathSeparator), "__")
306+
307+
galleryFile := filepath.Join(systemState.Model.ModelsPath, galleryFileName(name))
308+
if err := utils.VerifyPath(galleryFile, systemState.Model.ModelsPath); err != nil {
309+
return nil, fmt.Errorf("failed to verify path %s: %w", galleryFile, err)
302310
}
311+
312+
additionalFiles := []string{}
313+
allFiles := []string{}
314+
303315
// Galleryname is the name of the model in this case
304316
dat, err := os.ReadFile(configFile)
305317
if err == nil {
306318
modelConfig := &lconfig.ModelConfig{}
307319

308320
err = yaml.Unmarshal(dat, &modelConfig)
309321
if err != nil {
310-
return err
322+
return nil, err
311323
}
312324
if modelConfig.Model != "" {
313325
additionalFiles = append(additionalFiles, modelConfig.ModelFileName())
@@ -318,26 +330,15 @@ func DeleteModelFromSystem(systemState *system.SystemState, name string) error {
318330
}
319331
}
320332

321-
// os.PathSeparator is not allowed in model names. Replace them with "__" to avoid conflicts with file paths.
322-
name = strings.ReplaceAll(name, string(os.PathSeparator), "__")
323-
324-
galleryFile := filepath.Join(systemState.Model.ModelsPath, galleryFileName(name))
325-
if err := utils.VerifyPath(galleryFile, systemState.Model.ModelsPath); err != nil {
326-
return fmt.Errorf("failed to verify path %s: %w", galleryFile, err)
327-
}
328-
329-
var filesToRemove []string
330-
331-
// Delete all the files associated to the model
332333
// read the model config
333334
galleryconfig, err := ReadConfigFile[ModelConfig](galleryFile)
334335
if err == nil && galleryconfig != nil {
335336
for _, f := range galleryconfig.Files {
336337
fullPath := filepath.Join(systemState.Model.ModelsPath, f.Filename)
337338
if err := utils.VerifyPath(fullPath, systemState.Model.ModelsPath); err != nil {
338-
return fmt.Errorf("failed to verify path %s: %w", fullPath, err)
339+
return allFiles, fmt.Errorf("failed to verify path %s: %w", fullPath, err)
339340
}
340-
filesToRemove = append(filesToRemove, fullPath)
341+
allFiles = append(allFiles, fullPath)
341342
}
342343
} else {
343344
log.Error().Err(err).Msgf("failed to read gallery file %s", configFile)
@@ -346,18 +347,68 @@ func DeleteModelFromSystem(systemState *system.SystemState, name string) error {
346347
for _, f := range additionalFiles {
347348
fullPath := filepath.Join(filepath.Join(systemState.Model.ModelsPath, f))
348349
if err := utils.VerifyPath(fullPath, systemState.Model.ModelsPath); err != nil {
349-
return fmt.Errorf("failed to verify path %s: %w", fullPath, err)
350+
return allFiles, fmt.Errorf("failed to verify path %s: %w", fullPath, err)
350351
}
351-
filesToRemove = append(filesToRemove, fullPath)
352+
allFiles = append(allFiles, fullPath)
352353
}
353354

354-
filesToRemove = append(filesToRemove, galleryFile)
355+
allFiles = append(allFiles, galleryFile)
355356

356357
// skip duplicates
357-
filesToRemove = utils.Unique(filesToRemove)
358+
allFiles = utils.Unique(allFiles)
359+
360+
return allFiles, nil
361+
}
362+
363+
func DeleteModelFromSystem(systemState *system.SystemState, name string) error {
364+
configFile := filepath.Join(systemState.Model.ModelsPath, fmt.Sprintf("%s.yaml", name))
365+
366+
filesToRemove, err := listModelFiles(systemState, name)
367+
if err != nil {
368+
return err
369+
}
370+
371+
allOtherFiles := []string{}
372+
// Get all files of all other models
373+
fi, err := os.ReadDir(systemState.Model.ModelsPath)
374+
if err != nil {
375+
return err
376+
}
377+
for _, f := range fi {
378+
if f.IsDir() {
379+
continue
380+
}
381+
if strings.HasPrefix(f.Name(), "._gallery_") {
382+
continue
383+
}
384+
if !strings.HasSuffix(f.Name(), ".yaml") || !strings.HasSuffix(f.Name(), ".yml") {
385+
continue
386+
}
387+
if f.Name() == name {
388+
continue
389+
}
390+
391+
name := strings.TrimSuffix(f.Name(), ".yaml")
392+
name = strings.TrimSuffix(name, ".yml")
393+
394+
log.Debug().Msgf("Checking file %s", f.Name())
395+
files, err := listModelFiles(systemState, name)
396+
if err != nil {
397+
log.Debug().Err(err).Msgf("failed to list files for model %s", f.Name())
398+
continue
399+
}
400+
allOtherFiles = append(allOtherFiles, files...)
401+
}
402+
403+
log.Debug().Msgf("Files to remove: %+v", filesToRemove)
404+
log.Debug().Msgf("All other files: %+v", allOtherFiles)
358405

359406
// Removing files
360407
for _, f := range filesToRemove {
408+
if slices.Contains(allOtherFiles, f) {
409+
log.Debug().Msgf("Skipping file %s because it is part of another model", f)
410+
continue
411+
}
361412
if e := os.Remove(f); e != nil {
362413
log.Error().Err(e).Msgf("failed to remove file %s", f)
363414
}

0 commit comments

Comments
 (0)