fix removing empty recording folders (#4227) (#4231)

This commit is contained in:
Alessandro Ros
2025-02-07 17:07:08 +01:00
committed by GitHub
parent e8297478f3
commit d31f1ab882
2 changed files with 34 additions and 0 deletions

View File

@@ -3,7 +3,10 @@ package recordcleaner
import (
"context"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
"github.com/bluenviron/mediamtx/internal/conf"
@@ -118,6 +121,17 @@ func (c *Cleaner) processPath(now time.Time, pathName string) error {
return nil
}
err = c.deleteExpiredSegments(now, pathName, pathConf)
if err != nil {
return err
}
c.deleteEmptyDirs(pathConf)
return nil
}
func (c *Cleaner) deleteExpiredSegments(now time.Time, pathName string, pathConf *conf.Path) error {
end := now.Add(-time.Duration(pathConf.RecordDeleteAfter))
segments, err := recordstore.FindSegments(pathConf, pathName, nil, &end)
if err != nil {
@@ -131,3 +145,20 @@ func (c *Cleaner) processPath(now time.Time, pathName string) error {
return nil
}
func (c *Cleaner) deleteEmptyDirs(pathConf *conf.Path) {
recordPath := strings.ReplaceAll(pathConf.RecordPath, "%path", pathConf.Name)
commonPath := recordstore.CommonPath(recordPath)
filepath.WalkDir(commonPath, func(fpath string, info fs.DirEntry, err error) error { //nolint:errcheck
if err != nil {
return err
}
if info.IsDir() {
os.Remove(fpath)
}
return nil
})
}

View File

@@ -102,6 +102,9 @@ func TestCleanerMultipleEntriesSamePath(t *testing.T) {
_, err = os.Stat(filepath.Join(dir, "path1", "2009-05-19_22-15-25-000427.mp4"))
require.Error(t, err)
_, err = os.Stat(filepath.Join(dir, "path1"))
require.Error(t, err, "testing")
_, err = os.Stat(filepath.Join(dir, "path2", "2009-05-19_22-15-25-000427.mp4"))
require.NoError(t, err)
}