refactor: allow to preload from gallery, cleanup dup code

This commit is contained in:
mudler 2023-06-26 00:40:54 +02:00
parent f12b6298ca
commit 43cf62e5f8
2 changed files with 16 additions and 21 deletions

View file

@ -80,13 +80,13 @@ func App(opts ...AppOption) (*fiber.App, error) {
app.Use(recover.New())
if options.preloadJSONModels != "" {
if err := ApplyGalleryFromString(options.loader.ModelPath, options.preloadJSONModels, cm); err != nil {
if err := ApplyGalleryFromString(options.loader.ModelPath, options.preloadJSONModels, cm, options.galleries); err != nil {
return nil, err
}
}
if options.preloadModelsFromPath != "" {
if err := ApplyGalleryFromFile(options.loader.ModelPath, options.preloadModelsFromPath, cm); err != nil {
if err := ApplyGalleryFromFile(options.loader.ModelPath, options.preloadModelsFromPath, cm, options.galleries); err != nil {
return nil, err
}
}

View file

@ -143,40 +143,35 @@ func displayDownload(fileName string, current string, total string, percentage f
}
}
func ApplyGalleryFromFile(modelPath, s string, cm *ConfigMerger) error {
type galleryModel struct {
gallery.GalleryModel
ID string `json:"id"`
}
func ApplyGalleryFromFile(modelPath, s string, cm *ConfigMerger, galleries []gallery.Gallery) error {
dat, err := os.ReadFile(s)
if err != nil {
return err
}
var requests []gallery.GalleryModel
err = json.Unmarshal(dat, &requests)
if err != nil {
return err
return ApplyGalleryFromString(modelPath, string(dat), cm, galleries)
}
for _, r := range requests {
if err := prepareModel(modelPath, r, cm, displayDownload); err != nil {
return err
}
}
return nil
}
func ApplyGalleryFromString(modelPath, s string, cm *ConfigMerger) error {
var requests []gallery.GalleryModel
func ApplyGalleryFromString(modelPath, s string, cm *ConfigMerger, galleries []gallery.Gallery) error {
var requests []galleryModel
err := json.Unmarshal([]byte(s), &requests)
if err != nil {
return err
}
for _, r := range requests {
if err := prepareModel(modelPath, r, cm, displayDownload); err != nil {
return err
if r.ID == "" {
err = prepareModel(modelPath, r.GalleryModel, cm, displayDownload)
} else {
err = gallery.InstallModelFromGallery(galleries, r.ID, modelPath, r.GalleryModel, displayDownload)
}
}
return nil
return err
}
func getOpStatus(g *galleryApplier) func(c *fiber.Ctx) error {