mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-22 19:06:44 +00:00
Fix send_file to actually work with aiohttp.
This commit is contained in:
parent
92212a4f2a
commit
0009225d08
@ -838,8 +838,6 @@ class Client:
|
|||||||
|
|
||||||
Raises
|
Raises
|
||||||
-------
|
-------
|
||||||
InvalidArgument
|
|
||||||
If ``fp.name`` is an invalid default for ``filename``.
|
|
||||||
HTTPException
|
HTTPException
|
||||||
Sending the file failed.
|
Sending the file failed.
|
||||||
|
|
||||||
@ -852,24 +850,22 @@ class Client:
|
|||||||
channel_id = self._resolve_destination(destination)
|
channel_id = self._resolve_destination(destination)
|
||||||
|
|
||||||
url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
|
url = '{base}/{id}/messages'.format(base=endpoints.CHANNELS, id=channel_id)
|
||||||
|
files = aiohttp.FormData()
|
||||||
|
|
||||||
|
# we don't want the content-type json in this request
|
||||||
|
headers = {
|
||||||
|
'authorization': self.token
|
||||||
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# attempt to open the file and send the request
|
# attempt to open the file and send the request
|
||||||
with open(fp, 'rb') as f:
|
with open(fp, 'rb') as f:
|
||||||
files = {
|
files.add_field('file', f, filename=filename)
|
||||||
'file': (fp if filename is None else filename, f)
|
response = yield from self.session.post(url, data=files, headers=headers)
|
||||||
}
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
# if we got a TypeError then this is probably a file-like object
|
files.add_field('file', fp, filename=filename)
|
||||||
fname = getattr(fp, 'name', None) if filename is None else filename
|
response = yield from self.session.post(url, data=files, headers=headers)
|
||||||
if fname is None:
|
|
||||||
raise InvalidArgument('file-like object has no name attribute and no filename was specified')
|
|
||||||
|
|
||||||
files = {
|
|
||||||
'file': (fname, fp)
|
|
||||||
}
|
|
||||||
|
|
||||||
response = yield from self.session.post(url, data=files, headers=self.headers)
|
|
||||||
log.debug(request_logging_format.format(method='POST', response=response))
|
log.debug(request_logging_format.format(method='POST', response=response))
|
||||||
yield from utils._verify_successful_response(response)
|
yield from utils._verify_successful_response(response)
|
||||||
data = yield from response.json()
|
data = yield from response.json()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user