From 4c1ffb3382e0ed47c21cf1b51265fb68c51f9fd8 Mon Sep 17 00:00:00 2001 From: Davte Date: Fri, 27 Dec 2019 21:08:29 +0100 Subject: [PATCH] Hack no longer needed to encode filenames Thanks to tirkarthi.github.io, a simple parameter `quote_fields` can solve the issue which needed a messy workaround until now. --- davtelepot/__init__.py | 15 +-------------- davtelepot/api.py | 3 ++- davtelepot/hacks.py | 35 ----------------------------------- 3 files changed, 3 insertions(+), 50 deletions(-) delete mode 100644 davtelepot/hacks.py diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index dab8d70..7e45177 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -14,22 +14,9 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __license__ = "GNU General Public License v3.0" -__version__ = "2.3.7" +__version__ = "2.3.8" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" -import logging - # Legacy module; please use `from davtelepot.bot import Bot` from now on from davtelepot.custombot import Bot - -# Prevent aiohttp from encoding file names as URLs, replacing ' ' with '%20' -# Thanks @Nickoala (Nick Lee) for this hack from his archived `telepot` repo -try: - import aiohttp - from davtelepot.hacks import hacked_content_disposition_header - aiohttp.payload.content_disposition_header = ( - hacked_content_disposition_header - ) -except (ImportError, AttributeError) as e: - logging.error(f"{e}") diff --git a/davtelepot/api.py b/davtelepot/api.py index 6e168fb..ea105bc 100644 --- a/davtelepot/api.py +++ b/davtelepot/api.py @@ -146,7 +146,8 @@ class TelegramBot(object): Cast integers to string to avoid TypeError during json serialization. """ exclude.append('self') - data = aiohttp.FormData() + # quote_fields must be set to False, otherwise filenames cause troubles + data = aiohttp.FormData(quote_fields=False) for key, value in parameters.items(): if not (key in exclude or value is None): if ( diff --git a/davtelepot/hacks.py b/davtelepot/hacks.py deleted file mode 100644 index d43bb12..0000000 --- a/davtelepot/hacks.py +++ /dev/null @@ -1,35 +0,0 @@ -"""Useful functions to patch third part libraries until they are fixed.""" - -import aiohttp -from urllib.parse import quote - - -def hacked_content_disposition_header(disptype, quote_fields=True, **params): - """Prevent aiohttp from encoding file names as URLs. - - Thanks @Nickoala (Nick Lee) for this hack from his archived `telepot` repo. - See https://github.com/nickoala/telepot/blob/master/telepot/aio/hack.py - for details. - """ - if not disptype or not (aiohttp.helpers.TOKEN > set(disptype)): - raise ValueError('bad content disposition type {!r}' - ''.format(disptype)) - - value = disptype - if params: - lparams = [] - for key, val in params.items(): - if not key or not (aiohttp.helpers.TOKEN > set(key)): - raise ValueError('bad content disposition parameter' - ' {!r}={!r}'.format(key, val)) - - if key == 'filename': - qval = val - else: - qval = quote(val, '') if quote_fields else val - - lparams.append((key, '"%s"' % qval)) - - sparams = '; '.join('='.join(pair) for pair in lparams) - value = '; '.join((value, sparams)) - return value