package utils import ( "encoding/base64" "io" "os" "os/exec" "strconv" "strings" "github.com/sirupsen/logrus" "gopkg.in/ini.v1" ) // 缓存 timeZone 信息 var timeZoneCache string // FileExists checks if a file exists and is not a directory before we // try using it to prevent further errors. func FileExists(filename string) bool { info, err := os.Stat(filename) if os.IsNotExist(err) { return false } return !info.IsDir() } // pbx 不能正确读取时区, 读取自定义文件 /etc/asterisk/ntp.conf func GetLocationName() string { if timeZoneCache != "" { return timeZoneCache } filePath := "/etc/asterisk/ntp.conf" _, err := os.Stat(filePath) if err != nil { logrus.Error(err) return "" } iniFile, err := ini.Load(filePath) if err != nil { logrus.Error(err) return "" } timeZoneCache = iniFile.Section("ntp").Key("TZNAME").Value() logrus.Infof("use location %s", timeZoneCache) return timeZoneCache } func GetDuration(filePath string) (int, error) { cmd := exec.Command("soxi", "-D", filePath) output, err := cmd.Output() if err != nil { return 0, err } durationStr := strings.TrimSpace(string(output)) duration, err := strconv.ParseFloat(durationStr, 64) if err != nil { return 0, err } return int(duration), nil } func AudioFileEncode(dstFile, srcFile string) error { data := "sripis123" encoded := base64.StdEncoding.EncodeToString([]byte(data)) // Step 2: 创建一个新文件,写入 Base64 编码结果 outputFile, err := os.Create(dstFile) if err != nil { return err } defer outputFile.Close() _, err = outputFile.WriteString(encoded) if err != nil { return err } // Step 3: 打开 audio.wav 文件 audioFile, err := os.Open(srcFile) if err != nil { return err } defer audioFile.Close() // Step 4: 将 audio.wav 的内容追加到 output.bin 文件末尾 _, err = audioFile.Seek(0, io.SeekStart) // 确保从文件开头读取 if err != nil { return err } _, err = io.Copy(outputFile, audioFile) if err != nil { return err } return err }