music-library/library.go
2023-02-14 21:59:56 +01:00

56 lines
1.1 KiB
Go

package main
import (
"os"
"path/filepath"
"git.milar.in/milarin/slices"
)
type Playlist struct {
Name string `json:"name"`
Songs []string `json:"songs"`
}
func GetAll() ([]Playlist, error) {
playlistNames, err := GetPlaylists()
if err != nil {
return nil, err
}
playlists := make([]Playlist, 0, len(playlistNames))
for _, playlistName := range playlistNames {
songs, err := GetSongs(playlistName)
if err != nil {
return nil, err
}
playlists = append(playlists, Playlist{
Name: playlistName,
Songs: songs,
})
}
return playlists, nil
}
func GetPlaylists() ([]string, error) {
entries, err := os.ReadDir(LibraryPath)
if err != nil {
return nil, err
}
directories := slices.Filter(entries, And(IsDir, Not(IsHidden)))
return slices.Map(directories, FsEntry2Name), nil
}
func GetSongs(playlist string) ([]string, error) {
entries, err := os.ReadDir(filepath.Join(LibraryPath, playlist))
if err != nil {
return nil, err
}
directories := slices.Filter(entries, Not(IsDir))
return slices.Map(directories, FsEntry2Name), nil
}