Update imap_tasks.py
Added some filtering for file types to avoid your usual image attachment to trigger a conversion task and clutter my documents
This commit is contained in:
committed by
GitHub
parent
d8aacebce1
commit
473ae3a20a
+38
-5
@@ -240,13 +240,39 @@ def pull_inbox(mailbox_key, host, port, username, password, use_ssl, delete_afte
|
|||||||
|
|
||||||
def fetch_attachments_and_enqueue(email_message):
|
def fetch_attachments_and_enqueue(email_message):
|
||||||
"""
|
"""
|
||||||
Extracts attachments from the email.
|
Extracts attachments from the email and processes only allowed file types.
|
||||||
|
|
||||||
- If the attachment is a PDF (MIME type "application/pdf"), it is enqueued for upload.
|
Allowed file types include:
|
||||||
- For any other attachment, a conversion task is enqueued that converts it to PDF.
|
- PDF: application/pdf
|
||||||
|
- Microsoft Office files:
|
||||||
|
- Word: application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document
|
||||||
|
- Excel: application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
|
||||||
|
- PowerPoint: application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation
|
||||||
|
- Other meaningful attachments:
|
||||||
|
- Plain text: text/plain
|
||||||
|
- CSV: text/csv
|
||||||
|
- Rich Text Format: application/rtf, text/rtf
|
||||||
|
|
||||||
Returns True if at least one attachment was processed.
|
- If the attachment is a PDF, it is enqueued for upload.
|
||||||
|
- For any other allowed file, a conversion task is enqueued to convert it to PDF.
|
||||||
|
|
||||||
|
Returns True if at least one allowed attachment was processed.
|
||||||
"""
|
"""
|
||||||
|
# Define allowed MIME types for office files and other meaningful attachments.
|
||||||
|
ALLOWED_MIME_TYPES = {
|
||||||
|
"application/pdf",
|
||||||
|
"application/msword",
|
||||||
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||||
|
"application/vnd.ms-excel",
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
"application/vnd.ms-powerpoint",
|
||||||
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||||
|
"text/plain",
|
||||||
|
"text/csv",
|
||||||
|
"application/rtf",
|
||||||
|
"text/rtf",
|
||||||
|
}
|
||||||
|
|
||||||
has_attachment = False
|
has_attachment = False
|
||||||
for part in email_message.walk():
|
for part in email_message.walk():
|
||||||
if part.get_content_maintype() == "multipart":
|
if part.get_content_maintype() == "multipart":
|
||||||
@@ -256,11 +282,17 @@ def fetch_attachments_and_enqueue(email_message):
|
|||||||
if not filename:
|
if not filename:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Check the MIME type of the attachment.
|
||||||
|
mime_type = part.get_content_type()
|
||||||
|
if mime_type not in ALLOWED_MIME_TYPES:
|
||||||
|
logger.info(f"Skipping attachment {filename} with MIME type {mime_type}")
|
||||||
|
continue
|
||||||
|
|
||||||
file_path = os.path.join(settings.workdir, filename)
|
file_path = os.path.join(settings.workdir, filename)
|
||||||
with open(file_path, "wb") as f:
|
with open(file_path, "wb") as f:
|
||||||
f.write(part.get_payload(decode=True))
|
f.write(part.get_payload(decode=True))
|
||||||
|
|
||||||
if part.get_content_type() == "application/pdf":
|
if mime_type == "application/pdf":
|
||||||
upload_to_s3.delay(file_path)
|
upload_to_s3.delay(file_path)
|
||||||
logger.info(f"Enqueued PDF for upload: {filename}")
|
logger.info(f"Enqueued PDF for upload: {filename}")
|
||||||
else:
|
else:
|
||||||
@@ -272,6 +304,7 @@ def fetch_attachments_and_enqueue(email_message):
|
|||||||
return has_attachment
|
return has_attachment
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def email_already_has_label(mail, msg_id, label="Ingested"):
|
def email_already_has_label(mail, msg_id, label="Ingested"):
|
||||||
"""
|
"""
|
||||||
Checks if the given message (msg_id) has the specified Gmail label.
|
Checks if the given message (msg_id) has the specified Gmail label.
|
||||||
|
|||||||
Reference in New Issue
Block a user