musicutil/types/file.go
2022-02-22 14:02:58 +00:00

68 lines
1.5 KiB
Go

package types
import (
"fmt"
"path"
"path/filepath"
"strings"
)
type Tags struct {
Title string `json:"title"`
Artist string `json:"artist"`
}
func (tags *Tags) DeepCopy() *Tags {
return &Tags{
Title: tags.Title,
Artist: tags.Artist,
}
}
type File struct {
Filename string `json:"filename"`
Extension string `json:"extension"`
// The path to the actual file from current dir
PathTo string `json:"path_to"`
// The path of the file relative to the source directory
PathFromSource string `json:"path_from_source"`
Tags *Tags `json:"tags"`
}
func FileFromPath(sourceDir string, fullPath string) *File {
path_to := filepath.Dir(fullPath)
path_from_src, _ := filepath.Rel(sourceDir, filepath.Dir(fullPath))
filename := strings.TrimSuffix(path.Base(fullPath), path.Ext(fullPath))
extension := strings.TrimPrefix(path.Ext(fullPath), ".")
return &File{
PathTo: path_to,
PathFromSource: path_from_src,
Filename: filename,
Extension: extension,
Tags: &Tags{Title: "", Artist: ""},
}
}
func (f *File) JoinFilename() string {
return fmt.Sprintf("%s.%s", f.Filename, f.Extension)
}
func (f *File) JoinPathTo() string {
return fmt.Sprintf("%s/%s", f.PathTo, f.JoinFilename())
}
func (f *File) JoinPathFromSource() string {
return fmt.Sprintf("%s/%s", f.PathFromSource, f.JoinFilename())
}
func (f *File) DeepCopy() *File {
return &File{
Filename: f.Filename,
Extension: f.Extension,
PathTo: f.PathTo,
PathFromSource: f.PathFromSource,
Tags: f.Tags.DeepCopy(),
}
}