61 lines
1.1 KiB
Go
61 lines
1.1 KiB
Go
|
|
package boxstore
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"os"
|
||
|
|
)
|
||
|
|
|
||
|
|
type CleanupExpiredResult struct {
|
||
|
|
Scanned int
|
||
|
|
Deleted int
|
||
|
|
Skipped int
|
||
|
|
DeletedIDs []string
|
||
|
|
Warnings []string
|
||
|
|
}
|
||
|
|
|
||
|
|
func CleanupExpiredBoxes() (CleanupExpiredResult, error) {
|
||
|
|
entries, err := os.ReadDir(uploadRoot)
|
||
|
|
if err != nil {
|
||
|
|
if os.IsNotExist(err) {
|
||
|
|
return CleanupExpiredResult{}, nil
|
||
|
|
}
|
||
|
|
return CleanupExpiredResult{}, err
|
||
|
|
}
|
||
|
|
|
||
|
|
result := CleanupExpiredResult{
|
||
|
|
DeletedIDs: make([]string, 0),
|
||
|
|
Warnings: make([]string, 0),
|
||
|
|
}
|
||
|
|
for _, entry := range entries {
|
||
|
|
if !entry.IsDir() {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
boxID := entry.Name()
|
||
|
|
if !ValidBoxID(boxID) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
result.Scanned++
|
||
|
|
|
||
|
|
manifest, err := ReadManifest(boxID)
|
||
|
|
if err != nil {
|
||
|
|
result.Skipped++
|
||
|
|
if !os.IsNotExist(err) {
|
||
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("%s: %v", boxID, err))
|
||
|
|
}
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if !IsExpired(manifest) {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if err := DeleteBox(boxID); err != nil {
|
||
|
|
result.Skipped++
|
||
|
|
result.Warnings = append(result.Warnings, fmt.Sprintf("%s: %v", boxID, err))
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
result.Deleted++
|
||
|
|
result.DeletedIDs = append(result.DeletedIDs, boxID)
|
||
|
|
}
|
||
|
|
|
||
|
|
return result, nil
|
||
|
|
}
|