slack-ruby / slack-ruby-client

A Ruby and command-line client for the Slack Web, Real Time Messaging and Event APIs.
MIT License
1.21k stars 214 forks source link

Set a default filename so that Slack displays the preview correctly #375

Open ts-3156 opened 3 years ago

ts-3156 commented 3 years ago

Starting at 2021/05/05 05:30 UTC, Slack client no longer correctly displays previews of images when filename is not specified via slack-ruby-client gem.

Expectation Reality
スクリーンショット 2021-05-07 21 52 06 スクリーンショット 2021-05-07 21 47 43

The reason of this behavior is that when a filename is not set to both Faraday::UploadIO.new and Slack::Web::Client.new.files_upload, multipart-post gem sets local.path as a filename. If local.path is specified as the filename, Slack will recognize the file as binary and will not show the image preview. In this pull request, I specify the default filename to avoid this behavior.

# An example of files.upload API
image = File.binread('image.png')
file = Faraday::UploadIO.new(StringIO.new(image), '_content_type_', 'can_specify_filename_here')
Slack::Web::Client.new.files_upload(channels: '#general', file: file, filename: 'can_specify_filename_here_too')
# The location where local.path is specified 
# https://github.com/socketry/multipart-post/blob/master/lib/multipart/post/composite_read_io.rb#L83
local_path = filename_or_io.respond_to?(:path) ? filename_or_io.path : "local.path"

The solution of specifying a default filename may be out of scope for slack-ruby-client gem. If you think so, please feel free to close this pull request.

dangerpr-bot commented 3 years ago
2 Warnings
:warning: There're library changes, but not tests. That's OK as long as you're refactoring existing code.
:warning: Unless you're refactoring existing code or improving documentation, please update CHANGELOG.md.

Here's an example of a CHANGELOG.md entry:

* [#375](https://github.com/slack-ruby/slack-ruby-client/pull/375): Set a default filename so that slack displays the preview correctly - [@ts-3156](https://github.com/ts-3156).

Generated by :no_entry_sign: Danger

dblock commented 3 years ago

This file is generated, so you'll need to introduce this as a patch, or we'll lose the change next update of the web API, see https://github.com/slack-ruby/slack-ruby-client/blob/master/CONTRIBUTING.md#patching-slack-web-api. It will also need tests and a README change. Thanks!