Skip to content

Commit 174eee9

Browse files
dblockts-3156Copilot
committed
Set a default filename so that Slack displays the file preview correctly
Fixes files_upload not showing image previews when filename is not specified, since multipart-post falls back to the local IO path as the filename, which Slack treats as a binary file. Co-authored-by: ts-3156 <ts_3156@yahoo.co.jp> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2a697df commit 174eee9

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### 3.3.0 (Next)
22

33
* [#602](https://github.com/slack-ruby/slack-ruby-client/pull/602): Make gli an optional dependency; only the `slack` command-line client needs it - [@corsonknowles](https://github.com/corsonknowles).
4+
* [#599](https://github.com/slack-ruby/slack-ruby-client/pull/599): Set a default filename in `files_upload` so Slack displays image previews correctly when none is specified - [@ts-3156](https://github.com/ts-3156), [@dblock](https://github.com/dblock).
45
* [#591](https://github.com/slack-ruby/slack-ruby-client/pull/591): Generate AI CHANGELOG entries and PR summaries for automated API update PRs, lock simplecov below 1.1.0 to avoid breaking Coveralls - [@dblock](https://github.com/dblock).
56
* [#590](https://github.com/slack-ruby/slack-ruby-client/pull/590): Add entity.acknowledgeCommentAction and entity.presentComments methods - [@slack-ruby-ci-bot](https://github.com/apps/slack-ruby-ci-bot).
67
* [#590](https://github.com/slack-ruby/slack-ruby-client/pull/590): Add admin.apps.mcpServers, admin.apps.mcpServers.permissions, and admin.apps.permissions endpoints - [@slack-ruby-ci-bot](https://github.com/apps/slack-ruby-ci-bot).

‎lib/slack/web/api/endpoints/files.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def files_sharedPublicURL(options = {})
192192
# @see https://api.slack.com/methods/files.upload
193193
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/files/files.upload.json
194194
def files_upload(options = {})
195+
options = options.merge(filename: 'file') if options[:file] && !options[:filename]
195196
post('files.upload', options)
196197
end
197198
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
diff --git a/lib/slack/web/api/endpoints/files.rb b/lib/slack/web/api/endpoints/files.rb
2+
index 13a1889..9ce4894 100644
3+
--- a/lib/slack/web/api/endpoints/files.rb
4+
+++ b/lib/slack/web/api/endpoints/files.rb
5+
@@ -192,6 +192,7 @@ module Slack
6+
# @see https://api.slack.com/methods/files.upload
7+
# @see https://github.com/slack-ruby/slack-api-ref/blob/master/methods/files/files.upload.json
8+
def files_upload(options = {})
9+
+ options = options.merge(filename: 'file') if options[:file] && !options[:filename]
10+
post('files.upload', options)
11+
end
12+
end

‎spec/slack/web/api/endpoints/custom/files_spec.rb‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
RSpec.describe Slack::Web::Api::Endpoints::Files do
55
let(:client) { Slack::Web::Client.new }
66

7+
describe '#files_upload' do
8+
it 'defaults the filename when a file is supplied' do
9+
options = { file: 'file contents' }
10+
expect(client).to receive(:post).with('files.upload', { file: 'file contents', filename: 'file' })
11+
client.files_upload(options)
12+
expect(options).to eq(file: 'file contents')
13+
end
14+
15+
it 'preserves an explicit filename' do
16+
expect(client).to receive(:post).with('files.upload', { file: 'file contents', filename: 'image.png' })
17+
client.files_upload(file: 'file contents', filename: 'image.png')
18+
end
19+
20+
it 'does not add a filename when no file is supplied' do
21+
expect(client).to receive(:post).with('files.upload', { content: 'text' })
22+
client.files_upload(content: 'text')
23+
end
24+
end
25+
726
%w[filename content].each do |arg|
827
context "when #{arg} is missing from options" do
928
let(:params) do

0 commit comments

Comments
 (0)