slack-ruby-client icon indicating copy to clipboard operation
slack-ruby-client copied to clipboard

Allow file upload with bytes

Open mbowen13 opened this issue 7 years ago • 5 comments

I am able to upload a file directly from a buffer fo bytes using the Node sdk, but I cannot do this with the Ruby sdk.

Example: For Node I have a buffer of bytes <Buffer 89 50 4e 47> that I can pass in to files.upload(file: buffer)

But I cannot do the same with a Ruby byte array. I get the error: "invalid_array_arg"

/Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/slack-ruby-client-0.14.2/lib/slack/web/faraday/response/raise_error.rb:11:in `on_complete'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/response.rb:9:in `block in call'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/response.rb:61:in `on_complete'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/response.rb:8:in `call'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/response.rb:8:in `call'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/request/url_encoded.rb:15:in `call'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/request/multipart.rb:15:in `call'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/rack_builder.rb:143:in `build_response'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/connection.rb:387:in `run_request'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/faraday-0.15.4/lib/faraday/connection.rb:175:in `post'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/slack-ruby-client-0.14.2/lib/slack/web/faraday/request.rb:25:in `request'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/slack-ruby-client-0.14.2/lib/slack/web/faraday/request.rb:10:in `post'
    /Users/michael.bowen/.rvm/gems/ruby-2.5.5@lex/gems/slack-ruby-client-0.14.2/lib/slack/web/api/endpoints/files.rb:142:in `files_upload'
    /Users/michael.bowen/Documents/ops-slackbot/lambdas/fulfillment/lib/slack_wrapper.rb:34:in `post_img'
    /Users/michael.bowen/Documents/ops-slackbot/lambdas/fulfillment/test/slack_wrapper_test.rb:36:in `test_post_img_works'

mbowen13 avatar Jun 24 '19 20:06 mbowen13

I got this to work, but only after reading through the Faraday documentation because it wasn't clear in the slack docs what contents: and file: each expect as inputs.

The solution is:

file = StringIO.new("\x89PNG\r\n\x1A\n\x00\x00\x00\r")
@client.files_upload(
      channels: '#general,
      as_user: true,
      file: Faraday::UploadIO.new(file, 'image/png'),
      filetype: 'png'
    )

To this point I still have no idea how to work around using Faraday for the file: option.

mbowen13 avatar Jun 24 '19 23:06 mbowen13

We actually have the file option documented. To send a stream of bytes I believe this will work.

io = StringIO.new('Test data')
upload_io = Faraday::UploadIO.new(io, 'application/octet/stream')

I found this here.

Please do contribute to the docs!

dblock avatar Jun 25 '19 12:06 dblock

@dblock I guess the best thing is for me to contribute. Do I submit a PR to just the README, or where else?

mbowen13 avatar Jun 25 '19 15:06 mbowen13

@dblock Can I submit PR to update the README and flags within the code and any other places in the code to help clarify this issue?

mbowen13 avatar Jun 25 '19 15:06 mbowen13

@dblock Can I submit PR to update the README and flags within the code and any other places in the code to help clarify this issue?

yes please!

dblock avatar Jun 26 '19 00:06 dblock