Python-slack-sdk: files_upload ignores filetype

Created on 18 Jul 2019  路  3Comments  路  Source: slackapi/python-slack-sdk

Description

Describe your issue here.

What type of issue is this?

  • [x] bug
  • [ ] enhancement (feature request)
  • [x] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Reproducible in:

slackclient version: 2.0.1 and 2.1.0

python version: 3.6.7

OS version(s): OSX 10.14.1 and Ubuntu 16.04.5

Steps to reproduce:

Take any html file, upload with explicitly setting the filetype and check out the assigned filetype in response:

>>> web_client.files_upload(channels=channel, file='index.html', filetype='svg').data
{'ok': True,
 'file': {'id': 'xxxxxxx',
  'created': 1563466794,
  'timestamp': 1563466794,
  'name': 'test',
  'title': 'test',
  'mimetype': 'text/plain',
  'filetype': 'html',
  'pretty_type': 'HTML',
  'user': 'xxxxxx',
  'editable': False,
  'size': 5148500,
  'mode': 'hosted',
  'is_external': False,
  'external_type': '',
  'is_public': False,
  'public_url_shared': False,
  'display_as_bot': False,
  'username': '',
  'url_private': 'https://files.slack.com/files-pri/xxxxxx-xxxxxxxx/test',
  'url_private_download': 'https://files.slack.com/files-pri/xxxxxx-xxxxxxx/download/test',
  'permalink': 'https://fafb.slack.com/files/xxxxxxx/xxxxx/test',
  'permalink_public': 'https://slack-files.com/xxxxxx-xxxxxx-xxxxxxxxx',
  'comments_count': 0,
  'is_starred': False,
  'shares': {'private': {'xxxxxxxx': [{'reply_users': [],
      'reply_users_count': 0,
      'reply_count': 0,
      'ts': 'xxxxxxxxx'}]}},
  'channels': [],
  'groups': [],
  'ims': ['xxxxxx'],
  'has_rich_preview': False}}

Expected result:

File is forced into given file type.

Actual result:

No matter what is used as filetype, it will always (correctly) parse the file as a html file with mimetype text/plain.

The reason why I tried forcing a different file type in the first place is that the specific .html file I am uploading is renamed to index.html.txt file by Slack upon subsequent download and I was hoping that forcing a different filetype would stop this behaviour.

FWIW: the same happenswhen I upload just the content instead of a file:

with open('index.html', 'r') as f: 
     _ = web_client.files_upload(channels=channel, content=f.read(), filetype='svg', filename='test.svg') 

I'm aware that this looks to be more of an upstream issue with the Slack API. Please feel free to point me elsewhere with this issue.

2x question

Most helpful comment

@schlegelp and @blacktig3r To specify custom file types you can create your own file object with the FormData class provided by aiohttp.

from aiohttp import FormData

with open("index.html", "rb") as f:
    data = FormData()
    data.add_field(
        "file", f, filename="index.html", content_type="image/svg+xml"
    )

    web_client.files_upload(channels=channel, file=data)

Please let me know if you run into any additional trouble with this.

All 3 comments

facing same issue..plz help me if u got any solution
Thanks

@schlegelp and @blacktig3r To specify custom file types you can create your own file object with the FormData class provided by aiohttp.

from aiohttp import FormData

with open("index.html", "rb") as f:
    data = FormData()
    data.add_field(
        "file", f, filename="index.html", content_type="image/svg+xml"
    )

    web_client.files_upload(channels=channel, file=data)

Please let me know if you run into any additional trouble with this.

As Rodney answered this question, please allow me to close this issue. Feel free to reopen or write in if you have something further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Terrance picture Terrance  路  3Comments

divyatman picture divyatman  路  4Comments

tinoargentino picture tinoargentino  路  4Comments

marshallino16 picture marshallino16  路  3Comments

seratch picture seratch  路  3Comments