Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract content from a file with mime multipart

I have a file that contain a tiff image and a document xml in a multipart mime document. I would extract the image from this file. How I can get it?

I have this code, but it requires an infinite time to extract it, if I have a big file (for example 30Mb), so this is unuseful.

f=open("content_file.txt","rb")
msg = email.message_from_file(f)
j=0
image=False
for i in msg.walk():
    if i.is_multipart():
        #print "MULTIPART: "
        continue
    if i.get_content_maintype() == 'text':
        j=j+1
        continue
    if i.get_content_maintype() == 'image':
        image=True
        j=j+1
        pl = i.get_payload(decode=True)
        localFile = open("map.out.tiff", 'wb')
        localFile.write(pl)
        continue
        f.close()
    if (image==False):
        sys.exit(0);

Thank you so much.

like image 901
michele Avatar asked Oct 27 '25 19:10

michele


1 Answers

Solved:

def extract_mime_part_matching(stream, mimetype):
"""Return the first element in a multipart MIME message on stream
matching mimetype."""

msg = mimetools.Message(stream)
msgtype = msg.gettype()
params = msg.getplist()

data = StringIO.StringIO()
if msgtype[:10] == "multipart/":

    file = multifile.MultiFile(stream)
    file.push(msg.getparam("boundary"))
    while file.next():
        submsg = mimetools.Message(file)
        try:
            data = StringIO.StringIO()
            mimetools.decode(file, data, submsg.getencoding())
        except ValueError:
            continue
        if submsg.gettype() == mimetype:
            break
    file.pop()
return data.getvalue()

From: http://docs.python.org/release/2.6.6/library/multifile.html

Thank you for the support.

like image 150
michele Avatar answered Oct 29 '25 07:10

michele



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!