Queer European MD passionate about IT
소스 검색

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.
Davte 5 년 전
부모
커밋
4c1ffb3382
3개의 변경된 파일3개의 추가작업 그리고 50개의 파일을 삭제
  1. 1 14
      davtelepot/__init__.py
  2. 2 1
      davtelepot/api.py
  3. 0 35
      davtelepot/hacks.py

+ 1 - 14
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}")

+ 2 - 1
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 (

+ 0 - 35
davtelepot/hacks.py

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