From 54e46c7ef5573cb839c81acf2f2d639fa3eedc5a Mon Sep 17 00:00:00 2001 From: Ovyerus Date: Wed, 27 Feb 2019 21:52:13 +1100 Subject: [PATCH] fix(async_owo.py): update for latest aiohttp The reason for the `aiohttp2` file existing is now redundant (https://github.com/aio-libs/aiohttp/pull/916 got merged), so migrated to use regular aiohttp `MultipartWriter`. --- owo/aiohttp2.py | 36 ------------------------------------ owo/async_owo.py | 8 ++++---- 2 files changed, 4 insertions(+), 40 deletions(-) delete mode 100644 owo/aiohttp2.py diff --git a/owo/aiohttp2.py b/owo/aiohttp2.py deleted file mode 100644 index 5b7211c..0000000 --- a/owo/aiohttp2.py +++ /dev/null @@ -1,36 +0,0 @@ -# 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 diff --git a/owo/async_owo.py b/owo/async_owo.py index 1a79697..29ce3c0 100644 --- a/owo/async_owo.py +++ b/owo/async_owo.py @@ -1,4 +1,5 @@ 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)