From 7762f2851536c5864a50b5f7c4acb4071e6e4d07 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Fri, 10 Nov 2023 16:01:40 +0100 Subject: [PATCH] record: allow using special characters in recording path --- internal/record/cleaner_test.go | 12 ++++++------ internal/record/record_path.go | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/record/cleaner_test.go b/internal/record/cleaner_test.go index 8b4f50dd6b54..d44b5d8859a5 100644 --- a/internal/record/cleaner_test.go +++ b/internal/record/cleaner_test.go @@ -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( @@ -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) } diff --git a/internal/record/record_path.go b/internal/record/record_path.go index 4f1643914662..5419f3e78622 100644 --- a/internal/record/record_path.go +++ b/internal/record/record_path.go @@ -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})")