added revalidate cache feature

This commit is contained in:
2025-11-20 15:41:00 +01:00
parent ff2cb2335c
commit 34f55eccf0
4 changed files with 171 additions and 2 deletions

View File

@@ -4,6 +4,8 @@ import (
"fmt"
"log"
"slices"
"sort"
"strconv"
)
type Config struct {
@@ -86,3 +88,84 @@ func Run(config Config) {
return
}
}
func Revalidate(config Config) {
immichClient := newImmichHttpClient(config.APIKey)
localState, err := loadLocalState(config.CacheFile)
if err != nil {
log.Fatalf("Failed to load local state: %s", err)
return
}
hashes, err := hashFiles(localState.Assets)
if err != nil {
log.Fatalf("Failed to generate hashes: %s", err)
return
}
idxToRemoveFromCache := []int{}
rejected := 0
updated := 0
missingFromImmich := 0
missingFile := 0
// Prepare payload
var idsToCheck = []assetBulkUploadCheckItem{}
for i := range localState.Assets {
hash := hashes[i]
if hash == "" {
idxToRemoveFromCache = append(idxToRemoveFromCache, i)
missingFile += 1
} else {
idsToCheck = append(idsToCheck, assetBulkUploadCheckItem{Checksum: hashes[i], Id: strconv.Itoa(i)})
}
}
results, err := bulkCheckAssets(idsToCheck, config.BaseURL, immichClient)
if err != nil {
log.Fatalf("Failed to bulk check hashes: %s", err)
}
for _, result := range results {
i, err := strconv.Atoi(result.Id)
if err != nil {
log.Fatalf("What ?: %s", err)
return
}
asset := localState.Assets[i]
if asset.ImmichID != result.AssetId {
log.Printf("Asset %s had the wrong Immich ID. New %s", asset.FilePath, result.AssetId)
localState.Assets[i].ImmichID = result.AssetId
updated += 1
}
if result.Action == "reject" {
rejected += 1
} else {
idxToRemoveFromCache = append(idxToRemoveFromCache, i)
missingFromImmich += 1
}
}
sort.Sort(sort.Reverse(sort.IntSlice(idxToRemoveFromCache)))
for _, idx := range idxToRemoveFromCache {
localState.Assets[idx] = localState.Assets[len(localState.Assets)-1]
localState.Assets = localState.Assets[:len(localState.Assets)-1]
}
saveLocalState(config.CacheFile, *localState)
log.Printf("Revalidated %d assets", len(results))
log.Printf("Assets present on Immich: %d", rejected)
log.Printf("Wrong metadata on cache: %d", updated)
log.Printf("Missing on Immich: %d", missingFromImmich)
log.Printf("Missing on local storage: %d", missingFile)
if (updated + missingFromImmich + missingFromImmich) > 0 {
log.Printf("Fixed cache. Run the normal upload command again. If error persists try removing cache file at: %s", config.CacheFile)
}
}