mirror of
https://github.com/issadarkthing/gomu.git
synced 2025-04-26 13:49:21 +08:00
209 lines
4.2 KiB
Go
209 lines
4.2 KiB
Go
// Copyright (C) 2020 Raziman
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/ztrue/tracerr"
|
|
)
|
|
|
|
// Logs erros to /tmp/gomu.log
|
|
func logError(err error) {
|
|
|
|
tmpDir := os.TempDir()
|
|
logFile := path.Join(tmpDir, "gomu.log")
|
|
file, e := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if e != nil {
|
|
log.Fatalf("Error opening file %s", logFile)
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
log.SetOutput(file)
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
|
|
log.Println(tracerr.SprintSource(err))
|
|
}
|
|
|
|
func debugLog(val ...interface{}) {
|
|
|
|
tmpDir := os.TempDir()
|
|
logFile := path.Join(tmpDir, "gomu.log")
|
|
file, e := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
|
|
if e != nil {
|
|
log.Fatalf("Error opening file %s", logFile)
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
log.SetOutput(file)
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
|
|
log.Println(val...)
|
|
}
|
|
|
|
// Wraps error in a formatted way.
|
|
func wrapError(fnName string, err error) error {
|
|
return fmt.Errorf("%s: \n%e", fnName, err)
|
|
}
|
|
|
|
// Formats duration to my desired output mm:ss
|
|
func fmtDuration(input time.Duration) string {
|
|
|
|
val := input.Round(time.Second).String()
|
|
|
|
if !strings.Contains(val, "m") {
|
|
val = "0m" + val
|
|
}
|
|
val = strings.ReplaceAll(val, "h", ":")
|
|
val = strings.ReplaceAll(val, "m", ":")
|
|
val = strings.ReplaceAll(val, "s", "")
|
|
var result []string
|
|
|
|
for _, v := range strings.Split(val, ":") {
|
|
|
|
if len(v) < 2 {
|
|
result = append(result, "0"+v)
|
|
} else {
|
|
result = append(result, v)
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(result, ":")
|
|
}
|
|
|
|
// fmtDurationH returns the formatted duration `x hr x min`
|
|
func fmtDurationH(input time.Duration) string {
|
|
|
|
re := regexp.MustCompile(`\d+s`)
|
|
val := input.Round(time.Second).String()
|
|
|
|
// remove seconds
|
|
result := re.ReplaceAllString(val, "")
|
|
result = strings.Replace(result, "h", " hr ", 1)
|
|
result = strings.Replace(result, "m", " min", 1)
|
|
|
|
if result == "" {
|
|
return "0 hr 0 min"
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// Expands relative path to absolute path and tilde to /home/(user)
|
|
func expandFilePath(path string) string {
|
|
p := expandTilde(path)
|
|
|
|
if filepath.IsAbs(p) {
|
|
return p
|
|
}
|
|
|
|
p, err := filepath.Abs(p)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
// Expands tilde alias to /home/user
|
|
func expandTilde(_path string) string {
|
|
if !strings.HasPrefix(_path, "~") {
|
|
return _path
|
|
}
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
log.Panicln(tracerr.SprintSource(err))
|
|
}
|
|
|
|
return path.Join(home, strings.TrimPrefix(_path, "~"))
|
|
}
|
|
|
|
// Detects the filetype of file
|
|
func getFileContentType(out *os.File) (string, error) {
|
|
|
|
buffer := make([]byte, 512)
|
|
|
|
_, err := out.Read(buffer)
|
|
if err != nil {
|
|
return "", tracerr.Wrap(err)
|
|
}
|
|
|
|
contentType := http.DetectContentType(buffer)
|
|
|
|
return strings.SplitAfter(contentType, "/")[1], nil
|
|
}
|
|
|
|
// Gets the file name by removing extension and path
|
|
func getName(fn string) string {
|
|
return strings.TrimSuffix(path.Base(fn), ".mp3")
|
|
}
|
|
|
|
// This just parsing the output from the ytdl to get the audio path
|
|
// This is used because we need to get the song name
|
|
// example ~/path/to/song/song.mp3
|
|
func extractFilePath(output []byte, dir string) string {
|
|
|
|
regexSearch := fmt.Sprintf(`\[ffmpeg\] Destination: %s\/.*.mp3`,
|
|
escapeBackSlash(dir))
|
|
|
|
parseAudioPathOnly := regexp.MustCompile(`\/.*mp3$`)
|
|
|
|
re := regexp.MustCompile(regexSearch)
|
|
|
|
return string(parseAudioPathOnly.Find(re.Find(output)))
|
|
|
|
}
|
|
|
|
func escapeBackSlash(input string) string {
|
|
return strings.ReplaceAll(input, "/", `\/`)
|
|
}
|
|
|
|
// progresStr creates a simple progress bar
|
|
// example: =====-----
|
|
func progresStr(progress, maxProgress, maxLength int,
|
|
fill, empty string) string {
|
|
|
|
currLength := maxLength * progress / maxProgress
|
|
|
|
return fmt.Sprintf("%s%s",
|
|
strings.Repeat(fill, currLength),
|
|
strings.Repeat(empty, maxLength-currLength),
|
|
)
|
|
}
|
|
|
|
// padHex pad the neccessary 0 to create six hex digit
|
|
func padHex(r, g, b int32) string {
|
|
|
|
var result strings.Builder
|
|
|
|
for _, v := range []int32{r, g, b} {
|
|
hex := fmt.Sprintf("%x", v)
|
|
|
|
if len(hex) == 1 {
|
|
result.WriteString(fmt.Sprintf("0%s", hex))
|
|
} else {
|
|
result.WriteString(hex)
|
|
}
|
|
}
|
|
|
|
return result.String()
|
|
}
|
|
|
|
func validateHexColor(color string) bool {
|
|
reg := regexp.MustCompile(`^#([A-Fa-f0-9]{6})$`)
|
|
return reg.MatchString(color)
|
|
}
|