fix: potential handle leak when send attachments on slack
This commit is contained in:
parent
45d2edef5c
commit
a31403c547
1 changed files with 20 additions and 16 deletions
|
|
@ -155,10 +155,12 @@ func (c *SlackChannel) Send(ctx context.Context, msg bus.OutboundMessage) error
|
|||
|
||||
func (c *SlackChannel) sendWithAttachments(ctx context.Context, channelID, threadTS, content string, attachments []bus.Attachment) error {
|
||||
for _, attachment := range attachments {
|
||||
uploadErr := func() error {
|
||||
file, err := os.Open(attachment.Path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open attachment %s: %w", attachment.Path, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
params := slack.UploadFileV2Parameters{
|
||||
Channel: channelID,
|
||||
|
|
@ -168,12 +170,14 @@ func (c *SlackChannel) sendWithAttachments(ctx context.Context, channelID, threa
|
|||
ThreadTimestamp: threadTS,
|
||||
}
|
||||
|
||||
_, err = c.api.UploadFileV2Context(ctx, params)
|
||||
defer file.Close()
|
||||
|
||||
if err != nil {
|
||||
if _, err = c.api.UploadFileV2Context(ctx, params); err != nil {
|
||||
return fmt.Errorf("failed to upload file %s: %w", attachment.Filename, err)
|
||||
}
|
||||
return nil
|
||||
}()
|
||||
if uploadErr != nil {
|
||||
return uploadErr
|
||||
}
|
||||
|
||||
// Only use content for first attachment to avoid duplicate comments
|
||||
content = ""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue