mirror of
https://github.com/caronc/apprise.git
synced 2024-11-27 02:23:25 +01:00
more attachment refactoring/alignment
This commit is contained in:
parent
9bd068b274
commit
cda1bd9dd9
@ -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,
|
||||
)
|
||||
|
@ -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)
|
||||
))
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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(
|
||||
|
@ -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
|
||||
|
@ -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(),
|
||||
|
@ -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)
|
||||
|
@ -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')
|
||||
|
@ -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,
|
||||
})
|
||||
|
@ -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(),
|
||||
})
|
||||
|
@ -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,
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user