gomu/utils.go

119 lines
1.8 KiB
Go
Raw Normal View History

2020-06-22 00:05:56 +08:00
// Copyright (C) 2020 Raziman
2020-06-19 16:22:20 +08:00
package main
2020-06-24 11:34:20 +08:00
import (
2020-06-26 12:54:48 +08:00
"net/http"
2020-06-24 11:34:20 +08:00
"os"
2020-06-25 14:12:23 +08:00
"path"
2020-06-24 11:34:20 +08:00
"strings"
"time"
2020-06-26 12:54:48 +08:00
"github.com/faiface/beep/mp3"
2020-06-24 11:34:20 +08:00
)
2020-06-19 16:22:20 +08:00
func log(text string) {
f, err := os.OpenFile("message.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
2020-06-21 23:47:12 +08:00
if _, err := f.Write([]byte(text + "\n")); err != nil {
2020-06-19 16:22:20 +08:00
panic(err)
}
if err := f.Close(); err != nil {
panic(err)
}
}
2020-06-24 11:34:20 +08:00
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, ":") {
2020-06-26 12:54:48 +08:00
2020-06-24 11:34:20 +08:00
if len(v) < 2 {
2020-06-26 12:54:48 +08:00
result = append(result, "0"+v)
2020-06-24 11:34:20 +08:00
} else {
result = append(result, v)
}
}
return strings.Join(result, ":")
}
2020-06-25 14:12:23 +08:00
func expandTilde(_path string) string {
if !strings.HasPrefix(_path, "~") {
return _path
}
2020-06-26 12:54:48 +08:00
home, err := os.UserHomeDir()
2020-06-25 14:12:23 +08:00
if err != nil {
log(err.Error())
}
return path.Join(home, strings.TrimPrefix(_path, "~"))
}
2020-06-26 12:54:48 +08:00
// gets the length of the song in the queue
func GetLength(audioPath string) (time.Duration, error) {
f, err := os.Open(audioPath)
defer f.Close()
if err != nil {
return 0, err
}
streamer, format, err := mp3.Decode(f)
defer streamer.Close()
if err != nil {
return 0, err
}
return format.SampleRate.D(streamer.Len()), nil
}
// detects the filetype of file
func GetFileContentType(out *os.File) (string, error) {
buffer := make([]byte, 512)
_, err := out.Read(buffer)
if err != nil {
return "", 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), path.Ext(fn))
}