-
Notifications
You must be signed in to change notification settings - Fork 0
/
expiry.go
69 lines (58 loc) · 1.43 KB
/
expiry.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
package main
import (
"time"
"github.com/d-a-s-h-o/bin404/expiry"
"github.com/dustin/go-humanize"
)
var defaultExpiryList = []uint64{
60,
300,
3600,
86400,
604800,
2419200,
31536000,
}
type ExpirationTime struct {
Seconds uint64
Human string
}
// Determine if the given filename is expired
func isFileExpired(filename string) (bool, error) {
metadata, err := storageBackend.Head(filename)
if err != nil {
return false, err
}
return expiry.IsTsExpired(metadata.Expiry), nil
}
// Return a list of expiration times and their humanized versions
func listExpirationTimes() []ExpirationTime {
epoch := time.Now()
actualExpiryInList := false
var expiryList []ExpirationTime
for _, expiryEntry := range defaultExpiryList {
if Config.maxExpiry == 0 || expiryEntry <= Config.maxExpiry {
if expiryEntry == Config.maxExpiry {
actualExpiryInList = true
}
duration := time.Duration(expiryEntry) * time.Second
expiryList = append(expiryList, ExpirationTime{
Seconds: expiryEntry,
Human: humanize.RelTime(epoch, epoch.Add(duration), "", ""),
})
}
}
if Config.maxExpiry == 0 {
expiryList = append(expiryList, ExpirationTime{
0,
"never",
})
} else if actualExpiryInList == false {
duration := time.Duration(Config.maxExpiry) * time.Second
expiryList = append(expiryList, ExpirationTime{
Config.maxExpiry,
humanize.RelTime(epoch, epoch.Add(duration), "", ""),
})
}
return expiryList
}