...
 
Commits (2)
# This here is purely to override aiohttp's quoting system
# because it's shit and PR #916 has been open for about a year
# Thanks to Leovoel for helping me with this
from urllib.parse import quote
from aiohttp.multipart import TOKEN
from aiohttp.hdrs import CONTENT_DISPOSITION
import aiohttp
class BodyPartWriter(aiohttp.BodyPartWriter):
def set_content_disposition(self, disposition_type,
should_quote=True, **params):
if not disposition_type or not TOKEN > set(disposition_type):
raise ValueError('bad content disposition type '
'{!r}'.format(disposition_type))
value = disposition_type
if params:
lparams = []
for key, val in params.items():
if not key or not TOKEN > set(key):
raise ValueError('bad content disposition parameter '
'{!r}={!r}'.format(key, val))
qval = quote(val, '') if should_quote else val
lparams.append((key, '"%s"' % qval))
if key == 'filename':
lparams.append(('filename*', "utf-8''" + qval))
value = '; '.join((value, '; '.join('='.join(pair)
for pair in lparams)))
self.headers[CONTENT_DISPOSITION] = value
class MultipartWriter(aiohttp.MultipartWriter):
part_writer_cls = BodyPartWriter
import asyncio
import os.path as osp
from .utils import check_size, BASE_URL, MAX_FILES,\
UPLOAD_PATH, SHORTEN_PATH, UPLOAD_STANDARD,\
......@@ -14,7 +15,6 @@ def async_upload_files(key, *files, **kwargs):
"is {}".format(MAX_FILES))
try:
from . import aiohttp2
import aiohttp
except ImportError:
raise ImportError("Please install the `aiohttp` module "
......@@ -25,14 +25,14 @@ def async_upload_files(key, *files, **kwargs):
for file in files:
check_size(file)
with aiohttp2.MultipartWriter('form-data') as mp:
with aiohttp.MultipartWriter('form-data') as mp:
for file in files:
part = mp.append(open(file, "rb"))
part.set_content_disposition(
'form-data',
should_quote=False,
quote_fields=False,
name='files[]',
filename=file.lower()
filename=osp.basename(file).lower() # Errors without basename
)
session = aiohttp.ClientSession(loop=loop)
......