Add 'CacheDir' config option

This commit is contained in:
Paul Nicholson 2015-04-24 19:43:20 -07:00
parent 23be5da322
commit d61cd8230c
5 changed files with 18 additions and 15 deletions

View file

@ -42,13 +42,13 @@ func NewSongCache() *SongCache {
} }
func (c *SongCache) GetNumSongs() int { func (c *SongCache) GetNumSongs() int {
songs, _ := ioutil.ReadDir(fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir)) songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
return len(songs) return len(songs)
} }
func (c *SongCache) GetCurrentTotalFileSize() int64 { func (c *SongCache) GetCurrentTotalFileSize() int64 {
var totalSize int64 = 0 var totalSize int64 = 0
songs, _ := ioutil.ReadDir(fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir)) songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
for _, song := range songs { for _, song := range songs {
totalSize += song.Size() totalSize += song.Size()
} }
@ -70,16 +70,16 @@ func (c *SongCache) Update() {
func (c *SongCache) ClearExpired() { func (c *SongCache) ClearExpired() {
for range time.Tick(5 * time.Minute) { for range time.Tick(5 * time.Minute) {
songs, _ := ioutil.ReadDir(fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir)) songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
for _, song := range songs { for _, song := range songs {
hours := time.Since(song.ModTime()).Hours() hours := time.Since(song.ModTime()).Hours()
if hours >= dj.conf.Cache.ExpireTime { if hours >= dj.conf.Cache.ExpireTime {
if dj.queue.Len() > 0 { if dj.queue.Len() > 0 {
if (dj.queue.CurrentSong().Filename()) != song.Name() { if (dj.queue.CurrentSong().Filename()) != song.Name() {
os.Remove(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, song.Name())) os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, song.Name()))
} }
} else { } else {
os.Remove(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, song.Name())) os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, song.Name()))
} }
} }
} }
@ -87,15 +87,15 @@ func (c *SongCache) ClearExpired() {
} }
func (c *SongCache) ClearOldest() error { func (c *SongCache) ClearOldest() error {
songs, _ := ioutil.ReadDir(fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir)) songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
sort.Sort(ByAge(songs)) sort.Sort(ByAge(songs))
if dj.queue.Len() > 0 { if dj.queue.Len() > 0 {
if (dj.queue.CurrentSong().Filename()) != songs[0].Name() { if (dj.queue.CurrentSong().Filename()) != songs[0].Name() {
return os.Remove(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, songs[0].Name())) return os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, songs[0].Name()))
} else { } else {
return errors.New("Song is currently playing.") return errors.New("Song is currently playing.")
} }
} else { } else {
return os.Remove(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, songs[0].Name())) return os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, songs[0].Name()))
} }
} }

View file

@ -443,11 +443,10 @@ func kill() {
// Deletes songs from ~/.mumbledj/songs. // Deletes songs from ~/.mumbledj/songs.
func deleteSongs() error { func deleteSongs() error {
songsDir := fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir) if err := os.RemoveAll(dj.conf.Cache.CacheDir); err != nil {
if err := os.RemoveAll(songsDir); err != nil {
return errors.New("An error occurred while deleting the audio files.") return errors.New("An error occurred while deleting the audio files.")
} else { } else {
if err := os.Mkdir(songsDir, 0777); err != nil { if err := os.Mkdir(dj.conf.Cache.CacheDir, 0777); err != nil {
return errors.New("An error occurred while recreating the songs directory.") return errors.New("An error occurred while recreating the songs directory.")
} }
return nil return nil

View file

@ -40,6 +40,9 @@ MaximumSize = 512
# DEFAULT VALUE: 24 # DEFAULT VALUE: 24
ExpireTime = 24 ExpireTime = 24
# Directory to store cached songs
# DEFAULT VALUE: ~/.mumbledj/songs
CacheDir = ~/.mumbledj/songs
[Volume] [Volume]

View file

@ -27,6 +27,7 @@ type DjConfig struct {
Enabled bool Enabled bool
MaximumSize int64 MaximumSize int64
ExpireTime float64 ExpireTime float64
CacheDir string
} }
Volume struct { Volume struct {
DefaultVolume float32 DefaultVolume float32

View file

@ -88,8 +88,8 @@ func NewYouTubeSong(user, id string, playlist *YouTubePlaylist) (*YouTubeSong, e
// Download downloads the song via youtube-dl if it does not already exist on disk. // Download downloads the song via youtube-dl if it does not already exist on disk.
// All downloaded songs are stored in ~/.mumbledj/songs and should be automatically cleaned. // All downloaded songs are stored in ~/.mumbledj/songs and should be automatically cleaned.
func (s *YouTubeSong) Download() error { func (s *YouTubeSong) Download() error {
if _, err := os.Stat(fmt.Sprintf("%s/.mumbledj/songs/%s", dj.homeDir, s.Filename())); os.IsNotExist(err) { if _, err := os.Stat(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, s.Filename())); os.IsNotExist(err) {
cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`~/.mumbledj/songs/%s`, s.Filename()), "--format", "m4a", "--", s.ID()) cmd := exec.Command("youtube-dl", "--output", fmt.Sprintf(`%s/%s`, dj.conf.Cache.CacheDir, s.Filename()), "--format", "m4a", "--", s.ID())
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
if dj.conf.Cache.Enabled { if dj.conf.Cache.Enabled {
dj.cache.CheckMaximumDirectorySize() dj.cache.CheckMaximumDirectorySize()
@ -104,7 +104,7 @@ func (s *YouTubeSong) Download() error {
// Play plays the song. Once the song is playing, a notification is displayed in a text message that features the video // Play plays the song. Once the song is playing, a notification is displayed in a text message that features the video
// thumbnail, URL, title, duration, and submitter. // thumbnail, URL, title, duration, and submitter.
func (s *YouTubeSong) Play() { func (s *YouTubeSong) Play() {
if err := dj.audioStream.Play(fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.ID()), dj.queue.OnSongFinished); err != nil { if err := dj.audioStream.Play(fmt.Sprintf("%s/%s.m4a", dj.conf.Cache.CacheDir, s.ID()), dj.queue.OnSongFinished); err != nil {
panic(err) panic(err)
} else { } else {
if s.Playlist() == nil { if s.Playlist() == nil {
@ -149,7 +149,7 @@ func (s *YouTubeSong) Play() {
// Delete deletes the song from ~/.mumbledj/songs if the cache is disabled. // Delete deletes the song from ~/.mumbledj/songs if the cache is disabled.
func (s *YouTubeSong) Delete() error { func (s *YouTubeSong) Delete() error {
if dj.conf.Cache.Enabled == false { if dj.conf.Cache.Enabled == false {
filePath := fmt.Sprintf("%s/.mumbledj/songs/%s.m4a", dj.homeDir, s.ID()) filePath := fmt.Sprintf("%s/%s.m4a", dj.conf.Cache.CacheDir, s.ID())
if _, err := os.Stat(filePath); err == nil { if _, err := os.Stat(filePath); err == nil {
if err := os.Remove(filePath); err == nil { if err := os.Remove(filePath); err == nil {
return nil return nil