Handle tracks
This commit is contained in:
parent
5799eb32ef
commit
5c7841fdc6
7 changed files with 343 additions and 52 deletions
88
model/track.go
Normal file
88
model/track.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package reveil
|
||||
|
||||
import (
|
||||
"crypto/sha512"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.nemunai.re/nemunaire/reveil/config"
|
||||
)
|
||||
|
||||
type Track struct {
|
||||
Id []byte `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Path string `json:"path"`
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
func LoadTracks(cfg *config.Config) (tracks []*Track, err error) {
|
||||
err = filepath.Walk(cfg.TracksDir, func(path string, d fs.FileInfo, err error) error {
|
||||
if d.Mode().IsRegular() {
|
||||
hash := sha512.Sum512([]byte(path))
|
||||
tracks = append(tracks, &Track{
|
||||
Id: hash[:63],
|
||||
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
||||
Path: path,
|
||||
Enabled: len(strings.Split(path, "/")) == 2,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (t *Track) Rename(newName string) error {
|
||||
newPath := filepath.Join(filepath.Dir(t.Path), newName+filepath.Ext(t.Path))
|
||||
|
||||
err := os.Rename(
|
||||
t.Path,
|
||||
newPath,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.Path = newPath
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Track) MoveTo(path string) error {
|
||||
os.Mkdir(filepath.Dir(path), 0755)
|
||||
|
||||
err := os.Rename(
|
||||
t.Path,
|
||||
path,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
t.Path = path
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Track) Disable() error {
|
||||
if t.Enabled {
|
||||
date := time.Now()
|
||||
return t.MoveTo(filepath.Join(filepath.Dir(t.Path), date.Format("20060102"), filepath.Base(t.Path)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Track) Enable(cfg *config.Config) error {
|
||||
if !t.Enabled {
|
||||
return t.MoveTo(filepath.Join(cfg.TracksDir, filepath.Base(t.Path)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Track) Remove() error {
|
||||
return os.Remove(t.Path)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue