Example #1
0
def parse_attachment(message_part):
    '''Parse an email message part into an attachment object.'''
    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 = message_part.get_filename()
            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
                    attachment.ctime = parse_time(value)
                elif name == "modification-date":
                    attachment.mod_date = value
                    attachment.mtime = parse_time(value)
                elif name == "read-date":
                    attachment.read_date = value
                    attachment.atime = parse_time(value)
            return attachment
    return None
 def get_file(self):
     self._update_object()
     response = requests.get(self._list_object.SITE + self.location)
     response = requests.get(response.content.decode())
     buff = StringIO()
     buff.content_type = response.headers['content-type']
     buff.name = self.filename
     buff.write(response.content.decode())
     buff.seek(0)
     #buff.close()
     return buff
Example #3
0
def parse_attachment(message_part):
    """Function to parse attachment from MIME message part.

        Args:
            message_part (obj): part in the MIME message object tree.

        Returns:
            obj of either StringIO (for strings) else BytesIO. If no
            attachments were present, 'None' is returned.
    """
    content_disposition = message_part.get("Content-Disposition", None)
    content_id = message_part.get("Content-ID", None)
    if content_disposition:
        dispositions = content_disposition.strip().split(";")
        if (content_disposition and (dispositions[0].lower() == "attachment"
                                     or dispositions[0].lower() == "inline")):
            file_data = message_part.get_payload(decode=True)
            if isinstance(file_data, str):
                attachment = StringIO(file_data)
            else:
                attachment = BytesIO(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
            attachment.disposition = dispositions[0]
            attachment.id = content_id

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

                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 #4
0
def parse_attachment(message_part):
    content_disposition = message_part.get("Content-Disposition", None)
    content_id = message_part.get("Content-ID", None)
    if content_disposition:
        dispositions = content_disposition.strip().split(";")
        if (content_disposition and (dispositions[0].lower() == "attachment"
                                     or dispositions[0].lower() == "inline")):
            file_data = message_part.get_payload(decode=True)
            if isinstance(file_data, str):
                attachment = StringIO(file_data)
            else:
                attachment = BytesIO(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
            attachment.disposition = dispositions[0]
            attachment.id = content_id

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

                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
 def file_from_dict(self, educt):
     file_ = StringIO(json.dumps(educt).decode())
     file_.filename = 'customstyles.json'
     file_.content_type = 'application/json'
     return file_
Example #6
0
import json
import time
from io import StringIO
from django.test import TestCase
from django.urls import reverse


FAKE_FILE = StringIO("""\
Hello World!
Проверка загрузки файла.
0123456789
#()+-.,;
""")
FAKE_FILE.name = 'hello_world.txt'
FAKE_FILE.content_type = 'text/plain; charset=utf-8'
FAKE_FILE_RESULT = {
    'file_name': FAKE_FILE.name,
    'parsed_percentage': 100,
    'digits': 10,
    'characters': 5+5+8+8+5,
    'whitespaces': 7,
    'punctuation': 2+8,
    'done': True,
}


class ViewTests(TestCase):

    def test_index(self):
        time.sleep(1)
        response = self.client.get(reverse('index'))
Example #7
0
def test_mimetype(app: Flask, db: SQLAlchemy) -> None:
    content = StringIO("test")
    content.content_type = "text/plain"
    blob = Blob(content)
    assert "mimetype" in blob.meta
    assert blob.meta["mimetype"] == "text/plain"
Example #8
0
def test_mimetype(app, db):
    content = StringIO("test")
    content.content_type = "text/plain"
    blob = Blob(content)
    assert "mimetype" in blob.meta
    assert blob.meta["mimetype"] == "text/plain"
 def file_from_dict(self, educt):
     file_ = StringIO(json.dumps(educt).decode())
     file_.filename = 'customstyles.json'
     file_.content_type = 'application/json'
     return file_