diff --git a/store/image.go b/store/image.go index 25c093cd99114426375af5caf3542f57f2dae8af..22b76b5497ce99a6cb2c66b4afb50de590c026de 100644 --- a/store/image.go +++ b/store/image.go @@ -24,6 +24,7 @@ type Image struct { User string `json:"user"` Source string `json:"source"` Parent string `json:"parent"` + Child string `json:"child"` Commentary string `json:"commentary"` CommentaryTranslation string `json:"commentary_translation"` } @@ -302,27 +303,61 @@ func (s *Store) ImageUpdate(hash, source, parent, commentary, commentaryTranslat s.getLock(hash).Lock() defer s.getLock(hash).Unlock() + var msg string + // Update and save if source != "\000" { info.Source = source + msg += "source" } - if parent != "\000" && s.ImageSnowflake(parent).Snowflake == parent { - info.Parent = parent + if parent != "\000" { + if p := s.ImageSnowflake(parent); p.Snowflake == parent { + s.getLock(p.Hash).Lock() + defer s.getLock(p.Hash).Unlock() + + info.Parent = parent + + // Update the parent to reflect the child + p.Child = info.Snowflake + s.imageMetadataWrite(p) + + if msg != "" { + msg += ", " + } + msg += "parent " + parent + } } if commentary != "\000" { info.Commentary = commentary + + if msg != "" { + msg += ", " + } + msg += "commentary" } if commentaryTranslation != "\000" { info.CommentaryTranslation = commentaryTranslation + + if msg != "" { + msg += ", " + } + msg += "commentary translation" + } + + if msg != "" { + s.imageMetadataWrite(info) + log.Infof("Image %s %s updated.", info.Snowflake, msg) } +} + +func (s *Store) imageMetadataWrite(info Image) { if payload, err := json.Marshal(info); err != nil { - s.fatalClose(fmt.Sprintf("Error encoding metadata of image %s while updating, %s", hash, err)) + s.fatalClose(fmt.Sprintf("Error encoding metadata of image %s, %s", info.Hash, err)) } else { - if err = os.WriteFile(s.ImageMetadataPath(hash), payload, s.PermissionFile); err != nil { - s.fatalClose(fmt.Sprintf("Error saving metadata of image %s while updating, %s", hash, err)) + if err = os.WriteFile(s.ImageMetadataPath(info.Hash), payload, s.PermissionFile); err != nil { + s.fatalClose(fmt.Sprintf("Error saving metadata of image %s, %s", info.Hash, err)) } } - log.Infof("Image hash %s source set to %s.", hash, source) } // ImageSnowflakes returns a slice of image snowflakes.