streamdata: edit metadata
This edits metadata in a robust way. Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user