more attachment refactoring/alignment

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

View File

@ -302,7 +302,8 @@ class NotifyForm(NotifyBase):
files.append(( files.append((
self.attach_as.format(no) self.attach_as.format(no)
if self.attach_multi_support else self.attach_as, ( 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'), open(attachment.path, 'rb'),
attachment.mimetype) attachment.mimetype)
)) ))

View File

@ -213,7 +213,7 @@ class NotifyJSON(NotifyBase):
# Track our potential attachments # Track our potential attachments
attachments = [] attachments = []
if attach and self.attachment_support: 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 # Perform some simple error checking
if not attachment: if not attachment:
# We could not access the attachment # We could not access the attachment

View File

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

View File

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

View File

@ -152,7 +152,7 @@ class NotifyPushBullet(NotifyBase):
if attach and self.attachment_support: if attach and self.attachment_support:
# We need to upload our payload first so that we can source it # We need to upload our payload first so that we can source it
# in remaining messages # in remaining messages
for attachment in attach: for no, attachment in enumerate(attach, start=1):
# Perform some simple error checking # Perform some simple error checking
if not attachment: if not attachment:
@ -168,7 +168,8 @@ class NotifyPushBullet(NotifyBase):
# prepare payload # prepare payload
payload = { payload = {
'file_name': attachment.name, 'file_name': attachment.name
if attachment.name else f'file{no:03}.dat',
'file_type': attachment.mimetype, 'file_type': attachment.mimetype,
} }
# First thing we need to do is make a request so that we can # 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: if attach and self.attachment_support:
# We need to upload our payload first so that we can source it # We need to upload our payload first so that we can source it
# in remaining messages # in remaining messages
for attachment in attach: for no, attachment in enumerate(attach, start=1):
# prepare payload # prepare payload
if not attachment: if not attachment:
# We could not access the attachment # We could not access the attachment
@ -572,7 +572,8 @@ class NotifyPushSafer(NotifyBase):
# Output must be in a DataURL format (that's what # Output must be in a DataURL format (that's what
# PushSafer calls it): # PushSafer calls it):
attachments.append(( attachments.append((
attachment.name, attachment.name
if attachment.name else f'file{no:03}.dat',
'data:{};base64,{}'.format( 'data:{};base64,{}'.format(
attachment.mimetype, attachment.mimetype,
attachment.base64(), attachment.base64(),

View File

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

View File

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

View File

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

View File

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

View File

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