diff --git a/internal/immich.go b/internal/immich.go index 068f681..23f28b6 100644 --- a/internal/immich.go +++ b/internal/immich.go @@ -28,7 +28,27 @@ type bulkIdResponseDto struct { Success bool `json:"success"` } -func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadResponse, error) { +type APIKeyTransport struct { + APIKey string +} + +func (t *APIKeyTransport) RoundTrip(req *http.Request) (*http.Response, error) { + req = req.Clone(req.Context()) + req.Header.Add("x-api-key", t.APIKey) + return http.DefaultTransport.RoundTrip(req) +} + +func newImmichHttpClient(apiKey string) http.Client { + transport := APIKeyTransport{ + APIKey: apiKey, + } + + return http.Client{ + Transport: &transport, + } +} + +func uploadToImmich(filePath, appID, baseURL, deviceID string, httpClient *http.Client) (*uploadResponse, error) { file, err := os.Open(filePath) if err != nil { return nil, fmt.Errorf("open file: %w", err) @@ -66,12 +86,11 @@ func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadR if err != nil { return nil, fmt.Errorf("create request: %w", err) } - req.Header.Set("x-api-key", apiKey) + req.Header.Set("Content-Type", writer.FormDataContentType()) req.Header.Set("Accept", "application/json") - client := &http.Client{Timeout: 60 * time.Second} - resp, err := client.Do(req) + resp, err := httpClient.Do(req) if err != nil { return nil, fmt.Errorf("POST to Immich: %w", err) } @@ -90,7 +109,7 @@ func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadR return &result, nil } -func setAssetMetadata(assets []string, description, baseURL, apiKey string) error { +func setAssetMetadata(assets []string, description, baseURL string, httpClient http.Client) error { body, _ := json.Marshal(assetPutRequest{ Ids: assets, Description: description, @@ -101,11 +120,10 @@ func setAssetMetadata(assets []string, description, baseURL, apiKey string) erro return err } - req.Header.Set("x-api-key", apiKey) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") - _, err = http.DefaultClient.Do(req) + _, err = httpClient.Do(req) if err != nil { return err } @@ -113,7 +131,7 @@ func setAssetMetadata(assets []string, description, baseURL, apiKey string) erro return nil } -func addAssetsToAlbum(assets []string, album, baseURL, apiKey string) error { +func addAssetsToAlbum(assets []string, album, baseURL string, httpClient http.Client) error { body, _ := json.Marshal(struct { IDs []string `json:"ids"` }{IDs: assets}) @@ -123,11 +141,10 @@ func addAssetsToAlbum(assets []string, album, baseURL, apiKey string) error { return err } - req.Header.Set("x-api-key", apiKey) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") - res, err := http.DefaultClient.Do(req) + res, err := httpClient.Do(req) if err != nil { return err } diff --git a/internal/steamimmich.go b/internal/steamimmich.go index 26e6792..17a40b4 100644 --- a/internal/steamimmich.go +++ b/internal/steamimmich.go @@ -26,6 +26,8 @@ func Run(config Config) { return } + immichClient := newImmichHttpClient(config.APIKey) + gameNameCache := make(map[string]string) for appid, files := range screenshotFiles { @@ -33,7 +35,7 @@ func Run(config Config) { assetsInGame := make([]string, 0) for _, filepath := range files { - res, err := uploadToImmich(filepath, appid, config.BaseURL, config.APIKey, config.DeiveID) + res, err := uploadToImmich(filepath, appid, config.BaseURL, config.DeiveID, &immichClient) if err != nil { log.Fatalf("Failed to upload to immich: %s", err) @@ -42,17 +44,19 @@ func Run(config Config) { log.Printf("Assest uploaded: %s (%s) %s with ID: %s", gameName, appid, filepath, res.ID) + return + assetsInGame = append(assetsInGame, res.ID) } - err := setAssetMetadata(assetsInGame, fmt.Sprintf("Game: %s", gameName), config.BaseURL, config.APIKey) + err := setAssetMetadata(assetsInGame, fmt.Sprintf("Game: %s", gameName), config.BaseURL, immichClient) if err != nil { log.Fatalf("Failed to set asset metadata: %s", err) return } if config.Album != "" { - err = addAssetsToAlbum(assetsInGame, config.Album, config.BaseURL, config.APIKey) + err = addAssetsToAlbum(assetsInGame, config.Album, config.BaseURL, immichClient) if err != nil { log.Fatalf("Failed to add assets to album: %s", err) return