From cda1bd9dd9a4639d584586eb750968b7a260a5ff Mon Sep 17 00:00:00 2001 From: Chris Caron Date: Tue, 27 Aug 2024 18:54:52 -0400 Subject: [PATCH] more attachment refactoring/alignment --- apprise/plugins/apprise_api.py | 9 ++++++--- apprise/plugins/custom_form.py | 3 ++- apprise/plugins/custom_json.py | 2 +- apprise/plugins/email.py | 8 ++++++-- apprise/plugins/mailgun.py | 8 +++++++- apprise/plugins/pushbullet.py | 5 +++-- apprise/plugins/pushsafer.py | 5 +++-- apprise/plugins/ses.py | 7 +++++-- apprise/plugins/slack.py | 5 +++-- apprise/plugins/smtp2go.py | 5 +++-- apprise/plugins/sparkpost.py | 5 +++-- apprise/plugins/twitter.py | 10 +++++++--- 12 files changed, 49 insertions(+), 23 deletions(-) diff --git a/apprise/plugins/apprise_api.py b/apprise/plugins/apprise_api.py index eda41b6c..d6156438 100644 --- a/apprise/plugins/apprise_api.py +++ b/apprise/plugins/apprise_api.py @@ -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, ) diff --git a/apprise/plugins/custom_form.py b/apprise/plugins/custom_form.py index 05fe51d1..e9ffcbbb 100644 --- a/apprise/plugins/custom_form.py +++ b/apprise/plugins/custom_form.py @@ -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) )) diff --git a/apprise/plugins/custom_json.py b/apprise/plugins/custom_json.py index 84ad2c21..03585c9e 100644 --- a/apprise/plugins/custom_json.py +++ b/apprise/plugins/custom_json.py @@ -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 diff --git a/apprise/plugins/email.py b/apprise/plugins/email.py index f720c426..2e423916 100644 --- a/apprise/plugins/email.py +++ b/apprise/plugins/email.py @@ -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 diff --git a/apprise/plugins/mailgun.py b/apprise/plugins/mailgun.py index 4b73957a..b6818395 100644 --- a/apprise/plugins/mailgun.py +++ b/apprise/plugins/mailgun.py @@ -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( diff --git a/apprise/plugins/pushbullet.py b/apprise/plugins/pushbullet.py index 9f2226f3..2b88bbed 100644 --- a/apprise/plugins/pushbullet.py +++ b/apprise/plugins/pushbullet.py @@ -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 diff --git a/apprise/plugins/pushsafer.py b/apprise/plugins/pushsafer.py index da4277de..dd5a6c82 100644 --- a/apprise/plugins/pushsafer.py +++ b/apprise/plugins/pushsafer.py @@ -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(), diff --git a/apprise/plugins/ses.py b/apprise/plugins/ses.py index 5fe4a369..30e59702 100644 --- a/apprise/plugins/ses.py +++ b/apprise/plugins/ses.py @@ -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) diff --git a/apprise/plugins/slack.py b/apprise/plugins/slack.py index fb4a8b6e..11d78e4c 100644 --- a/apprise/plugins/slack.py +++ b/apprise/plugins/slack.py @@ -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') diff --git a/apprise/plugins/smtp2go.py b/apprise/plugins/smtp2go.py index 2781baa4..a19e523f 100644 --- a/apprise/plugins/smtp2go.py +++ b/apprise/plugins/smtp2go.py @@ -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, }) diff --git a/apprise/plugins/sparkpost.py b/apprise/plugins/sparkpost.py index 13e081cf..4e4233ca 100644 --- a/apprise/plugins/sparkpost.py +++ b/apprise/plugins/sparkpost.py @@ -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(), }) diff --git a/apprise/plugins/twitter.py b/apprise/plugins/twitter.py index 369aaac0..6d352ea6 100644 --- a/apprise/plugins/twitter.py +++ b/apprise/plugins/twitter.py @@ -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, })