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.
This commit is contained in:
Davte 2019-12-27 21:08:29 +01:00
parent 189a10ddbe
commit 4c1ffb3382
3 changed files with 3 additions and 50 deletions

View File

@ -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}")

View File

@ -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 (

View File

@ -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