mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-09-26 19:51:26 +08:00
@@ -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
|
||||
})
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user