-
Notifications
You must be signed in to change notification settings - Fork 2
/
recent.go
95 lines (80 loc) · 1.97 KB
/
recent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"context"
"log"
"os"
"path/filepath"
"strings"
"time"
)
const cleanupInterval = 30 * time.Second
type recent struct {
URL string
Title string
Artist string
Description string
Timestamp time.Time
}
func GetRecentURLs(ctx context.Context, webRoot, outPath string, cmdTimeout time.Duration) ([]recent, error) {
recentURLs := make([]recent, 0)
files, err := os.ReadDir(filepath.Join(webRoot, outPath))
if err != nil {
return nil, err
}
for _, file := range files {
if !file.IsDir() && file.Name() != ".README" && !strings.HasSuffix(file.Name(), ".json") {
ff, err := runFFprobe(ctx, FFprobeCmd, filepath.Join(webRoot, outPath, file.Name()), cmdTimeout)
if err != nil {
log.Printf("ffprobe error %s\n", err)
continue
}
r := recent{}
r.URL = filepath.Join(outPath, file.Name())
r.Title, r.Artist, r.Description = titleArtistDescription(ff)
i, err := file.Info()
if err != nil {
log.Print(err)
continue
}
r.Timestamp = i.ModTime()
recentURLs = append(recentURLs, r)
}
}
return recentURLs, nil
}
func DeleteFiles(urls []string, webRoot string) error {
for _, u := range urls {
path := filepath.Join(webRoot, u)
if err := os.Remove(path); err != nil {
return err
}
log.Printf("file removed: %s\n", path)
}
return nil
}
func fileCleanup(outPath string, expiry time.Duration) {
visit := func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
if f.IsDir() || strings.HasPrefix(f.Name(), ".") {
return nil
}
// if last modification time is prior to expiry time,
// then delete the file
if time.Since(f.ModTime()) > expiry {
if err := os.Remove(path); err != nil {
return err
}
log.Printf("old file removed: %s\n", path)
}
return nil
}
tickChan := time.NewTicker(cleanupInterval).C
for _ = range tickChan {
err := filepath.Walk(outPath, visit)
if err != nil {
log.Printf("file cleanup error: %s\n", err)
}
}
}