Example #1
0
    def parse_attachments_poptres(self, content_disposition, part):
        dispositions = content_disposition.strip().split(";")
        if bool(content_disposition and dispositions[0].lower() == "attachment"):

            file_data = part.get_payload(decode=True)
            attachment = StringIO(file_data)
            attachment.content_type = part.get_content_type()
            attachment.size = len(file_data)
            attachment.name = None
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None

            for param in dispositions[1:]:
                name, value = param.split("=")
                name = name.lower().strip()
                value = value.replace('"', '').strip()

                if name == "filename":
                    attachment.name = value
                elif name == "create-date":
                    attachment.create_date = value
                elif name == "modification-date":
                    attachment.mod_date = value
                elif name == "read-date":
                    attachment.read_date = value

            attachment.seek(0, 2)
            f = InMemoryUploadedFile(attachment, "", attachment.name, attachment.content_type, attachment.tell(), None)

            atch = Attachment()
            atch.user = self.usr
            atch.file.save(attachment.name, f)
            atch.save()
            return atch
Example #2
0
 def parse_attachment(self, message_part):
     content_disposition = message_part.get("Content-Disposition", None)
     if content_disposition:
         dispo_type, dispo_dict = self.parse_dispositions(content_disposition)
         if dispo_type == "attachment" or (dispo_type == 'inline' and
                 'filename' in dispo_dict):
             file_data = message_part.get_payload(decode=True)
             if file_data is None:
                 file_data = ""
             attachment = StringIO(file_data)
             attachment.content_type = message_part.get_content_type()
             attachment.size = len(file_data)
             attachment.name = None
             attachment.create_date = None
             attachment.mod_date = None
             attachment.read_date = None
             if "filename" in dispo_dict:
                 attachment.name = dispo_dict['filename']
             else:
                 content_type = message_part.get("Content-Type", None)
                 if content_type:
                     _, content_dict = self.parse_dispositions(content_type)
                     if 'name' in content_dict:
                         attachment.name = content_dict['name']
             if "create-date" in dispo_dict:
                 attachment.create_date = dispo_dict['create-date']  # TODO: datetime
             if "modification-date" in dispo_dict:
                 attachment.mod_date = dispo_dict['modification-date']  # TODO: datetime
             if "read-date" in dispo_dict:
                 attachment.read_date = dispo_dict['read-date']  # TODO: datetime
             return attachment
     return None
Example #3
0
def resize_image(image,size=(800,600)):
    """Utility function to make thumbnails of images"""
    im = Image.open(image)
    output_buff = StringIO()
    width,height = im.size

    if width > height:
        delta = width-height
        left = int(delta/2)
        upper = 0
        right = height+left
        lower = height
    else:
        delta = height-width
        left = 0
        upper = int(delta/2)
        right = width
        lower = width+upper
    
    im.thumbnail(size, Image.ANTIALIAS)

    im.convert('RGB').save(output_buff, format = 'jpeg', quality=100)
    output_buff.seek(0)
    output_buff.content_type = 'image/jpeg'
    return output_buff
Example #4
0
    def get_attachment(self, msg, attach_id):
        "Get and return an attachment"
        num = 0
        attach_id = int(attach_id)

        for part in msg.walk():
            attachment = part.get_param('attachment',
                        NOTFOUND, 'Content-Disposition')
            if not attachment is NOTFOUND:
                filename = part.get_filename(None)
                if filename:
                    filename = re.sub(r"\s", "_", filename)
                    num += 1
                if attach_id == num:
                    from StringIO import StringIO
                    if part.is_multipart():
                        data = part.as_string()
                    else:
                        data = part.get_payload(decode=True)
                    attachment  = StringIO(data)
                    attachment.content_type =  part.get_content_type()
                    attachment.size = len(data)
                    attachment.name = filename
                    return attachment
        return None
Example #5
0
	def parse_attachment(self, message_part):
		content_disposition = message_part.get("Content-Disposition", None)
		if content_disposition:
			dispositions = content_disposition.strip().split(";")
			if bool(content_disposition and dispositions[0].lower() == "attachment"):
 
				file_data = message_part.get_payload(decode=True)
				attachment = StringIO()
				attachment.write(file_data)
				attachment.content_type = message_part.get_content_type()
				attachment.size = len(file_data)
				attachment.name = None
				attachment.create_date = None
				attachment.mod_date = None
				attachment.read_date = None
				
				
				for param in dispositions[1:]:
					name,value = param.split("=")
					name = name.lower()
					
					attachment.name = value.replace('"', '')
 
				return attachment
 
		return None
    def migrate(self, field):
        """ Migrate OFS.Image to blob
        """
        storage = field.getStorage()
        try:
            zfile = storage.get(self.field, self.context)
        except AttributeError:
            logger.info('\t There is no %s to migrate', self.field)
            return False

        if not isinstance(zfile, self.ztype):
            logger.info('\t %s is already a Blob', self.field)
            return False

        data = StringIO(zfile.data)
        filename = getattr(zfile, 'filename', self.context.getId())
        if isinstance(filename, unicode):
            filename = filename.encode('utf-8')
        data.filename = filename

        ctype = getattr(zfile, 'content_type', None)
        if ctype:
            if isinstance(ctype, unicode):
                ctype = ctype.encode('utf-8')
            data.content_type = ctype

        field.getMutator(self.context)(data)
        logger.info('\t %s is now a Blob', self.field)
        return True
Example #7
0
    def parse_attachments_poptres(self, content_disposition, part):
        dispositions = content_disposition.strip().split(";")
        if bool(content_disposition and dispositions[0].lower() == "attachment"):

            file_data = part.get_payload(decode=True)
            attachment = StringIO(file_data)
            attachment.content_type = part.get_content_type()
            attachment.size = len(file_data)
            attachment.name = None
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None

            for param in dispositions[1:]:
                name, value = param.split("=")
                name = name.lower().strip()
                value = value.replace('"', '').strip()

                if name == "filename":
                    attachment.name = value
                elif name == "create-date":
                    attachment.create_date = value
                elif name == "modification-date":
                    attachment.mod_date = value
                elif name == "read-date":
                    attachment.read_date = value

            attachment.seek(0, 2)
            f = InMemoryUploadedFile(attachment, "", attachment.name, attachment.content_type, attachment.tell(), None)

            atch = Attachment()
            atch.user = self.usr
            atch.file.save(attachment.name, f)
            atch.save()
            return atch
    def test_import(self, browser):
        file_data = open(
            "%s/assets/redirector_config.xlsx" % os.path.split(__file__)[0],
            'r')
        file_ = StringIO(file_data.read())
        file_.filename = 'redirector_config.xlsx'
        file_.content_type = 'application/vnd.ms-excel'

        self.grant('Manager')
        browser.login().open(IRedirectConfig(self.portal), view='import')
        browser.fill({'Excel redirect config': file_}).submit()

        self.assertEquals([{
            'destination': u'/Plone',
            'source_path': u'/theploneasdf'
        }, {
            'destination': u'http://www.google.ch/',
            'source_path': u'/google'
        }, {
            'destination': u'/ziel',
            'source_path': u'/test'
        }, {
            'destination': u'/blub',
            'source_path': u'/bla'
        }, {
            'destination': u'/gnarg',
            'source_path': u'/gna'
        }, {
            'destination': u'/same',
            'source_path': u'/same'
        }],
                          IRedirectConfig(self.portal).rules)
Example #9
0
def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        dispositions = content_disposition.strip().split(";")
        if bool(content_disposition and dispositions[0].lower() == "attachment"):

            file_data = message_part.get_payload(decode=True)
            attachment = StringIO(file_data)
            attachment.content_type = message_part.get_content_type()
            attachment.size = len(file_data)
            attachment.name = None
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None

            for param in dispositions[1:]:
                name,value = param.split("=")
                name = name.lower()

                if name == "filename":
                    attachment.name = value
                elif name == "create-date":
                    attachment.create_date = value  #TODO: datetime
                elif name == "modification-date":
                    attachment.mod_date = value #TODO: datetime
                elif name == "read-date":
                    attachment.read_date = value #TODO: datetime
            return attachment

    return None
Example #10
0
    def migrate(self, field):
        """ Migrate OFS.Image to blob
        """
        storage = field.getStorage()
        try:
            zfile = storage.get(self.field, self.context)
        except AttributeError:
            logger.info('\t There is no %s to migrate', self.field)
            return False

        if not isinstance(zfile, self.ztype):
            logger.info('\t %s is already a Blob', self.field)
            return False

        data = StringIO(zfile.data)
        filename = getattr(zfile, 'filename', self.context.getId())
        if isinstance(filename, unicode):
            filename = filename.encode('utf-8')
        data.filename = filename

        ctype = getattr(zfile, 'content_type', None)
        if ctype:
            if isinstance(ctype, unicode):
                ctype = ctype.encode('utf-8')
            data.content_type = ctype

        field.getMutator(self.context)(data)
        logger.info('\t %s is now a Blob', self.field)
        return True
Example #11
0
def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        dispositions = content_disposition.strip().split(";")
        if bool(content_disposition
                and dispositions[0].lower() == "attachment"):

            file_data = message_part.get_payload(decode=True)
            # Used a StringIO object since PIL didn't seem to recognize
            # images using a custom file-like object
            attachment = StringIO(file_data)
            attachment.content_type = message_part.get_content_type()
            attachment.size = len(file_data)
            attachment.name = None
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None
            # print dispositions
            for param in dispositions[1:]:
                name, value = param.split("=")
                name = name.strip().lower()

                if name == "filename":
                    attachment.name = value
                elif name in ["create-date", "creation-date"]:
                    attachment.create_date = value  #TODO: datetime
                elif name == "modification-date":
                    attachment.mod_date = value  #TODO: datetime
                elif name == "read-date":
                    attachment.read_date = value  #TODO: datetime
            return attachment

    return None
Example #12
0
    def parse_attachment(self, message_part):
        content_disposition = message_part.get("Content-Disposition", None)
        if content_disposition:
            dispo_type, dispo_dict = self.parse_dispositions(
                content_disposition)
            if dispo_type == "attachment" or (dispo_type == 'inline'
                                              and 'filename' in dispo_dict):
                file_data = message_part.get_payload(decode=True)
                if file_data is None:
                    file_data = ""
                attachment = StringIO(file_data)
                attachment.content_type = message_part.get_content_type()
                attachment.size = len(file_data)
                attachment.name = None
                attachment.create_date = None
                attachment.mod_date = None
                attachment.read_date = None

                if "filename" in dispo_dict:
                    attachment.name = self.parse_header_field(
                        dispo_dict['filename'])
                elif "create-date" in dispo_dict:
                    attachment.create_date = dispo_dict[
                        'create-date']  # TODO: datetime
                elif "modification-date" in dispo_dict:
                    attachment.mod_date = dispo_dict[
                        'modification-date']  # TODO: datetime
                elif "read-date" in dispo_dict:
                    attachment.read_date = dispo_dict[
                        'read-date']  # TODO: datetime
                return attachment
        return None
Example #13
0
    def test_stream_syntax(self, browser):
        file_ = StringIO('file data')
        file_.filename = 'foo.txt'
        file_.content_type = 'text/plain'

        browser.login(SITE_OWNER_NAME).open()
        factoriesmenu.add('File')
        browser.fill({'Title': 'foo',
                      'file_file': file_}).submit()

        browser.find('foo.txt').click()
        self.assert_file_download('file data', browser)
Example #14
0
 def test_comment_image_upload_invalid(self):
     """
     comment image upload, invalid image
     """
     utils.login(self)
     image = StringIO('BAD\x02D\x01\x00;')
     image.name = 'image.gif'
     image.content_type = 'image/gif'
     files = {'image': SimpleUploadedFile(image.name, image.read()), }
     response = self.client.post(reverse('spirit:comment-image-upload-ajax'),
                                 HTTP_X_REQUESTED_WITH='XMLHttpRequest',
                                 data=files)
     res = json.loads(response.content)
     self.assertIn('error', res.keys())
     self.assertIn('image', res['error'].keys())
Example #15
0
 def test_comment_image_upload_invalid(self):
     """
     comment image upload, invalid image
     """
     utils.login(self)
     image = StringIO('BAD\x02D\x01\x00;')
     image.name = 'image.gif'
     image.content_type = 'image/gif'
     files = {'image': SimpleUploadedFile(image.name, image.read()), }
     response = self.client.post(reverse('spirit:comment-image-upload-ajax'),
                                 HTTP_X_REQUESTED_WITH='XMLHttpRequest',
                                 data=files)
     res = json.loads(response.content)
     self.assertIn('error', res.keys())
     self.assertIn('image', res['error'].keys())
Example #16
0
    def test_empty_body_html(self, mock_ml_get, mock_ci_get):
        """ TLT-2006
        Verifies that we can handle emails that
        * have at least one inlined attachment
        * do not have an html body
        """
        # prep an attachment object (attributes added to match what
        # django.test.client.encode_file() looks for)
        attachment_fp = StringIO("lorem ipsum")
        attachment_fp.name = "lorem.txt"
        attachment_fp.content_type = "text/plain"

        # prep a MailingList mock
        members = [{"address": a} for a in ["*****@*****.**", "*****@*****.**"]]
        ml = MagicMock(
            canvas_course_id=123,
            section_id=456,
            teaching_staff_addresses={"*****@*****.**"},
            members=members,
            address="*****@*****.**",
        )
        mock_ml_get.return_value = ml

        # prep a CourseInstance mock
        ci = MagicMock(course_instance_id=789, canvas_course_id=ml.canvas_course_id, short_title="Lorem For Beginners")
        mock_ci_get.return_value = ci

        # prep the post body
        post_body = {
            "sender": "Unit Test <*****@*****.**>",
            "recipient": ml.address,
            "subject": "blah",
            "body-plain": "blah [cid:{}] blah".format(attachment_fp.name),
            "To": ml.address,
            "attachment-count": 1,
            "attachment-1": attachment_fp,
            "content-id-map": json.dumps({attachment_fp.name: "attachment-1"}),
        }
        post_body.update(generate_signature_dict())

        # prep the request
        request = self.factory.post("/", post_body)
        request.user = self.user

        # run the view, verify success
        response = handle_mailing_list_email_route(request)
        self.assertEqual(response.status_code, 200)
Example #17
0
def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        #print content_disposition
        dispositions = content_disposition.strip().split(";")
        if bool(content_disposition
                and dispositions[0].lower() == "attachment") or bool(
                    content_disposition
                    and dispositions[0].lower() == "inline"):

            file_data = message_part.get_payload(decode=True)
            # Used a StringIO object since PIL didn't seem to recognize
            # images using a custom file-like object
            attachment = StringIO(file_data)
            attachment.content_type = message_part.get_content_type()
            attachment.size = len(file_data)
            attachment.name = None
            attachment.create_date = None
            attachment.mod_date = None
            attachment.read_date = None

            for param in dispositions[1:]:
                param = param.strip()
                name, value = param.split("=", 1)
                name = name.lower()

                if name == "filename":
                    h = email.Header.Header(value.lstrip('"').rstrip('"'))
                    dh = email.Header.decode_header(h)
                    fname = dh[0][0]
                    if dh[0][1] != None:
                        fname = fname.decode(dh[0][1])
                    attachment.name = fname
                    #print fname
                elif name == "create-date":
                    attachment.create_date = value  #TODO: datetime
                elif name == "modification-date":
                    attachment.mod_date = value  #TODO: datetime
                elif name == "read-date":
                    attachment.read_date = value  #TODO: datetime
            return attachment

    return None
    def test_ticket_attachment_in_zip_file(self, browser):
        ticket = create(Builder('ticket')
                        .titled(u'Ticket')
                        .within(self.ticketbox))

        file_ = StringIO('GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00'
                         '\x00\x00\x00!\xf9\x04\x04\x00\x00\x00\x00,\x00'
                         '\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02D\x01\x00;')
        file_.filename = 'im\xc3\xa4ge.gif'
        file_.content_type = 'image/gif'

        browser.login()
        browser.visit(ticket, view='edit')
        browser.fill({'attachment_file': file_}).submit()

        browser.visit(self.ticketbox, view='zip_export')
        zipfile = ZipFile(StringIO(browser.contents))
        image = zipfile.read(u'Ticket/image.gif')
        self.assertEqual(file_.getvalue(), image)
def parse_attachment(message_part, attachments=None):
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        try:
            cd = parse_headers(content_disposition, relaxed=True)
            if cd.disposition.lower() == "attachment":
                if not cd.assocs.has_key("filename"):
                    #print error or warning?
                    return None
                else:
                    file_data = message_part.get_payload(decode=True)
                    if not file_data:
                        payload = message_part.get_payload()
                        if isinstance(payload, list):
                            for msgobj in payload:
                                parse2(msgobj, attachments)
                            return None
                        print >>sys.stderr, message_part.get_payload()
                        print >>sys.stderr, message_part.get_content_charset()
                    attachment = StringIO(file_data)
                    attachment.content_type = message_part.get_content_type()
                    attachment.size = len(file_data)
                    attachment.name = cd.assocs['filename']
                    attachment.create_date = None
                    attachment.mod_date = None
                    attachment.read_date = None 
                    
                    for name, value in cd.assocs.iteritems():
                        if name == "create-date":
                            attachment.create_date = value  #TODO: datetime
                        elif name == "modification-date":
                            attachment.mod_date = value #TODO: datetime
                        elif name == "read-date":
                            attachment.read_date = value #TODO: datetime
                    
                    return attachment
                            
        except:
            print >>sys.stderr, "content_disposition:", content_disposition
            raise
    return None
    def test_import(self, browser):
        file_data = open("%s/assets/redirector_config.xlsx" % os.path.split(__file__)[0], "r")
        file_ = StringIO(file_data.read())
        file_.filename = "redirector_config.xlsx"
        file_.content_type = "application/vnd.ms-excel"

        self.grant("Manager")
        browser.login().open(IRedirectConfig(self.portal), view="import")
        browser.fill({"Excel redirect config": file_}).submit()

        self.assertEquals(
            [
                {"destination": u"/Plone", "source_path": u"/theploneasdf"},
                {"destination": u"http://www.google.ch/", "source_path": u"/google"},
                {"destination": u"/ziel", "source_path": u"/test"},
                {"destination": u"/blub", "source_path": u"/bla"},
                {"destination": u"/gnarg", "source_path": u"/gna"},
                {"destination": u"/same", "source_path": u"/same"},
            ],
            IRedirectConfig(self.portal).rules,
        )
Example #21
0
def parse_attachment(message_part, attachments=None):
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        try:
            cd = parse_headers(content_disposition, relaxed=True)
            if cd.disposition.lower() == "attachment":
                if not "filename" in cd.assocs:
                    #print error or warning?
                    return None
                else:
                    file_data = message_part.get_payload(decode=True)
                    if not file_data:
                        payload = message_part.get_payload()
                        if isinstance(payload, list):
                            for msgobj in payload:
                                _parse2(msgobj, attachments)
                        return None  # PSIPHON: fixed conditional return
                    attachment = StringIO(file_data)
                    attachment.content_type = message_part.get_content_type()
                    attachment.size = len(file_data)
                    attachment.name = cd.assocs['filename']
                    attachment.create_date = None
                    attachment.mod_date = None
                    attachment.read_date = None

                    for name, value in cd.assocs.iteritems():
                        if name == "create-date":
                            attachment.create_date = value  # TODO: datetime
                        elif name == "modification-date":
                            attachment.mod_date = value  # TODO: datetime
                        elif name == "read-date":
                            attachment.read_date = value  # TODO: datetime

                    return attachment

        except:
            print >> sys.stderr, "content_disposition:", content_disposition
            raise
    return None
Example #22
0
    def get_attachment(self, msg, attach_id):
        "Get and return an attachment"
        num = 0
        attach_id = int(attach_id)

        for part in msg.walk():
            attachment = part.get_param('attachment',
                        NOTFOUND, 'Content-Disposition')
            if not attachment is NOTFOUND:
                filename = part.get_filename(None)
                if filename:
                    filename = filename.replace(' ', '_')
                    num += 1
                if attach_id == num:
                    if part.is_multipart():
                        data = part.as_string()
                    else:
                        data = part.get_payload(decode=True)
                    attachment = StringIO(data)
                    attachment.content_type = part.get_content_type()
                    attachment.size = len(data)
                    attachment.name = filename
                    return attachment
        return None
Example #23
0
 def test_mimetype(self):
     content = StringIO(u'test')
     content.content_type = 'text/plain'
     b = Blob(content)
     assert 'mimetype' in b.meta
     assert b.meta['mimetype'] == u'text/plain'
Example #24
0
def make_file_like(name='file', content='data'):
    sio = StringIO(content)
    sio.filename = name
    sio.content_type = 'text/html'
    return sio
Example #25
0
def make_file_like(name='file', content='data'):
    sio = StringIO(content)
    sio.filename = name
    sio.content_type = 'text/html'
    return sio
Example #26
0
def parse_attachment(message_part, attachments=None):
    """ Extract the attachment and metadata about it from the message.

    Returns the content, content type, size, and create/modification/read dates
    for the attachment.
    """
    params = message_part.get_params(None, 'Content-Disposition')
    if params:
        # If a 'part' has a Content-Disposition, we assume it is an attachment
        try:
            params = dict(params)
            print('\tContent-Disposition (for following email)', params)
            if 'attachment' in params:
                # Not sure what's going on here
                # Why get payload with decode, then try again and reparse?
                # See details at
                # http://docs.python.org/2/library/email.message.html#email.message.Message.get_payload
                file_data = message_part.get_payload(decode=True)
                if not file_data:
                    payload = message_part.get_payload()
                    if isinstance(payload, list):
                        for msgobj in payload:
                            # TODO not sure this actually does anything
                            parse2(msgobj, attachments)
                        return None
                    print(message_part.get_payload(), file=sys.stderr)
                    print(message_part.get_content_charset(), file=sys.stderr)

                attachment = StringIO(file_data)
                attachment.content_type = message_part.get_content_type()
                attachment.size = params.get('size', len(file_data))
                attachment.create_date = params.get('create-date')
                attachment.mod_date = params.get('modification-date')
                attachment.read_date = params.get('read-date')
                # TODO convert dates to datetime

                filename = message_part.get_filename(None)
                if filename:
                    # Filenames may be encoded with =?encoding?...
                    # If so, convert to unicode
                    name, encoding = email.header.decode_header(filename)[0]
                    if encoding:
                        print(
                            '\t{filename} encoded with {encoding}, converting to unicode'
                            .format(filename=filename, encoding=encoding))
                        filename = name.decode(encoding)
                else:  # filename not in Content-Disposition
                    print(
                        """Warning, no filename found in: [{%s}%s] Content-Disposition: %s or Content-Type"""
                        %
                        (sharedVariablesAcrossModules.sourceFileUUID,
                         sharedVariablesAcrossModules.sourceFilePath, params),
                        file=sys.stderr)
                    filename = six.text_type(uuid.uuid4())
                    print("Attempting extraction with random filename: %s" %
                          (filename),
                          file=sys.stderr)
                # Remove newlines from filename because that breaks everything
                filename = filename.replace("\r", "").replace("\n", "")

                attachment.name = filename
                return attachment

        except Exception as inst:
            print(type(inst), file=sys.stderr)
            print(inst.args, file=sys.stderr)
            print("Error parsing: file: {%s}%s" %
                  (sharedVariablesAcrossModules.sourceFileUUID,
                   sharedVariablesAcrossModules.sourceFilePath),
                  file=sys.stderr)
            print("Error parsing: Content-Disposition: ",
                  params,
                  file=sys.stderr)
            print(file=sys.stderr)
            sharedVariablesAcrossModules.errorCounter += 1
    return None
def parse_attachment(message_part, attachments=None):
    content_disposition = message_part.get("Content-Disposition", None)
    if content_disposition:
        try:
            try:
                content_disposition = tweakContentDisposition(content_disposition)
                dispositions = content_disposition.strip().split(";", 1)
            except Exception as inst:
                print type(inst)
                print inst.args
                print >>sys.stderr, "Error parsing file: {%s}%s" % (sharedVariablesAcrossModules.sourceFileUUID, sharedVariablesAcrossModules.sourceFilePath)
                print >>sys.stderr, "Error parsing the content_disposition:", content_disposition
                if "attachment" in content_disposition.lower() and "filename" in content_disposition.lower():  
                    try:
                        filename = uuid.uuid4().__str__()
                        print >>sys.stderr, "Attempting extraction with random filename: %s" % (filename)
                        print >>sys.stderr
                        content_disposition = "attachment; filename=%s;" % (filename)
                        dispositions = content_disposition.strip().split(";")
                    except Exception as inst:
                        print >>sys.stderr, type(inst)
                        print >>sys.stderr, inst.args
                        print >>sys.stderr, "Failed"
                        print >>sys.stderr
                        return None
                else:
                    print >>sys.stderr
                    return None
            if content_disposition and dispositions[0].lower() == "attachment":
                file_data = message_part.get_payload(decode=True)
                if not file_data:
                    payload = message_part.get_payload()
                    if isinstance(payload, list):
                        for msgobj in payload:
                            parse2(msgobj, attachments)
                        return None
                    print >>sys.stderr, message_part.get_payload()
                    print >>sys.stderr, message_part.get_content_charset()

                attachment = StringIO(file_data)
                attachment.content_type = message_part.get_content_type()
                attachment.size = len(file_data)
                attachment.create_date = None
                attachment.mod_date = None
                attachment.read_date = None
                attachment.name = ""
                
                for param in dispositions[1:]:
                    name,value = param.split("=", 1)
                    name = name.lower().strip()
                    
                    if name == "filename":
                        attachment.name = urllib2.unquote(value.strip()).strip('"')
                    if name == "filename*":
                        attachment.name = urllib2.unquote(value.strip())
                        try:
                            enc, name = attachment.name.split("''", 1)
                            attachment.name = name.decode(enc)
                        except Exception as inst:
                            print >>sys.stderr, type(inst)
                            print >>sys.stderr, inst.args
                            pass
                    elif name == "create-date":
                        attachment.create_date = value  #TODO: datetime
                    elif name == "modification-date":
                        attachment.mod_date = value #TODO: datetime
                    elif name == "read-date":
                        attachment.read_date = value #TODO: datetime
                
                if not attachment.name: 
                    print >>sys.stderr, """Warning, no filename found in: [{%s}%s]%s""" % (sharedVariablesAcrossModules.sourceFileUUID, sharedVariablesAcrossModules.sourceFilePath, content_disposition)
                    filename = uuid.uuid4().__str__()
                    print >>sys.stderr, "Attempting extraction with random filename: %s" % (filename)
                    print >>sys.stderr

                return attachment
                            
        except Exception as inst:
            print >>sys.stderr, type(inst)
            print >>sys.stderr, inst.args
            print >>sys.stderr, "Error parsing file: {%s}%s" % (sharedVariablesAcrossModules.sourceFileUUID, sharedVariablesAcrossModules.sourceFilePath)
            print >>sys.stderr, "Error parsing:", dispositions
            print >>sys.stderr
            sharedVariablesAcrossModules.errorCounter += 1
    return None
Example #28
0
 def test_mimetype(self):
   content = StringIO(u'test')
   content.content_type = 'text/plain'
   b = Blob(content)
   assert 'mimetype' in b.meta
   assert b.meta['mimetype'] == u'text/plain'