-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.go
91 lines (79 loc) · 1.91 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package radikocast
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
const (
defaultOutputDir = "output"
envHome = "RADIKOCAST_HOME"
)
// OutputConfig contains the configuration for output files.
type OutputConfig struct {
DirFullPath string
FileBaseName string // base name of the file
FileFormat string // m4a, mp3
}
func NewOutputConfig(fileBaseName, fileFormat string) (*OutputConfig, error) {
// If the environment variable RADIKOCAST_HOME is set,
// override working directory path.
fullPath := os.Getenv(envHome)
switch {
case fullPath != "" && !filepath.IsAbs(fullPath):
wd, err := os.Getwd()
if err != nil {
return nil, err
}
fullPath = filepath.Join(wd, fullPath)
case fullPath == "":
wd, err := os.Getwd()
if err != nil {
return nil, err
}
fullPath = filepath.Join(wd, defaultOutputDir)
default:
}
return &OutputConfig{
DirFullPath: filepath.Clean(fullPath),
FileBaseName: fileBaseName,
FileFormat: fileFormat,
}, nil
}
// SetupDir configures the output directory or returns an error if failed to create it.
func (c *OutputConfig) SetupDir() error {
_, err := os.Stat(c.DirFullPath)
switch {
case err == nil:
// Output directory already exists.
case os.IsNotExist(err):
// Output directory does not exist.
if err := os.MkdirAll(c.DirFullPath, 0755); err != nil {
return err
}
default:
return err
}
return nil
}
func (c *OutputConfig) TempAACDir() (string, error) {
aacDir, err := ioutil.TempDir(c.DirFullPath, "aac")
if err != nil {
return "", err
}
return aacDir, nil
}
func (c *OutputConfig) AudioFormat() string {
return c.FileFormat
}
func (c *OutputConfig) AudioExt() string {
return c.FileFormat
}
func (c *OutputConfig) AbsPath() string {
name := fmt.Sprintf("%s.%s", c.FileBaseName, c.AudioExt())
return filepath.Join(c.DirFullPath, name)
}
func (c *OutputConfig) IsExist() bool {
_, err := os.Stat(c.AbsPath())
return err == nil
}