Skip to content

Commit

Permalink
fix removing empty recording folders (#4227)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Feb 7, 2025
1 parent a1c6da8 commit 57011f1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/recordcleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package recordcleaner

import (
"context"
"io/fs"
"os"
"path/filepath"
"strings"
"time"

"github.com/bluenviron/mediamtx/internal/conf"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
})
}
3 changes: 3 additions & 0 deletions internal/recordcleaner/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 57011f1

Please sign in to comment.