feat/security
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m44s
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m44s
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
60
lib/boxstore/cleanup.go
Normal file
60
lib/boxstore/cleanup.go
Normal file
@@ -0,0 +1,60 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user