diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index db1bee3..acb2e9d 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -7,9 +7,22 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __license__ = "GNU General Public License v3.0" -__version__ = "2.0.10" +__version__ = "2.1.0" __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/hacks.py b/davtelepot/hacks.py new file mode 100644 index 0000000..d43bb12 --- /dev/null +++ b/davtelepot/hacks.py @@ -0,0 +1,35 @@ +"""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