diff options
author | Keuin <[email protected]> | 2022-09-12 02:59:36 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-12 02:59:36 +0800 |
commit | 32fbadfff7205f94a5089ec8ff2fc1cef30d325d (patch) | |
tree | 5a2b633a4e338402960cc3b4b1918ecb26f77e23 /recording/runner.go | |
parent | 6bbb50f7a400767a8d2ff9b6982099b538203b83 (diff) |
Feature: use alternative file extension name when the downloading is not completed.v0.2.0
Diffstat (limited to 'recording/runner.go')
-rw-r--r-- | recording/runner.go | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/recording/runner.go b/recording/runner.go index 039b760..fddb562 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -25,6 +25,7 @@ type TaskResult struct { } const kReadChunkSize = 128 * 1024 +const kSpecialExtName = "partial" // runTaskWithAutoRestart // start a monitor&download task. @@ -193,12 +194,37 @@ func record( } streamSource := urlInfo.Data.URLs[0] - fileName := fmt.Sprintf( - "%s.%s", - GenerateFileName(profile.Data.Title, time.Now()), - common.Errorable[string](common.GetFileExtensionFromUrl(streamSource.URL)).OrElse("flv"), - ) - filePath := path.Join(task.Download.SaveDirectory, fileName) + var extName string + + // the real extension name (without renaming) + originalExtName := common.Errorable[string](common.GetFileExtensionFromUrl(streamSource.URL)).OrElse("flv") + + if task.TaskConfig.Download.UseSpecialExtNameBeforeFinishing { + extName = kSpecialExtName + } else { + extName = originalExtName + } + + baseName := GenerateFileName(profile.Data.Title, time.Now()) + fileName := common.CombineFileName(baseName, extName) + saveDir := task.Download.SaveDirectory + filePath := path.Join(saveDir, fileName) + + // rename the extension name to originalExtName when finish writing + defer func() { + if extName == originalExtName { + return + } + from := filePath + to := path.Join(saveDir, common.CombineFileName(baseName, originalExtName)) + err := os.Rename(from, to) + if err != nil { + task.logger.Error("Cannot rename %v to %v: %v", from, to, err) + return + } + 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) |