Skip to content

Commit

Permalink
bugfix: APPRISE_ATTACH_SIZE correctly manages attachment size (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
caronc authored Mar 13, 2024
1 parent 6e29103 commit 4bf5093
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
10 changes: 6 additions & 4 deletions apprise_api/api/tests/test_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ def test_form_file_attachment_parsing(self):

# Test a case where our attachment exceeds the maximum size we allow
# for
with override_settings(APPRISE_MAX_ATTACHMENT_SIZE=1):
with override_settings(APPRISE_ATTACH_SIZE=1):
files_request = {
'file1': SimpleUploadedFile(
"attach.txt",
b"some content more then 1 byte in length to pass along.",
# More then 1 MB in size causing error to trip
("content" * 1024 * 1024).encode('utf-8'),
content_type="text/plain")
}
with self.assertRaises(ValueError):
Expand Down Expand Up @@ -324,9 +325,10 @@ def test(*args, **kwargs):

# Test a case where our attachment exceeds the maximum size we allow
# for
with override_settings(APPRISE_MAX_ATTACHMENT_SIZE=1):
with override_settings(APPRISE_ATTACH_SIZE=1):
# More then 1 MB in size causing error to trip
attachment_payload = \
b"some content more then 1 byte in length to pass along."
("content" * 1024 * 1024).encode('utf-8')
with self.assertRaises(ValueError):
parse_attachments(attachment_payload, {})

Expand Down
8 changes: 4 additions & 4 deletions apprise_api/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ def parse_attachments(attachment_payload, files_request):
#
# Some Validation
#
if settings.APPRISE_MAX_ATTACHMENT_SIZE > 0 and \
attachment.size > settings.APPRISE_MAX_ATTACHMENT_SIZE:
if settings.APPRISE_ATTACH_SIZE > 0 and \
attachment.size > settings.APPRISE_ATTACH_SIZE:
raise ValueError(
"attachment %s's filesize is to large" % filename)

Expand Down Expand Up @@ -387,8 +387,8 @@ def parse_attachments(attachment_payload, files_request):
#
# Some Validation
#
if settings.APPRISE_MAX_ATTACHMENT_SIZE > 0 and \
attachment.size > settings.APPRISE_MAX_ATTACHMENT_SIZE:
if settings.APPRISE_ATTACH_SIZE > 0 and \
attachment.size > settings.APPRISE_ATTACH_SIZE:
raise ValueError(
"attachment %s's filesize is to large" % filename)

Expand Down
12 changes: 6 additions & 6 deletions apprise_api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,11 @@ def post(self, request, key):
attach = parse_attachments(
content.get('attachment'), request.FILES)

except (TypeError, ValueError):
except (TypeError, ValueError) as e:
# Invalid entry found in list
logger.warning(
'NOTIFY - %s - Bad attachment specified',
request.META['REMOTE_ADDR'])
'NOTIFY - %s - Bad attachment: %s',
request.META['REMOTE_ADDR'], str(e))

return HttpResponse(
_('Bad attachment'),
Expand Down Expand Up @@ -1177,11 +1177,11 @@ def post(self, request):
attach = parse_attachments(
content.get('attachment'), request.FILES)

except (TypeError, ValueError):
except (TypeError, ValueError) as e:
# Invalid entry found in list
logger.warning(
'NOTIFY - %s - Bad attachment specified',
request.META['REMOTE_ADDR'])
'NOTIFY - %s - Bad attachment: %s',
request.META['REMOTE_ADDR'], str(e))

return HttpResponse(
_('Bad attachment'),
Expand Down
7 changes: 1 addition & 6 deletions apprise_api/core/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
APPRISE_ATTACH_DIR = os.environ.get(
'APPRISE_ATTACH_DIR', os.path.join(BASE_DIR, 'var', 'attach'))

# The file attachment size allowed by the API
# The maximum file attachment size allowed by the API (defined in MB)
APPRISE_ATTACH_SIZE = int(os.environ.get('APPRISE_ATTACH_SIZE', 200)) * 1048576

# When set Apprise API Locks itself down so that future (configuration)
Expand Down Expand Up @@ -205,8 +205,3 @@
# Define the number of attachments that can exist as part of a payload
# Setting this to zero disables the limit
APPRISE_MAX_ATTACHMENTS = int(os.environ.get('APPRISE_MAX_ATTACHMENTS', 6))

# Defines the maximum size each attachment can be
# 8388608 == 8MB
APPRISE_MAX_ATTACHMENT_SIZE = int(
os.environ.get('APPRISE_MAX_ATTACHMENT_SIZE', 8388608))

0 comments on commit 4bf5093

Please sign in to comment.