Skip to content

Commit

Permalink
record: allow using special characters in recording path (#2674)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Nov 10, 2023
1 parent d51baa0 commit 2fe31a2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
12 changes: 6 additions & 6 deletions internal/record/cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func TestCleaner(t *testing.T) {
require.NoError(t, err)
defer os.RemoveAll(dir)

recordPath := filepath.Join(dir, "%path/%Y-%m-%d_%H-%M-%S-%f")
recordPath := filepath.Join(dir, "_-+*?^$()[]{}|_%path/%Y-%m-%d_%H-%M-%S-%f")

err = os.Mkdir(filepath.Join(dir, "mypath"), 0o755)
err = os.Mkdir(filepath.Join(dir, "_-+*?^$()[]{}|_mypath"), 0o755)
require.NoError(t, err)

err = os.WriteFile(filepath.Join(dir, "mypath", "2008-05-20_22-15-25-000125.mp4"), []byte{1}, 0o644)
err = os.WriteFile(filepath.Join(dir, "_-+*?^$()[]{}|_mypath", "2008-05-20_22-15-25-000125.mp4"), []byte{1}, 0o644)
require.NoError(t, err)

err = os.WriteFile(filepath.Join(dir, "mypath", "2009-05-20_22-15-25-000427.mp4"), []byte{1}, 0o644)
err = os.WriteFile(filepath.Join(dir, "_-+*?^$()[]{}|_mypath", "2009-05-20_22-15-25-000427.mp4"), []byte{1}, 0o644)
require.NoError(t, err)

c := NewCleaner(
Expand All @@ -43,9 +43,9 @@ func TestCleaner(t *testing.T) {

time.Sleep(500 * time.Millisecond)

_, err = os.Stat(filepath.Join(dir, "mypath", "2008-05-20_22-15-25-000125.mp4"))
_, err = os.Stat(filepath.Join(dir, "_-+*?^$()[]{}|_mypath", "2008-05-20_22-15-25-000125.mp4"))
require.Error(t, err)

_, err = os.Stat(filepath.Join(dir, "mypath", "2009-05-20_22-15-25-000427.mp4"))
_, err = os.Stat(filepath.Join(dir, "_-+*?^$()[]{}|_mypath", "2009-05-20_22-15-25-000427.mp4"))
require.NoError(t, err)
}
21 changes: 20 additions & 1 deletion internal/record/record_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,26 @@ type recordPathParams struct {

func decodeRecordPath(format string, v string) *recordPathParams {
re := format
re = strings.ReplaceAll(re, "\\", "\\\\")

for _, ch := range []uint8{
'\\',
'.',
'+',
'*',
'?',
'^',
'$',
'(',
')',
'[',
']',
'{',
'}',
'|',
} {
re = strings.ReplaceAll(re, string(ch), "\\"+string(ch))
}

re = strings.ReplaceAll(re, "%path", "(.*?)")
re = strings.ReplaceAll(re, "%Y", "([0-9]{4})")
re = strings.ReplaceAll(re, "%m", "([0-9]{2})")
Expand Down

0 comments on commit 2fe31a2

Please sign in to comment.