refactor(code): Cleaned-up the code base
This commit is contained in:
74
lib/boxstore/retention.go
Normal file
74
lib/boxstore/retention.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package boxstore
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"warpbox/lib/models"
|
||||
)
|
||||
|
||||
const OneTimeDownloadRetentionKey = "one-time"
|
||||
|
||||
var oneTimeDownloadExpiry int64
|
||||
|
||||
var retentionOptions = []models.RetentionOption{
|
||||
{Key: "10s", Label: "10 seconds", Seconds: 10},
|
||||
{Key: "10m", Label: "10 minutes", Seconds: 10 * 60},
|
||||
{Key: "1h", Label: "1 hour", Seconds: 60 * 60},
|
||||
{Key: "12h", Label: "12 hours", Seconds: 12 * 60 * 60},
|
||||
{Key: "24h", Label: "24 hours", Seconds: 24 * 60 * 60},
|
||||
{Key: "48h", Label: "48 hours", Seconds: 48 * 60 * 60},
|
||||
{Key: OneTimeDownloadRetentionKey, Label: "One time download", Seconds: 0},
|
||||
}
|
||||
|
||||
func RetentionOptions() []models.RetentionOption {
|
||||
options := make([]models.RetentionOption, len(retentionOptions))
|
||||
copy(options, retentionOptions)
|
||||
return options
|
||||
}
|
||||
|
||||
func DefaultRetentionOption() models.RetentionOption {
|
||||
return retentionOptions[0]
|
||||
}
|
||||
func SetOneTimeDownloadExpiry(seconds int64) {
|
||||
oneTimeDownloadExpiry = seconds
|
||||
}
|
||||
|
||||
func OneTimeDownloadExpiry() int64 {
|
||||
return oneTimeDownloadExpiry
|
||||
}
|
||||
func normalizeRetentionOption(key string) models.RetentionOption {
|
||||
for _, option := range retentionOptions {
|
||||
if option.Key == key {
|
||||
return option
|
||||
}
|
||||
}
|
||||
|
||||
return DefaultRetentionOption()
|
||||
}
|
||||
|
||||
func startRetentionIfTerminalUnlocked(manifest *models.BoxManifest) {
|
||||
if !manifest.ExpiresAt.IsZero() || len(manifest.Files) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
seconds := manifest.RetentionSecs
|
||||
if manifest.OneTimeDownload {
|
||||
seconds = oneTimeDownloadExpiry
|
||||
} else if seconds <= 0 {
|
||||
seconds = normalizeRetentionOption(manifest.RetentionKey).Seconds
|
||||
}
|
||||
|
||||
if seconds <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range manifest.Files {
|
||||
if file.Status != models.FileStatusReady && file.Status != models.FileStatusFailed {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Retention starts after uploads settle so slow or very large uploads do
|
||||
// not expire before users get a real chance to open the box.
|
||||
manifest.ExpiresAt = time.Now().UTC().Add(time.Duration(seconds) * time.Second)
|
||||
}
|
||||
Reference in New Issue
Block a user