diff options
author | Trygve Aaberge <trygveaa@gmail.com> | 2019-06-04 20:15:04 +0200 |
---|---|---|
committer | Trygve Aaberge <trygveaa@gmail.com> | 2019-06-04 20:42:26 +0200 |
commit | 913e9c9e847ce753fcfb2354894682018d03084d (patch) | |
tree | da8dcce088084c1360ae0217cda4d971119e005e /wee_slack.py | |
parent | 8518fce0363208753119e893a9b83e3d32cb17ce (diff) | |
download | wee-slack-913e9c9e847ce753fcfb2354894682018d03084d.tar.gz |
Use hook_process_hashtable for uploading files
Instead of creating a space separated string of the arguments for curl,
create a dict and use hook_process_hashtable. The benefit of this is
that we don't need to escape the spaces in the file name.
This also utilizes SlackRequest to create the url we post to, so we
don't have to hard code the url in this function.
I tried to use hook_process_hashtable to request the url directly,
instead of calling the curl command, but I didn't manage to make it
upload the file in a format accepted by Slack. I opened an issue to
weechat about it: https://github.com/weechat/weechat/issues/1352
Diffstat (limited to 'wee_slack.py')
-rw-r--r-- | wee_slack.py | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/wee_slack.py b/wee_slack.py index 234e5ee..cf908dc 100644 --- a/wee_slack.py +++ b/wee_slack.py @@ -252,7 +252,7 @@ class ProxyWrapper(object): else: port = "" - return "--proxy {}{}{}".format(user, self.proxy_address, port) + return "-x{}{}{}".format(user, self.proxy_address, port) ##### Helpers @@ -4085,27 +4085,28 @@ def command_upload(data, current_buffer, args): Uploads a file to the current buffer. """ channel = EVENTROUTER.weechat_controller.buffers[current_buffer] - url = 'https://slack.com/api/files.upload' file_path = os.path.expanduser(args) - if ' ' in file_path: - file_path = file_path.replace(' ', r'\ ') - # only http proxy is currenlty supported - proxy = ProxyWrapper() - proxy_string = proxy.curl() - form_fields = { - 'file': '@' + file_path, + post_data = { 'channels': channel.identifier, - 'token': channel.team.token, } if isinstance(channel, SlackThreadChannel): - form_fields['thread_ts'] = channel.parent_message.ts + post_data['thread_ts'] = channel.parent_message.ts - curl_options = ' '.join( - '-F {}={}'.format(*field) for field in form_fields.items()) - command = 'curl {} {} {}'.format(curl_options, proxy_string, url) - w.hook_process(command, config.slack_timeout, '', '') + url = SlackRequest(channel.team.token, 'files.upload', post_data).request_string() + options = [ + '-s', + '-Ffile=@{}'.format(file_path), + url + ] + + proxy_string = ProxyWrapper().curl() + if proxy_string: + options.append(proxy_string) + + options_hashtable = {'arg{}'.format(i + 1): arg for i, arg in enumerate(options)} + w.hook_process_hashtable('curl', options_hashtable, config.slack_timeout, '', '') return w.WEECHAT_RC_OK_EAT command_upload.completion = '%(filename)' |