]> git.phdru.name Git - mimedecode.git/commitdiff
Guess filename extension for --save-* from the content type
authorOleg Broytman <phd@phdru.name>
Mon, 17 Mar 2014 19:40:25 +0000 (23:40 +0400)
committerOleg Broytman <phd@phdru.name>
Mon, 17 Mar 2014 19:40:25 +0000 (23:40 +0400)
mimedecode.docbook
mimedecode.py
test/expected-save/msg_07-4.txt [new file with mode: 0644]
test/expected/msg_07-1.txt [new file with mode: 0644]
test/input/msg_07-1.txt [new file with mode: 0644]
test/test_all

index 7d767f3cff55fe7eb7030c46e5952bca3c068724..e8a0b2346a53b187d0f1e3bbb24d725339412702 100644 (file)
@@ -536,9 +536,21 @@ cat input_file | mimedecode.py -o output_file</programlisting>
   counter is prepended to the filename to avoid collisions; if there are no
   name/filename parameters, or the name/filename parameters contain forbidden
   characters (null, slash, backslash) the filename is just the serial counter.
+</para>
+
+<para>
+  If the file doesn't have any extensions (no dots in the value of the
+  name/filename parameters, or the name is just the counter) the program tries
+  to guess an extension by looking up the content type in mime.types files
+  including .mime.types file in the user's home directory (if it exists). If
+  the file has an extension the program doesn't try to verify that it
+  corresponds to the content type.
+</para>
+
+<para>
   The file is saved in the directory set with -O (default is the current
-  directory). The save options are processed before option -e so the user can
-  save the message that causes the error.
+  directory). The save options are proceeded before -e options so the user can
+  save the message that causes an error.
 </para>
 </refsect1>
 
index 50f8a18675a5265700f2f4928208e169dc116f8d..48f6f7e4293963f756d079b5d3ac479a0e312567 100755 (executable)
@@ -1,10 +1,9 @@
 #! /usr/bin/env python
 """Decode MIME message"""
 
-from mimedecode_version import __version__, __author__, __copyright__, __license__
-
 import sys, os
-import email
+from mimedecode_version import __version__, \
+    __author__, __copyright__, __license__
 
 me = os.path.basename(sys.argv[0])
 
@@ -263,6 +262,18 @@ def totext(msg, instring):
     return s
 
 
+mimetypes = None
+
+def _guess_extension(ctype):
+    global mimetypes
+    if mimetypes is None:
+        import mimetypes
+        mimetypes.init()
+        user_mime_type = os.path.expanduser('~/.mime.types')
+        if os.path.exists(user_mime_type):
+            mimetypes._db.read(user_mime_type)
+    return mimetypes.guess_extension(ctype)
+
 def _save_message(msg, outstring, save_headers=False, save_body=False):
     for header, param in (
         ("Content-Disposition", "filename"),
@@ -284,6 +295,9 @@ def _save_message(msg, outstring, save_headers=False, save_body=False):
         fname = ''
     g.save_counter += 1
     fname = str(g.save_counter) + fname
+    if '.' not in fname:
+        ext = _guess_extension(msg.get_content_type())
+        if ext: fname += ext
 
     global output
     save_output = output
@@ -583,6 +597,7 @@ if __name__ == "__main__":
     g.outfile = outfile
     output = outfile.write
 
+    import email
     msg = email.message_from_file(infile)
 
     for header, value in g.set_header_value:
diff --git a/test/expected-save/msg_07-4.txt b/test/expected-save/msg_07-4.txt
new file mode 100644 (file)
index 0000000..0471928
Binary files /dev/null and b/test/expected-save/msg_07-4.txt differ
diff --git a/test/expected/msg_07-1.txt b/test/expected/msg_07-1.txt
new file mode 100644 (file)
index 0000000..9ed3c2f
Binary files /dev/null and b/test/expected/msg_07-1.txt differ
diff --git a/test/input/msg_07-1.txt b/test/input/msg_07-1.txt
new file mode 100644 (file)
index 0000000..aa0dc63
--- /dev/null
@@ -0,0 +1,84 @@
+From test Sat Feb  1 00:00:00 2014
+MIME-Version: 1.0
+From: Barry <barry@digicool.com>
+To: Dingus Lovers <cravindogs@cravindogs.com>
+Subject: Here is your dingus fish
+Date: Fri, 20 Apr 2001 19:35:02 -0400
+Content-Type: multipart/mixed; boundary="BOUNDARY"
+
+--BOUNDARY
+Content-Type: text/plain; charset="us-ascii"
+
+Hi there,
+
+This is the dingus fish.
+
+--BOUNDARY
+Content-Type: image/gif; name="dingusfish"
+Content-Transfer-Encoding: base64
+content-disposition: attachment; filename="dingusfish"
+
+R0lGODdhAAEAAfAAAP///wAAACwAAAAAAAEAAQAC/oSPqcvtD6OctNqLs968+w+G4kiW5omm6sq2
+7gvH8kzX9o3n+s73/g8MCofEovGITGICTKbyCV0FDNOo9SqpQqpOrJfXzTQj2vD3TGtqL+NtGQ2f
+qTXmx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eRozg3ULhhr9lAGtVEkCg5ZNRuuVleBPaZadhG0ZgkyPmDOTOKzViM
+YgOcpukKqQcbjAWS0IleQ2ROjdh6A+md1qWdBRSX7iSYgFRTtRmBpJioieXJiHfJiMGIR9fJOn8I
+MSfXYhspn4ooSa2mSAj4n+8Bmg03fBJZoPOJgsVZRxu1oOMRPXYYjdqjihFaEoZpXBREanuJoRI6
+cibFinq4ngUKh/wQd/H5ofYCZ0HJXR62opZFaAT0iFIZo4DIiUojkjeqKiuoZirKo5Y1a7AWckGa
+BkuYoD5lpDK6eUs6CkDqpETwl1EqpfhJpVeKpVl6EgUAADs=
+
+--BOUNDARY--
index 5009f703488eac884da382282763fb4c61663d18..c6a5498f0e6b2756491ec667d73c85f8c26855f4 100755 (executable)
@@ -93,6 +93,7 @@ test_save() {
 test_save msg_07.txt msg_07.txt 1-dingusfish.gif msg_07-1.txt --save-headers image/gif
 test_save msg_07.txt msg_07.txt 1-dingusfish.gif msg_07-2.txt --save-body image/gif
 test_save msg_07.txt msg_07.txt 1-dingusfish.gif msg_07-3.txt --save-message image/gif
+test_save msg_07-1.txt msg_07-1.txt 1-dingusfish.gif msg_07-4.txt --save-message image/gif
 
 if [ "$RC" -eq 0 ]; then
    echo "All tests passed!"