use http.Client for API key in immich

This commit is contained in:
2025-11-06 23:38:21 +01:00
parent 06996a35d5
commit 4d772878bc
2 changed files with 34 additions and 13 deletions

View File

@@ -28,7 +28,27 @@ type bulkIdResponseDto struct {
Success bool `json:"success"` 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) file, err := os.Open(filePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("open file: %w", err) return nil, fmt.Errorf("open file: %w", err)
@@ -66,12 +86,11 @@ func uploadToImmich(filePath, appID, baseURL, apiKey, deviceID string) (*uploadR
if err != nil { if err != nil {
return nil, fmt.Errorf("create request: %w", err) 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("Content-Type", writer.FormDataContentType())
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
client := &http.Client{Timeout: 60 * time.Second} resp, err := httpClient.Do(req)
resp, err := client.Do(req)
if err != nil { if err != nil {
return nil, fmt.Errorf("POST to Immich: %w", err) 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 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{ body, _ := json.Marshal(assetPutRequest{
Ids: assets, Ids: assets,
Description: description, Description: description,
@@ -101,11 +120,10 @@ func setAssetMetadata(assets []string, description, baseURL, apiKey string) erro
return err return err
} }
req.Header.Set("x-api-key", apiKey)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
_, err = http.DefaultClient.Do(req) _, err = httpClient.Do(req)
if err != nil { if err != nil {
return err return err
} }
@@ -113,7 +131,7 @@ func setAssetMetadata(assets []string, description, baseURL, apiKey string) erro
return nil 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 { body, _ := json.Marshal(struct {
IDs []string `json:"ids"` IDs []string `json:"ids"`
}{IDs: assets}) }{IDs: assets})
@@ -123,11 +141,10 @@ func addAssetsToAlbum(assets []string, album, baseURL, apiKey string) error {
return err return err
} }
req.Header.Set("x-api-key", apiKey)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json") req.Header.Set("Accept", "application/json")
res, err := http.DefaultClient.Do(req) res, err := httpClient.Do(req)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -26,6 +26,8 @@ func Run(config Config) {
return return
} }
immichClient := newImmichHttpClient(config.APIKey)
gameNameCache := make(map[string]string) gameNameCache := make(map[string]string)
for appid, files := range screenshotFiles { for appid, files := range screenshotFiles {
@@ -33,7 +35,7 @@ func Run(config Config) {
assetsInGame := make([]string, 0) assetsInGame := make([]string, 0)
for _, filepath := range files { 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 { if err != nil {
log.Fatalf("Failed to upload to immich: %s", err) 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) log.Printf("Assest uploaded: %s (%s) %s with ID: %s", gameName, appid, filepath, res.ID)
return
assetsInGame = append(assetsInGame, res.ID) 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 { if err != nil {
log.Fatalf("Failed to set asset metadata: %s", err) log.Fatalf("Failed to set asset metadata: %s", err)
return return
} }
if config.Album != "" { if config.Album != "" {
err = addAssetsToAlbum(assetsInGame, config.Album, config.BaseURL, config.APIKey) err = addAssetsToAlbum(assetsInGame, config.Album, config.BaseURL, immichClient)
if err != nil { if err != nil {
log.Fatalf("Failed to add assets to album: %s", err) log.Fatalf("Failed to add assets to album: %s", err)
return return