2020-05-14 02:37:33 +02:00
|
|
|
package cleanup
|
2017-05-02 06:25:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-05-14 02:37:33 +02:00
|
|
|
"time"
|
2017-05-02 06:25:56 +02:00
|
|
|
|
|
|
|
"github.com/andreimarcu/linx-server/backends/localfs"
|
|
|
|
"github.com/andreimarcu/linx-server/expiry"
|
|
|
|
)
|
|
|
|
|
2020-05-14 02:37:33 +02:00
|
|
|
func Cleanup(filesDir string, metaDir string, noLogs bool) {
|
2019-01-25 08:33:11 +01:00
|
|
|
fileBackend := localfs.NewLocalfsBackend(metaDir, filesDir)
|
2017-05-02 06:25:56 +02:00
|
|
|
|
2019-01-25 08:33:11 +01:00
|
|
|
files, err := fileBackend.List()
|
2017-05-02 06:25:56 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, filename := range files {
|
2019-01-25 08:33:11 +01:00
|
|
|
metadata, err := fileBackend.Head(filename)
|
2017-05-02 06:25:56 +02:00
|
|
|
if err != nil {
|
2018-02-25 23:46:20 +01:00
|
|
|
if !noLogs {
|
|
|
|
log.Printf("Failed to find metadata for %s", filename)
|
|
|
|
}
|
2017-05-02 06:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if expiry.IsTsExpired(metadata.Expiry) {
|
2018-02-25 23:46:20 +01:00
|
|
|
if !noLogs {
|
|
|
|
log.Printf("Delete %s", filename)
|
|
|
|
}
|
2017-05-02 06:25:56 +02:00
|
|
|
fileBackend.Delete(filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 02:37:33 +02:00
|
|
|
|
|
|
|
func PeriodicCleanup(minutes time.Duration, filesDir string, metaDir string, noLogs bool) {
|
|
|
|
c := time.Tick(minutes)
|
|
|
|
for range c {
|
|
|
|
Cleanup(filesDir, metaDir, noLogs)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|