2015-02-18 00:43:20 +01:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
|
|
|
* cache.go
|
|
|
|
* Copyright (c) 2014, 2015 Matthieu Grieger (MIT License)
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ByAge []os.FileInfo
|
|
|
|
|
|
|
|
func (a ByAge) Len() int {
|
|
|
|
return len(a)
|
|
|
|
}
|
|
|
|
func (a ByAge) Swap(i, j int) {
|
|
|
|
a[i], a[j] = a[j], a[i]
|
|
|
|
}
|
|
|
|
func (a ByAge) Less(i, j int) bool {
|
|
|
|
return time.Since(a[i].ModTime()) < time.Since(a[j].ModTime())
|
|
|
|
}
|
|
|
|
|
|
|
|
type SongCache struct {
|
|
|
|
NumSongs int
|
|
|
|
TotalFileSize int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSongCache() *SongCache {
|
|
|
|
newCache := &SongCache{
|
|
|
|
NumSongs: 0,
|
|
|
|
TotalFileSize: 0,
|
|
|
|
}
|
|
|
|
return newCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SongCache) GetNumSongs() int {
|
2015-04-25 04:43:20 +02:00
|
|
|
songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
|
2015-02-18 00:43:20 +01:00
|
|
|
return len(songs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SongCache) GetCurrentTotalFileSize() int64 {
|
|
|
|
var totalSize int64 = 0
|
2015-04-25 04:43:20 +02:00
|
|
|
songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
|
2015-02-18 00:43:20 +01:00
|
|
|
for _, song := range songs {
|
|
|
|
totalSize += song.Size()
|
|
|
|
}
|
|
|
|
return totalSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SongCache) CheckMaximumDirectorySize() {
|
|
|
|
for c.GetCurrentTotalFileSize() > (dj.conf.Cache.MaximumSize * 1048576) {
|
|
|
|
if err := c.ClearOldest(); err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SongCache) Update() {
|
|
|
|
c.NumSongs = c.GetNumSongs()
|
|
|
|
c.TotalFileSize = c.GetCurrentTotalFileSize()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SongCache) ClearExpired() {
|
2015-04-09 03:47:39 +02:00
|
|
|
for range time.Tick(5 * time.Minute) {
|
2015-04-25 04:43:20 +02:00
|
|
|
songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
|
2015-02-18 00:43:20 +01:00
|
|
|
for _, song := range songs {
|
|
|
|
hours := time.Since(song.ModTime()).Hours()
|
2015-02-19 00:09:06 +01:00
|
|
|
if hours >= dj.conf.Cache.ExpireTime {
|
|
|
|
if dj.queue.Len() > 0 {
|
2015-04-14 08:40:39 +02:00
|
|
|
if (dj.queue.CurrentSong().Filename()) != song.Name() {
|
2015-04-25 04:43:20 +02:00
|
|
|
os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, song.Name()))
|
2015-02-19 00:09:06 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-25 04:43:20 +02:00
|
|
|
os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, song.Name()))
|
2015-02-19 00:09:06 +01:00
|
|
|
}
|
2015-02-18 00:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *SongCache) ClearOldest() error {
|
2015-04-25 04:43:20 +02:00
|
|
|
songs, _ := ioutil.ReadDir(dj.conf.Cache.CacheDir)
|
2015-02-18 00:43:20 +01:00
|
|
|
sort.Sort(ByAge(songs))
|
2015-02-19 00:09:06 +01:00
|
|
|
if dj.queue.Len() > 0 {
|
2015-04-14 08:40:39 +02:00
|
|
|
if (dj.queue.CurrentSong().Filename()) != songs[0].Name() {
|
2015-04-25 04:43:20 +02:00
|
|
|
return os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, songs[0].Name()))
|
2015-02-19 00:09:06 +01:00
|
|
|
} else {
|
|
|
|
return errors.New("Song is currently playing.")
|
|
|
|
}
|
2015-02-18 00:43:20 +01:00
|
|
|
} else {
|
2015-04-25 04:43:20 +02:00
|
|
|
return os.Remove(fmt.Sprintf("%s/%s", dj.conf.Cache.CacheDir, songs[0].Name()))
|
2015-02-18 00:43:20 +01:00
|
|
|
}
|
|
|
|
}
|