Skip to content

Commit

Permalink
chore: make channel optional on UploadFileV2 (#1293)
Browse files Browse the repository at this point in the history
* Support no channel

* added test

* fix channel id param name
  • Loading branch information
0xdeafcafe authored Jul 15, 2024
1 parent bc70fad commit ef53c0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions files.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,13 @@ func (api *Client) completeUploadExternal(ctx context.Context, fileID string, pa
return nil, err
}
values := url.Values{
"token": {api.token},
"files": {string(requestBytes)},
"channel_id": {params.channel},
"token": {api.token},
"files": {string(requestBytes)},
}

if params.channel != "" {
values.Add("channel_id", params.channel)
}
if params.initialComment != "" {
values.Add("initial_comment", params.initialComment)
}
Expand Down Expand Up @@ -555,17 +557,15 @@ func (api *Client) UploadFileV2(params UploadFileV2Parameters) (*FileSummary, er
// UploadFileV2 uploads file to a given slack channel using 3 steps with a custom context -
// 1. Get an upload URL using files.getUploadURLExternal API
// 2. Send the file as a post to the URL provided by slack
// 3. Complete the upload and share it to the specified channel using files.completeUploadExternal
// 3. Complete the upload and if a channel is specified, post it to the specified channel using files.completeUploadExternal
func (api *Client) UploadFileV2Context(ctx context.Context, params UploadFileV2Parameters) (file *FileSummary, err error) {
if params.Filename == "" {
return nil, fmt.Errorf("file.upload.v2: filename cannot be empty")
}
if params.FileSize == 0 {
return nil, fmt.Errorf("file.upload.v2: file size cannot be 0")
}
if params.Channel == "" {
return nil, fmt.Errorf("file.upload.v2: channel cannot be empty")
}

u, err := api.getUploadURLExternal(ctx, getUploadURLExternalParameters{
altText: params.AltTxt,
fileName: params.Filename,
Expand Down
9 changes: 9 additions & 0 deletions files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,13 @@ func TestUploadFileV2(t *testing.T) {
if _, err := api.UploadFileV2(params); err != nil {
t.Errorf("Unexpected error: %s", err)
}

reader = bytes.NewBufferString("test no channel")
params = UploadFileV2Parameters{
Filename: "test.txt",
Reader: reader,
FileSize: 15}
if _, err := api.UploadFileV2(params); err != nil {
t.Errorf("Unexpected error: %s", err)
}
}

0 comments on commit ef53c0c

Please sign in to comment.