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,24 +155,28 @@ 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 {
|
func (c *SlackChannel) sendWithAttachments(ctx context.Context, channelID, threadTS, content string, attachments []bus.Attachment) error {
|
||||||
for _, attachment := range attachments {
|
for _, attachment := range attachments {
|
||||||
file, err := os.Open(attachment.Path)
|
uploadErr := func() error {
|
||||||
if err != nil {
|
file, err := os.Open(attachment.Path)
|
||||||
return fmt.Errorf("failed to open attachment %s: %w", attachment.Path, err)
|
if err != nil {
|
||||||
}
|
return fmt.Errorf("failed to open attachment %s: %w", attachment.Path, err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
params := slack.UploadFileV2Parameters{
|
params := slack.UploadFileV2Parameters{
|
||||||
Channel: channelID,
|
Channel: channelID,
|
||||||
Filename: attachment.Filename,
|
Filename: attachment.Filename,
|
||||||
Reader: file,
|
Reader: file,
|
||||||
InitialComment: content,
|
InitialComment: content,
|
||||||
ThreadTimestamp: threadTS,
|
ThreadTimestamp: threadTS,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = c.api.UploadFileV2Context(ctx, params)
|
if _, err = c.api.UploadFileV2Context(ctx, params); err != nil {
|
||||||
defer file.Close()
|
return fmt.Errorf("failed to upload file %s: %w", attachment.Filename, err)
|
||||||
|
}
|
||||||
if err != nil {
|
return nil
|
||||||
return fmt.Errorf("failed to upload file %s: %w", attachment.Filename, err)
|
}()
|
||||||
|
if uploadErr != nil {
|
||||||
|
return uploadErr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only use content for first attachment to avoid duplicate comments
|
// Only use content for first attachment to avoid duplicate comments
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue