greco: set binary parts after mime structure is created (#44555)

This commit is contained in:
Frédéric Péters 2020-06-29 14:13:57 +02:00
parent ca80b7cdaf
commit aea1a93c69
1 changed files with 10 additions and 5 deletions

View File

@ -16,10 +16,7 @@
import base64
import re
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
@ -140,9 +137,10 @@ class Greco(BaseResource):
part = MIMEText(content, _subtype=subtype)
else:
part = MIMEBase(maintype, subtype, name=filename)
part.set_payload(content)
attachment['real_bytes'] = content
attachment['fake_bytes'] = '\ue000%s\ue000' % num
part.set_payload(attachment['fake_bytes'])
part.add_header('Content-Transfer-Encoding', 'binary')
encoders.encode_noop(part)
part.add_header('Content-Disposition', 'attachment', name=filename, filename=filename)
part.add_header('Content-ID', '<%s>' % filename)
message.attach(part)
@ -163,6 +161,13 @@ class Greco(BaseResource):
request.message = message.as_string(unixfrom=False
).replace(boundary + '\n', boundary + '\r\n'
).replace('\n--' + boundary, '\r\n--' + boundary).encode('utf-8')
for attachment in attachments:
# substitute binary parts
if attachment.get('fake_bytes'):
request.message = request.message.replace(
attachment['fake_bytes'].encode('utf-8'),
attachment['real_bytes'])
request.headers.update(dict(message._headers))
request.headers['Authorization'] = self.instance.get_token()
resp = self.instance.requests.post(request.url, data=request.message,