From 15b87e8c361bb022fea0601a657343b2e3f2771f Mon Sep 17 00:00:00 2001 From: Niklas Kapelle Date: Sat, 8 Nov 2025 23:33:40 +0100 Subject: [PATCH] added local state to cache uploads --- internal/localstate.go | 49 +++++++++++++++++++++++++++++++++++++++++ internal/steamimmich.go | 21 +++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 internal/localstate.go diff --git a/internal/localstate.go b/internal/localstate.go new file mode 100644 index 0000000..8090378 --- /dev/null +++ b/internal/localstate.go @@ -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) +} diff --git a/internal/steamimmich.go b/internal/steamimmich.go index 17a40b4..f8ea2d6 100644 --- a/internal/steamimmich.go +++ b/internal/steamimmich.go @@ -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 + } }