streamdata: optionally rename from alternate

This is useful for migration, or external downloads.

Signed-off-by: Yonah <contrib@gensokyo.uk>
This commit is contained in:
2026-03-19 17:49:31 +09:00
parent 70476f2c51
commit 0e534c06fe
2 changed files with 45 additions and 3 deletions

View File

@@ -143,6 +143,14 @@ func (c *ChannelMismatchError) Error() string {
strconv.FormatUint(c.Want, 10)
}
// RenameFrom is a pathname returned by a function passed to [Channel.Add] to
// rename from this pathname instead of the managed transaction file.
type RenameFrom string
func (pathname RenameFrom) Error() string {
return "requesting rename from " + strconv.Quote(string(pathname))
}
// Add adds a [VOD] and its corresponding asset to the on-disk representation.
func (c *Channel) Add(ident *Ident, f func(v *VOD, w io.Writer) error) error {
if ident == nil || f == nil {
@@ -167,8 +175,20 @@ func (c *Channel) Add(ident *Ident, f func(v *VOD, w io.Writer) error) error {
err = closeErr
}
if err != nil {
_ = c.root.Remove(channelPathPending)
return err
var rf RenameFrom
if !errors.As(err, &rf) {
_ = c.root.Remove(channelPathPending)
return err
}
if err = c.root.Remove(channelPathPending); err != nil {
return err
}
if err = os.Rename(string(rf), path.Join(
c.root.Name(),
channelPathPending,
)); err != nil {
return err
}
}
if err = c.root.Chmod(channelPathPending, 0444); err != nil {