streamdata: edit metadata

This edits metadata in a robust way.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
2026-03-20 02:12:30 +09:00
parent 15222cbc25
commit 140f2a3b0f
2 changed files with 62 additions and 0 deletions

View File

@@ -224,6 +224,46 @@ func (c *Channel) Add(ident *Ident, f func(v *VOD, w io.Writer) error) error {
return nil
}
// Edit loads a [VOD] by its [Ident], and writes the modified [VOD] back to the
// on-disk representation.
func (c *Channel) Edit(ident *Ident, f func(v *VOD) error) error {
v, err := c.Load(ident)
if err != nil {
return err
}
err = f(v)
if err != nil {
return err
}
pathname := path.Join(
channelPathVOD,
"."+ident.String(),
)
var w *os.File
if w, err = c.root.OpenFile(
pathname,
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
0444,
); err != nil {
return err
} else if err = json.NewEncoder(w).Encode(v); err != nil {
_ = w.Close()
_ = c.root.Remove(pathname)
return err
} else if err = w.Close(); err != nil {
_ = c.root.Remove(pathname)
return err
} else {
return c.root.Rename(pathname, path.Join(
channelPathVOD,
ident.String(),
))
}
}
// Path returns a pathname by [Ident].
func (c *Channel) Path(ident *Ident) string {
return path.Join(