tnc-ca-geo / animl-email-relay

Lambda service for extracting camera trap images from email attachments
Other
0 stars 0 forks source link

Fix TypeError in helpers.py #15

Closed nathanielrindlaub closed 1 year ago

nathanielrindlaub commented 1 year ago

Hey Falk,

I can send you the email that triggers this if it's helpful, but I'm also getting the following error when I try to test cuddelink emails in the dev environment:

[ERROR] TypeError: a bytes-like object is required, not 'str'
Traceback (most recent call last):
  File "/var/task/serverless_sdk/__init__.py", line 144, in wrapped_handler
    return user_handler(event, context)
  File "/var/task/lambda_cache/caching_logic.py", line 30, in inner_function
    return func(event, context)
  File "src/handler.py", line 88, in handler
    for image in camera.images():
  File "src/cameras.py", line 87, in images
    for image in self.get_images():
  File "src/cameras.py", line 136, in get_images
    return helpers.save_attached_images(self.email)
  File "src/helpers.py", line 127, in save_attached_images
    handle.write(part.get_content())
nathanielrindlaub commented 1 year ago

Looks like this is because Cuddelink sends emails that include status report attachments as HTML files (e.g. the temp filename would be something like /tmp/tmp_ytv4snc/S_00001.HTML). In the previous code I had been filtering out all attachments that weren't .jpgs so I'll add that back in, but perhaps @postfalk we can add a test (or tests) to make sure the code can handle bad emails? Off the top of my head, we should be able to gracefully handle:

postfalk commented 1 year ago

Thank you. Yes, it would be great to have an example. Maybe we should also test whether the attachment is an image. E.g. by just opening it with PIL. However, there will be some whack-a-mole as we introduce more payload formats.