diff --git a/README.md b/README.md index 7a9b71d..48ea5c5 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,8 @@ recorder: dirFileMode: 0700 # File mode permissions for the recording files (octal) fileMode: 0700 + # Whether to write to /dev/null instead of a file (for testing) + writeToDevNull: false pubsub: channels: diff --git a/config/bbb-webrtc-recorder.yml b/config/bbb-webrtc-recorder.yml index 851e858..ad41333 100644 --- a/config/bbb-webrtc-recorder.yml +++ b/config/bbb-webrtc-recorder.yml @@ -4,6 +4,7 @@ recorder: directory: /var/lib/bbb-webrtc-recorder dirFileMode: 0700 fileMode: 0600 + writeToDevNull: false pubsub: channels: diff --git a/internal/config/config.go b/internal/config/config.go index fce93d9..04167d2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -42,6 +42,7 @@ func (cfg *Config) SetDefaults() { cfg.Recorder.DirFileMode = "0700" cfg.Recorder.FileMode = "0600" + cfg.Recorder.WriteToDevNull = false cfg.PubSub.Channels = Channels{ Subscribe: "to-" + cfg.App.Name, Publish: "from-" + cfg.App.Name, @@ -67,9 +68,10 @@ func (cfg *Config) SetDefaults() { } type Recorder struct { - Directory string `yaml:"directory,omitempty"` - DirFileMode string `yaml:"dirFileMode,omitempty"` - FileMode string `yaml:"fileMode,omitempty"` + Directory string `yaml:"directory,omitempty"` + DirFileMode string `yaml:"dirFileMode,omitempty"` + FileMode string `yaml:"fileMode,omitempty"` + WriteToDevNull bool `yaml:"writeToDevNull,omitempty"` } type Redis struct { diff --git a/internal/webrtc/recorder/recorder.go b/internal/webrtc/recorder/recorder.go index 3ff8ac5..be3951b 100644 --- a/internal/webrtc/recorder/recorder.go +++ b/internal/webrtc/recorder/recorder.go @@ -34,35 +34,43 @@ func NewRecorder(ctx context.Context, cfg config.Recorder, file string) (Recorde return nil, fmt.Errorf("directory does not exist %s", cfg.Directory) } - file = path.Clean(dir + string(os.PathSeparator) + file) - fileDir := path.Dir(file) + // If we are not writing to /dev/null, we need to check if the file exists + // and if the directory is writable. + if !cfg.WriteToDevNull { + file = path.Clean(dir + string(os.PathSeparator) + file) + fileDir := path.Dir(file) - if stat, err := os.Stat(fileDir); err != nil { - log.WithField("session", ctx.Value("session")). - Debug(stat) + if stat, err := os.Stat(fileDir); err != nil { + log.WithField("session", ctx.Value("session")). + Debug(stat) - if !os.IsNotExist(err) { - return nil, fmt.Errorf("file directory is not accessible %s", fileDir) - } + if !os.IsNotExist(err) { + return nil, fmt.Errorf("file directory is not accessible %s", fileDir) + } - var dirFileMode os.FileMode - if parsedFileMode, err := strconv.ParseUint(cfg.DirFileMode, 0, 32); err != nil { - return nil, fmt.Errorf("invalid file mode %s", cfg.DirFileMode) - } else { - dirFileMode = os.FileMode(parsedFileMode) - } + var dirFileMode os.FileMode + if parsedFileMode, err := strconv.ParseUint(cfg.DirFileMode, 0, 32); err != nil { + return nil, fmt.Errorf("invalid file mode %s", cfg.DirFileMode) + } else { + dirFileMode = os.FileMode(parsedFileMode) + } - err = os.MkdirAll(fileDir, dirFileMode) + err = os.MkdirAll(fileDir, dirFileMode) - if err != nil && !os.IsExist(err) { - return nil, fmt.Errorf("file directory could not be created %s", fileDir) + if err != nil && !os.IsExist(err) { + return nil, fmt.Errorf("file directory could not be created %s", fileDir) + } } - } - if stat, err := os.Stat(file); !os.IsNotExist(err) { - log.WithField("session", ctx.Value("session")). - Debug(stat) - return nil, fmt.Errorf("file already exists %s", file) + if stat, err := os.Stat(file); !os.IsNotExist(err) { + log.WithField("session", ctx.Value("session")). + Debug(stat) + return nil, fmt.Errorf("file already exists %s", file) + } + } else { + // We're writing to /dev/null - see the recorder.writeToDevNull config + // (for testing purposes) + file = os.DevNull } var fileMode os.FileMode