diff options
author | Keuin <[email protected]> | 2022-09-12 12:13:04 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-12 12:13:04 +0800 |
commit | 4d9af257d3824c7066ddc4ab856e00f656224d32 (patch) | |
tree | a31b5b4cb5dbc8f20f224680b57e35271ecfe210 | |
parent | e2790f75670e909f78b4a70aca39b9ec97536868 (diff) |
Defer file rename routine
only if the file is successfully opened.
-rw-r--r-- | recording/runner.go | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/recording/runner.go b/recording/runner.go index a2f2edc..e3735cb 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -204,6 +204,12 @@ func record( saveDir := task.Download.SaveDirectory filePath := path.Join(saveDir, fileName) + file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + task.logger.Error("Cannot open file for writing: %v", err) + cancelled = true + return + } // rename the extension name to originalExtName when finish writing defer func() { if extName == originalExtName { @@ -218,13 +224,6 @@ func record( } task.logger.Info("Rename file \"%s\" to \"%s\".", from, to) }() - - file, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) - if err != nil { - task.logger.Error("Cannot open file for writing: %v", err) - cancelled = true - return - } defer func() { _ = file.Close() }() writeBufferSize := task.Download.DiskWriteBufferBytes |