Prevent aiohttp from encoding file names as URLs
This commit is contained in:
parent
f590d3d677
commit
b6c8c7e5bc
@ -7,9 +7,22 @@ __author__ = "Davide Testa"
|
|||||||
__email__ = "davide@davte.it"
|
__email__ = "davide@davte.it"
|
||||||
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
|
||||||
__license__ = "GNU General Public License v3.0"
|
__license__ = "GNU General Public License v3.0"
|
||||||
__version__ = "2.0.10"
|
__version__ = "2.1.0"
|
||||||
__maintainer__ = "Davide Testa"
|
__maintainer__ = "Davide Testa"
|
||||||
__contact__ = "t.me/davte"
|
__contact__ = "t.me/davte"
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
# Legacy module; please use `from davtelepot.bot import Bot` from now on
|
# Legacy module; please use `from davtelepot.bot import Bot` from now on
|
||||||
from davtelepot.custombot import Bot
|
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}")
|
||||||
|
35
davtelepot/hacks.py
Normal file
35
davtelepot/hacks.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user