added local state to cache uploads
This commit is contained in:
49
internal/localstate.go
Normal file
49
internal/localstate.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user