Skip to content

Commit

Permalink
Update gallery when adding image via scan (#3802)
Browse files Browse the repository at this point in the history
  • Loading branch information
WithoutPants authored Jun 6, 2023
1 parent e22291d commit 0c99908
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions pkg/image/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type GalleryFinderCreator interface {
FindByFileID(ctx context.Context, fileID file.ID) ([]*models.Gallery, error)
FindByFolderID(ctx context.Context, folderID file.FolderID) ([]*models.Gallery, error)
Create(ctx context.Context, newObject *models.Gallery, fileIDs []file.ID) error
UpdatePartial(ctx context.Context, id int, updatedGallery models.GalleryPartial) (*models.Gallery, error)
}

type ScanConfig interface {
Expand Down Expand Up @@ -117,17 +118,32 @@ func (h *ScanHandler) Handle(ctx context.Context, f file.File, oldFile file.File

logger.Infof("%s doesn't exist. Creating new image...", f.Base().Path)

if _, err := h.associateGallery(ctx, newImage, imageFile); err != nil {
g, err := h.getGalleryToAssociate(ctx, newImage, f)
if err != nil {
return err
}

if g != nil {
newImage.GalleryIDs.Add(g.ID)
logger.Infof("Adding %s to gallery %s", f.Base().Path, g.Path)
}

if err := h.CreatorUpdater.Create(ctx, &models.ImageCreateInput{
Image: newImage,
FileIDs: []file.ID{imageFile.ID},
}); err != nil {
return fmt.Errorf("creating new image: %w", err)
}

// update the gallery updated at timestamp if applicable
if g != nil {
if _, err := h.GalleryFinder.UpdatePartial(ctx, g.ID, models.GalleryPartial{
UpdatedAt: models.NewOptionalTime(time.Now()),
}); err != nil {
return fmt.Errorf("updating gallery updated at timestamp: %w", err)
}
}

h.PluginCache.RegisterPostHooks(ctx, newImage.ID, plugin.ImageCreatePost, nil, nil)

existing = []*models.Image{newImage}
Expand Down Expand Up @@ -172,16 +188,18 @@ func (h *ScanHandler) associateExisting(ctx context.Context, existing []*models.
}

// associate with gallery if applicable
changed, err := h.associateGallery(ctx, i, f)
g, err := h.getGalleryToAssociate(ctx, i, f)
if err != nil {
return err
}

var galleryIDs *models.UpdateIDs
if changed {
changed := false
if g != nil {
changed = true
galleryIDs = &models.UpdateIDs{
IDs: i.GalleryIDs.List(),
Mode: models.RelationshipUpdateModeSet,
IDs: []int{g.ID},
Mode: models.RelationshipUpdateModeAdd,
}
}

Expand All @@ -203,6 +221,14 @@ func (h *ScanHandler) associateExisting(ctx context.Context, existing []*models.
}); err != nil {
return fmt.Errorf("updating image: %w", err)
}

if g != nil {
if _, err := h.GalleryFinder.UpdatePartial(ctx, g.ID, models.GalleryPartial{
UpdatedAt: models.NewOptionalTime(time.Now()),
}); err != nil {
return fmt.Errorf("updating gallery updated at timestamp: %w", err)
}
}
}

if changed || updateExisting {
Expand Down Expand Up @@ -331,22 +357,19 @@ func (h *ScanHandler) getOrCreateGallery(ctx context.Context, f file.File) (*mod
return nil, nil
}

func (h *ScanHandler) associateGallery(ctx context.Context, newImage *models.Image, f file.File) (bool, error) {
func (h *ScanHandler) getGalleryToAssociate(ctx context.Context, newImage *models.Image, f file.File) (*models.Gallery, error) {
g, err := h.getOrCreateGallery(ctx, f)
if err != nil {
return false, err
return nil, err
}

if err := newImage.LoadGalleryIDs(ctx, h.CreatorUpdater); err != nil {
return false, err
return nil, err
}

ret := false
if g != nil && !intslice.IntInclude(newImage.GalleryIDs.List(), g.ID) {
ret = true
newImage.GalleryIDs.Add(g.ID)
logger.Infof("Adding %s to gallery %s", f.Base().Path, g.Path)
return g, nil
}

return ret, nil
return nil, nil
}

0 comments on commit 0c99908

Please sign in to comment.