Files
RenewCMS/internal/infrastructure/useCases/variantCache.go
T
2026-10-08 02:33:32 +02:00

52 lines
1.0 KiB
Go

package useCases
import "RenewCMS/internal/domain/image"
type VariantPurgeResult struct {
Files int
Bytes int64
}
type variantCache struct {
files image.VariantFileRepository
storage image.Storage
}
func (c variantCache) purgeVariant(name string) (VariantPurgeResult, error) {
rows, err := c.files.ListByVariant(name)
if err != nil {
return VariantPurgeResult{}, err
}
return c.purge(rows)
}
func (c variantCache) purgeSource(storedName string) error {
rows, err := c.files.ListBySource(storedName)
if err != nil {
return err
}
_, err = c.purge(rows)
return err
}
func (c variantCache) purge(rows []image.VariantFile) (VariantPurgeResult, error) {
var result VariantPurgeResult
var failure error
for _, row := range rows {
if err := c.purgeRow(row); err != nil {
failure = err
continue
}
result.Files++
result.Bytes += row.Size
}
return result, failure
}
func (c variantCache) purgeRow(row image.VariantFile) error {
if err := c.storage.Delete(row.CachedName); err != nil {
return err
}
return c.files.Delete(row.ID)
}