2015-09-28 04:17:12 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2016-09-19 06:45:00 +02:00
|
|
|
|
2017-05-02 06:25:56 +02:00
|
|
|
"github.com/andreimarcu/linx-server/expiry"
|
2016-09-19 06:45:00 +02:00
|
|
|
"github.com/dustin/go-humanize"
|
2015-09-28 04:17:12 +02:00
|
|
|
)
|
|
|
|
|
2016-09-19 06:45:00 +02:00
|
|
|
var defaultExpiryList = []uint64{
|
|
|
|
60,
|
|
|
|
300,
|
|
|
|
3600,
|
|
|
|
86400,
|
|
|
|
604800,
|
|
|
|
2419200,
|
|
|
|
31536000,
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExpirationTime struct {
|
|
|
|
Seconds uint64
|
|
|
|
Human string
|
|
|
|
}
|
|
|
|
|
2015-09-28 04:17:12 +02:00
|
|
|
// Determine if the given filename is expired
|
2015-10-08 04:45:34 +02:00
|
|
|
func isFileExpired(filename string) (bool, error) {
|
2019-01-25 08:33:11 +01:00
|
|
|
metadata, err := storageBackend.Head(filename)
|
2015-10-08 04:45:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-05-02 06:25:56 +02:00
|
|
|
return expiry.IsTsExpired(metadata.Expiry), nil
|
2015-09-28 04:17:12 +02:00
|
|
|
}
|
2016-09-19 06:45:00 +02:00
|
|
|
|
|
|
|
// Return a list of expiration times and their humanized versions
|
|
|
|
func listExpirationTimes() []ExpirationTime {
|
|
|
|
epoch := time.Now()
|
|
|
|
actualExpiryInList := false
|
|
|
|
var expiryList []ExpirationTime
|
|
|
|
|
2017-05-02 06:25:56 +02:00
|
|
|
for _, expiryEntry := range defaultExpiryList {
|
|
|
|
if Config.maxExpiry == 0 || expiryEntry <= Config.maxExpiry {
|
|
|
|
if expiryEntry == Config.maxExpiry {
|
2016-09-19 06:45:00 +02:00
|
|
|
actualExpiryInList = true
|
|
|
|
}
|
|
|
|
|
2017-05-02 06:25:56 +02:00
|
|
|
duration := time.Duration(expiryEntry) * time.Second
|
2016-09-19 06:45:00 +02:00
|
|
|
expiryList = append(expiryList, ExpirationTime{
|
2017-05-02 06:25:56 +02:00
|
|
|
Seconds: expiryEntry,
|
|
|
|
Human: humanize.RelTime(epoch, epoch.Add(duration), "", ""),
|
2016-09-19 06:45:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|