import datetime
import discord
import hashlib
+import io
import math
import multiprocessing
import os
db.set_state(client, avatar = db.IDLE_AVATAR, idle = True)
def parse_attachment(attachment, filename = None):
- parsed = { 'bytes': None, 'filename': filename }
+ parsed = { 'filename': filename, 'fd': None }
if attachment is None:
log.warning("Called with no data!")
if type(attachment) == dict:
# Already formatted!
- if 'bytes' not in attachment:
+ if 'fd' not in attachment:
log.warning("Invalid attachment dict {}".format(attachment))
return None
if 'filename' not in attachment:
return attachment
elif type(attachment) == bytes:
# Raw bytes.
- parsed['bytes'] = attachment
+ parsed['fd'] = io.BytesIO(attachment)
+ elif type(attachment) == io.BufferedReader:
+ parsed['fd'] = attachment
elif type(attachment) == str:
# Assume URL.
try:
fd = open_url(attachment)
else:
fd = open(attachment, 'rb')
- parsed['bytes'] = fd.read()
+ parsed['fd'] = fd
if filename is None:
parsed['filename'] = os.path.basename(attachment)
except:
if attachment is not None:
parsed = parse_attachment(attachment)
if parsed is not None:
- message = yield from client.send_file(channel, parsed['bytes'], filename = parsed['filename'], content = text)
+ message = yield from client.send_file(channel, parsed['fd'], filename = parsed['filename'], content = text)
+ parsed['fd'].close()
else:
log.warning("Missing or invalid attachment!")
if message is None: