This is an automated email from the ASF dual-hosted git repository.
humbedooh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-ponymail-foal.git
The following commit(s) were added to refs/heads/master by this push:
new 12e11b6 use mimetypes to invent a better attachment name
12e11b6 is described below
commit 12e11b6209c735ea887c6fd30bcd2d5934241d44
Author: Daniel Gruno <[email protected]>
AuthorDate: Thu Jan 6 18:47:37 2022 +0100
use mimetypes to invent a better attachment name
---
tools/archiver.py | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/tools/archiver.py b/tools/archiver.py
index af491b6..986dadb 100755
--- a/tools/archiver.py
+++ b/tools/archiver.py
@@ -54,6 +54,7 @@ import time
import traceback
import typing
import uuid
+import mimetypes
import elasticsearch
import formatflowed
@@ -136,9 +137,13 @@ def parse_attachment(
if cdtype in {"attachment", "inline"}:
fd = part.get_payload(decode=True)
filename = part.get_filename()
- # If inline-attached email, fake a name
- if not filename and part.get_content_type() and
part.get_content_type().lower() == "message/rfc822":
- filename = "attached_email.eml"
+ # If attachment is without a name, invent it.
+ ctype = part.get_content_type()
+ if ctype and not filename:
+ ext = mimetypes.guess_extension(ctype)
+ if not ext: # dunno this extension, fake .txt
+ ext = ".txt"
+ filename = f"{cdtype}{ext}"
if not fd and cdtype == "inline": # If inline, convert to
source
fd = part.as_bytes()
# Allow for empty string