mirror of
https://github.com/skidoodle/safebin.git
synced 2026-04-28 11:17:42 +02:00
+58
-19
@@ -8,43 +8,82 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
cleanupInterval = 1 * time.Hour
|
||||
tempExpiry = 4 * time.Hour
|
||||
minRetention = 24 * time.Hour
|
||||
maxRetention = 365 * 24 * time.Hour
|
||||
bytesInMB = 1 << 20
|
||||
)
|
||||
|
||||
func (app *App) StartCleanupTask(ctx context.Context) {
|
||||
ticker := time.NewTicker(1 * time.Hour)
|
||||
ticker := time.NewTicker(cleanupInterval)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
ticker.Stop()
|
||||
return
|
||||
case <-ticker.C:
|
||||
app.CleanDir(app.Conf.StorageDir, false)
|
||||
app.CleanDir(filepath.Join(app.Conf.StorageDir, "tmp"), true)
|
||||
app.CleanStorage(app.Conf.StorageDir)
|
||||
app.CleanTemp(filepath.Join(app.Conf.StorageDir, "tmp"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (app *App) CleanDir(path string, isTmp bool) {
|
||||
entries, _ := os.ReadDir(path)
|
||||
func (app *App) CleanStorage(path string) {
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
app.Logger.Error("Failed to read storage dir", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
info, _ := entry.Info()
|
||||
expiry := 4 * time.Hour
|
||||
if !isTmp {
|
||||
expiry = CalculateRetention(info.Size(), app.Conf.MaxMB)
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
expiry := CalculateRetention(info.Size(), app.Conf.MaxMB)
|
||||
|
||||
if time.Since(info.ModTime()) > expiry {
|
||||
os.RemoveAll(filepath.Join(path, entry.Name()))
|
||||
if err := os.RemoveAll(filepath.Join(path, entry.Name())); err != nil {
|
||||
app.Logger.Error("Failed to remove expired file", "path", entry.Name(), "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CalculateRetention(fileSize int64, maxMB int64) time.Duration {
|
||||
const (
|
||||
minAge = 24 * time.Hour
|
||||
maxAge = 365 * 24 * time.Hour
|
||||
)
|
||||
ratio := math.Max(0, math.Min(1, float64(fileSize)/float64(maxMB<<20)))
|
||||
retention := float64(maxAge) * math.Pow(1.0-ratio, 3)
|
||||
if retention < float64(minAge) {
|
||||
return minAge
|
||||
func (app *App) CleanTemp(path string) {
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
app.Logger.Error("Failed to read temp dir", "err", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if time.Since(info.ModTime()) > tempExpiry {
|
||||
if err := os.RemoveAll(filepath.Join(path, entry.Name())); err != nil {
|
||||
app.Logger.Error("Failed to remove expired temp file", "path", entry.Name(), "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CalculateRetention(fileSize, maxMB int64) time.Duration {
|
||||
ratio := math.Max(0, math.Min(1, float64(fileSize)/float64(maxMB*bytesInMB)))
|
||||
|
||||
invRatio := 1.0 - ratio
|
||||
retention := float64(maxRetention) * (invRatio * invRatio * invRatio)
|
||||
|
||||
if retention < float64(minRetention) {
|
||||
return minRetention
|
||||
}
|
||||
|
||||
return time.Duration(retention)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user