more attachment refactoring/alignment

This commit is contained in:
Chris Caron 2024-08-27 18:54:52 -04:00
parent 9bd068b274
commit cda1bd9dd9
12 changed files with 49 additions and 23 deletions

View File

@ -266,12 +266,15 @@ class NotifyAppriseAPI(NotifyBase):
return False
try:
# Our Attachment filename
filename = attachment.name \
if attachment.name else f'file{no:03}.dat'
if self.method == AppriseAPIMethod.JSON:
# Output must be in a DataURL format (that's what
# PushSafer calls it):
attachments.append({
"filename": attachment.name
if attachment.name else f'file{no:03}.dat',
"filename": filename,
'base64': attachment.base64(),
'mimetype': attachment.mimetype,
})
@ -280,7 +283,7 @@ class NotifyAppriseAPI(NotifyBase):
files.append((
'file{:02d}'.format(no),
(
attachment.name,
filename,
open(attachment.path, 'rb'),
attachment.mimetype,
)

View File

@ -302,7 +302,8 @@ class NotifyForm(NotifyBase):
files.append((
self.attach_as.format(no)
if self.attach_multi_support else self.attach_as, (
attachment.name,
attachment.name
if attachment.name else f'file{no:03}.dat',
open(attachment.path, 'rb'),
attachment.mimetype)
))

View File

@ -213,7 +213,7 @@ class NotifyJSON(NotifyBase):
# Track our potential attachments
attachments = []
if attach and self.attachment_support:
for no, attachment in enumerate(attach):
for no, attachment in enumerate(attach, start=1):
# Perform some simple error checking
if not attachment:
# We could not access the attachment

View File

@ -799,7 +799,7 @@ class NotifyEmail(NotifyBase):
mixed = MIMEMultipart("mixed")
mixed.attach(base)
# Now store our attachments
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
if not attachment:
# We could not load the attachment; take an early
# exit since this isn't what the end user wanted
@ -819,10 +819,14 @@ class NotifyEmail(NotifyBase):
app = MIMEApplication(abody.read())
app.set_type(attachment.mimetype)
# Prepare our attachment name
filename = attachment.name \
if attachment.name else f'file{no:03}.dat'
app.add_header(
'Content-Disposition',
'attachment; filename="{}"'.format(
Header(attachment.name, 'utf-8')),
Header(filename, 'utf-8')),
)
mixed.attach(app)
base = mixed

View File

@ -383,9 +383,15 @@ class NotifyMailgun(NotifyBase):
self.logger.debug(
'Preparing Mailgun attachment {}'.format(
attachment.url(privacy=True)))
# Prepare our filename
filename = attachment.name \
if attachment.name \
else 'file{no:03}.dat'.format(no=idx + 1)
try:
files['attachment[{}]'.format(idx)] = \
(attachment.name, open(attachment.path, 'rb'))
(filename, open(attachment.path, 'rb'))
except (OSError, IOError) as e:
self.logger.warning(

View File

@ -152,7 +152,7 @@ class NotifyPushBullet(NotifyBase):
if attach and self.attachment_support:
# We need to upload our payload first so that we can source it
# in remaining messages
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
# Perform some simple error checking
if not attachment:
@ -168,7 +168,8 @@ class NotifyPushBullet(NotifyBase):
# prepare payload
payload = {
'file_name': attachment.name,
'file_name': attachment.name
if attachment.name else f'file{no:03}.dat',
'file_type': attachment.mimetype,
}
# First thing we need to do is make a request so that we can

View File

@ -548,7 +548,7 @@ class NotifyPushSafer(NotifyBase):
if attach and self.attachment_support:
# We need to upload our payload first so that we can source it
# in remaining messages
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
# prepare payload
if not attachment:
# We could not access the attachment
@ -572,7 +572,8 @@ class NotifyPushSafer(NotifyBase):
# Output must be in a DataURL format (that's what
# PushSafer calls it):
attachments.append((
attachment.name,
attachment.name
if attachment.name else f'file{no:03}.dat',
'data:{};base64,{}'.format(
attachment.mimetype,
attachment.base64(),

View File

@ -448,7 +448,7 @@ class NotifySES(NotifyBase):
base.attach(content)
# Now store our attachments
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
if not attachment:
# We could not load the attachment; take an early
# exit since this isn't what the end user wanted
@ -468,10 +468,13 @@ class NotifySES(NotifyBase):
app = MIMEApplication(abody.read())
app.set_type(attachment.mimetype)
filename = attachment.name \
if attachment.name else f'file{no:03}.dat'
app.add_header(
'Content-Disposition',
'attachment; filename="{}"'.format(
Header(attachment.name, 'utf-8')),
Header(filename, 'utf-8')),
)
base.attach(app)

View File

@ -646,7 +646,7 @@ class NotifySlack(NotifyBase):
if attach and self.attachment_support and \
self.mode is SlackMode.BOT and attach_channel_list:
# Send our attachments (can only be done in bot mode)
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
# Perform some simple error checking
if not attachment:
@ -663,7 +663,8 @@ class NotifySlack(NotifyBase):
# Get the URL to which to upload the file.
# https://api.slack.com/methods/files.getUploadURLExternal
_params = {
'filename': attachment.name,
'filename': attachment.name
if attachment.name else f'file{no:03}.dat',
'length': len(attachment),
}
_url = self.api_url.format('files.getUploadURLExternal')

View File

@ -294,7 +294,7 @@ class NotifySMTP2Go(NotifyBase):
attachments = []
if attach and self.attachment_support:
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
# Perform some simple error checking
if not attachment:
# We could not access the attachment
@ -306,7 +306,8 @@ class NotifySMTP2Go(NotifyBase):
try:
# Format our attachment
attachments.append({
'filename': attachment.name,
'filename': attachment.name
if attachment.name else f'file{no:03}.dat',
'fileblob': attachment.base64(),
'mimetype': attachment.mimetype,
})

View File

@ -546,7 +546,7 @@ class NotifySparkPost(NotifyBase):
# Prepare ourselves an attachment object
payload['content']['attachments'] = []
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
# Perform some simple error checking
if not attachment:
# We could not access the attachment
@ -558,7 +558,8 @@ class NotifySparkPost(NotifyBase):
try:
# Prepare API Upload Payload
payload['content']['attachments'].append({
'name': attachment.name,
'name': attachment.name
if attachment.name else f'file{no:03}.dat',
'type': attachment.mimetype,
'data': attachment.base64(),
})

View File

@ -287,7 +287,7 @@ class NotifyTwitter(NotifyBase):
if attach and self.attachment_support:
# We need to upload our payload first so that we can source it
# in remaining messages
for attachment in attach:
for no, attachment in enumerate(attach, start=1):
# Perform some simple error checking
if not attachment:
@ -320,11 +320,15 @@ class NotifyTwitter(NotifyBase):
# We can't post our attachment
return False
# Prepare our filename
filename = attachment.name \
if attachment.name else f'file{no:03}.dat'
if not (isinstance(response, dict)
and response.get('media_id')):
self.logger.debug(
'Could not attach the file to Twitter: %s (mime=%s)',
attachment.name, attachment.mimetype)
filename, attachment.mimetype)
continue
# If we get here, our output will look something like this:
@ -344,7 +348,7 @@ class NotifyTwitter(NotifyBase):
response.update({
# Update our response to additionally include the
# attachment details
'file_name': attachment.name,
'file_name': filename,
'file_mime': attachment.mimetype,
'file_path': attachment.path,
})