From f66c750075b1e46329967dc12c74c47b16980adf Mon Sep 17 00:00:00 2001 From: Johnny Date: Wed, 31 Dec 2025 21:54:37 +0800 Subject: [PATCH] chore: simplify attachment file writing --- plugin/idp/oauth2/oauth2.go | 3 ++- plugin/scheduler/scheduler.go | 2 ++ plugin/webhook/webhook.go | 2 +- server/router/api/v1/attachment_service.go | 5 ----- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/plugin/idp/oauth2/oauth2.go b/plugin/idp/oauth2/oauth2.go index 651bd8431..0640d01a1 100644 --- a/plugin/idp/oauth2/oauth2.go +++ b/plugin/idp/oauth2/oauth2.go @@ -90,11 +90,12 @@ func (p *IdentityProvider) UserInfo(token string) (*idp.IdentityProviderUserInfo if err != nil { return nil, errors.Wrap(err, "failed to get user information") } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) if err != nil { return nil, errors.Wrap(err, "failed to read response body") } - defer resp.Body.Close() var claims map[string]any if err := json.Unmarshal(body, &claims); err != nil { diff --git a/plugin/scheduler/scheduler.go b/plugin/scheduler/scheduler.go index 703c0a667..b795dd1e8 100644 --- a/plugin/scheduler/scheduler.go +++ b/plugin/scheduler/scheduler.go @@ -153,9 +153,11 @@ func (s *Scheduler) runJobWithSchedule(ctx context.Context, rj *registeredJob, s _ = err } case <-ctx.Done(): + // Stop the timer to prevent it from firing. The timer will be garbage collected. timer.Stop() return case <-s.stopCh: + // Stop the timer to prevent it from firing. The timer will be garbage collected. timer.Stop() return } diff --git a/plugin/webhook/webhook.go b/plugin/webhook/webhook.go index fe59d4b88..c87019d26 100644 --- a/plugin/webhook/webhook.go +++ b/plugin/webhook/webhook.go @@ -49,12 +49,12 @@ func Post(requestPayload *WebhookRequestPayload) error { if err != nil { return errors.Wrapf(err, "failed to post webhook to %s", requestPayload.URL) } + defer resp.Body.Close() b, err := io.ReadAll(resp.Body) if err != nil { return errors.Wrapf(err, "failed to read webhook response from %s", requestPayload.URL) } - defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode > 299 { return errors.Errorf("failed to post webhook %s, status code: %d, response body: %s", requestPayload.URL, resp.StatusCode, b) diff --git a/server/router/api/v1/attachment_service.go b/server/router/api/v1/attachment_service.go index 642fcb67f..3226b9f42 100644 --- a/server/router/api/v1/attachment_service.go +++ b/server/router/api/v1/attachment_service.go @@ -317,11 +317,6 @@ func SaveAttachmentBlob(ctx context.Context, profile *profile.Profile, stores *s if err = os.MkdirAll(dir, os.ModePerm); err != nil { return errors.Wrap(err, "Failed to create directory") } - dst, err := os.Create(osPath) - if err != nil { - return errors.Wrap(err, "Failed to create file") - } - defer dst.Close() // Write the blob to the file. if err := os.WriteFile(osPath, create.Blob, 0644); err != nil {