added local state to cache uploads

This commit is contained in:
2025-11-08 23:33:40 +01:00
parent 4d772878bc
commit 15b87e8c36
2 changed files with 69 additions and 1 deletions

49
internal/localstate.go Normal file
View File

@@ -0,0 +1,49 @@
package steamimmich
import (
"encoding/json"
"errors"
"io"
"os"
)
type AssetState struct {
FilePath string
ImmichID string
}
type LocalState struct {
Assets []AssetState
}
func loadLocalState(filename string) (*LocalState, error) {
file, err := os.Open(filename)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return &LocalState{Assets: []AssetState{}}, nil
}
return nil, err
}
content, err := io.ReadAll(file)
if err != nil {
return nil, err
}
var data LocalState
err = json.Unmarshal(content, &data)
if err != nil {
return nil, err
}
return &data, nil
}
func saveLocalState(filename string, state LocalState) error {
content, err := json.Marshal(state)
if err != nil {
return err
}
return os.WriteFile(filename, content, 0644)
}

View File

@@ -3,6 +3,7 @@ package steamimmich
import (
"fmt"
"log"
"slices"
)
type Config struct {
@@ -28,6 +29,11 @@ func Run(config Config) {
immichClient := newImmichHttpClient(config.APIKey)
localState, err := loadLocalState("./state.json")
if err != nil {
log.Fatalf("Failed to load local state: %s", err)
}
gameNameCache := make(map[string]string)
for appid, files := range screenshotFiles {
@@ -35,6 +41,14 @@ func Run(config Config) {
assetsInGame := make([]string, 0)
for _, filepath := range files {
idx := slices.IndexFunc(localState.Assets, func(e AssetState) bool { return e.FilePath == filepath })
if idx != -1 {
log.Printf("Asset already uploaded: %s", filepath)
continue
}
res, err := uploadToImmich(filepath, appid, config.BaseURL, config.DeiveID, &immichClient)
if err != nil {
@@ -44,7 +58,7 @@ func Run(config Config) {
log.Printf("Assest uploaded: %s (%s) %s with ID: %s", gameName, appid, filepath, res.ID)
return
localState.Assets = append(localState.Assets, AssetState{FilePath: filepath, ImmichID: res.ID})
assetsInGame = append(assetsInGame, res.ID)
}
@@ -67,4 +81,9 @@ func Run(config Config) {
}
log.Println("Finished uploading screenshots")
err = saveLocalState("./state.json", *localState)
if err != nil {
log.Fatalf("Failed to save local cache: %s", err)
return
}
}