2015-09-27 22:17:12 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2016-09-19 00:45:00 -04:00
|
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
2015-09-27 22:17:12 -04:00
|
|
|
)
|
|
|
|
|
2016-09-19 00:45:00 -04:00
|
|
|
var defaultExpiryList = []uint64{
|
|
|
|
60,
|
|
|
|
300,
|
|
|
|
3600,
|
|
|
|
86400,
|
|
|
|
604800,
|
|
|
|
2419200,
|
|
|
|
31536000,
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExpirationTime struct {
|
|
|
|
Seconds uint64
|
|
|
|
Human string
|
|
|
|
}
|
|
|
|
|
2015-10-07 01:15:45 -04:00
|
|
|
var neverExpire = time.Unix(0, 0)
|
|
|
|
|
2015-09-27 22:17:12 -04:00
|
|
|
// Determine if a file with expiry set to "ts" has expired yet
|
2015-10-06 02:50:20 -04:00
|
|
|
func isTsExpired(ts time.Time) bool {
|
|
|
|
now := time.Now()
|
2015-10-07 01:15:45 -04:00
|
|
|
return ts != neverExpire && now.After(ts)
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if the given filename is expired
|
2015-10-07 22:45:34 -04:00
|
|
|
func isFileExpired(filename string) (bool, error) {
|
|
|
|
metadata, err := metadataRead(filename)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return isTsExpired(metadata.Expiry), nil
|
2015-09-27 22:17:12 -04:00
|
|
|
}
|
2016-09-19 00:45:00 -04:00
|
|
|
|
|
|
|
// Return a list of expiration times and their humanized versions
|
|
|
|
func listExpirationTimes() []ExpirationTime {
|
|
|
|
epoch := time.Now()
|
|
|
|
actualExpiryInList := false
|
|
|
|
var expiryList []ExpirationTime
|
|
|
|
|
|
|
|
for _, expiry := range defaultExpiryList {
|
|
|
|
if Config.maxExpiry == 0 || expiry <= Config.maxExpiry {
|
|
|
|
if expiry == Config.maxExpiry {
|
|
|
|
actualExpiryInList = true
|
|
|
|
}
|
|
|
|
|
|
|
|
duration := time.Duration(expiry) * time.Second
|
|
|
|
expiryList = append(expiryList, ExpirationTime{
|
|
|
|
expiry,
|
|
|
|
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
|
|
|
|
}
|