Example #1
0
def file_upload_api():
    all_files = []
    for name, uploaded in request.files.iteritems():
        g.log.info("Processing upload '{0}'".format(name))
        f = Block()
        f.namespace = g.namespace
        f.content_type = uploaded.content_type
        f.filename = uploaded.filename
        f.data = uploaded.read()
        all_files.append(f)

    g.db_session.add_all(all_files)
    g.db_session.commit()  # to generate public_ids
    return g.encoder.jsonify(all_files)
Example #2
0
 def _save_attachment(self, data, content_disposition, content_type,
                      filename, content_id, namespace_id, mid):
     from inbox.models import Part, Block
     block = Block()
     block.namespace_id = namespace_id
     block.filename = _trim_filename(filename, namespace_id=namespace_id)
     block.content_type = content_type
     part = Part(block=block, message=self)
     if content_id:
         content_id = content_id[:255]
     part.content_id = content_id
     part.content_disposition = content_disposition
     data = data or ''
     if isinstance(data, unicode):
         data = data.encode('utf-8', 'strict')
     block.data = data
Example #3
0
def test_namespace_limiting(db, test_client):
    dt = datetime.datetime.utcnow()
    subject = dt.isoformat()
    db.session.add(Namespace())
    db.session.commit()
    namespaces = db.session.query(Namespace).all()
    assert len(namespaces) > 1
    for ns in namespaces:
        thread = Thread(namespace=ns, subjectdate=dt, recentdate=dt,
                        subject=subject)
        add_fake_message(db.session, ns.id, thread, received_date=dt,
                         subject=subject)
        db.session.add(Block(namespace=ns, filename=subject))
    db.session.commit()

    for ns in namespaces:
        r = json.loads(test_client.get('/n/{}/threads?subject={}'.
                                       format(ns.public_id, subject)).data)
        assert len(r) == 1

        r = json.loads(test_client.get('/n/{}/messages?subject={}'.
                                       format(ns.public_id, subject)).data)
        assert len(r) == 1

        r = json.loads(test_client.get('/n/{}/files?filename={}'.
                                       format(ns.public_id, subject)).data)
        assert len(r) == 1
Example #4
0
def test_namespace_limiting(db, api_client, default_namespaces):
    dt = datetime.datetime.utcnow()
    subject = dt.isoformat()
    namespaces = db.session.query(Namespace).all()
    assert len(namespaces) > 1
    for ns in namespaces:
        thread = Thread(namespace=ns,
                        subjectdate=dt,
                        recentdate=dt,
                        subject=subject)
        add_fake_message(db.session,
                         ns.id,
                         thread,
                         received_date=dt,
                         subject=subject)
        db.session.add(Block(namespace=ns, filename=subject))
    db.session.commit()

    for ns in namespaces:
        r = api_client.get_data('/threads?subject={}'.format(subject))
        assert len(r) == 1

        r = api_client.get_data('/messages?subject={}'.format(subject))
        assert len(r) == 1

        r = api_client.get_data('/files?filename={}'.format(subject))
        assert len(r) == 1
Example #5
0
    def _save_attachment(
        self,
        data,
        content_disposition,
        content_type,
        filename,
        content_id,
        namespace_id,
        mid,
    ):
        from inbox.models import Block, Part

        block = Block()
        block.namespace_id = namespace_id
        block.filename = _trim_filename(filename, namespace_id=namespace_id)
        block.content_type = content_type
        part = Part(block=block, message=self)
        if content_id:
            content_id = content_id[:255]
        part.content_id = content_id
        part.content_disposition = content_disposition
        data = data or ""
        if isinstance(data, unicode):
            data = data.encode("utf-8", "strict")
        block.data = data
Example #6
0
def attachments(db):
    from inbox.models import Block

    test_data = [
        ("muir.jpg", "image/jpeg"),
        ("LetMeSendYouEmail.wav", "audio/vnd.wave"),
        ("first-attachment.jpg", "image/jpeg"),
    ]

    new_attachments = []
    for filename, content_type in test_data:

        test_attachment_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "data", filename)

        with open(test_attachment_path, "r") as f:
            b = Block(namespace_id=NAMESPACE_ID, filename=filename, data=f.read())
            # for now because of lousy _content_type enum
            b.content_type = content_type
            db.session.add(b)
            db.session.commit()
            new_attachments.append(b.public_id)

    return new_attachments
Example #7
0
def attachments(db):
    from inbox.models import Block
    test_data = [('muir.jpg', 'image/jpeg'),
                 ('LetMeSendYouEmail.wav', 'audio/vnd.wave'),
                 ('first-attachment.jpg', 'image/jpeg')]

    new_attachments = []
    for filename, content_type in test_data:

        test_attachment_path = os.path.join(
            os.path.dirname(os.path.abspath(__file__)), '..', 'data', filename)

        with open(test_attachment_path, 'r') as f:
            b = Block(namespace_id=NAMESPACE_ID,
                      filename=filename,
                      data=f.read())
            # for now because of lousy _content_type enum
            b.content_type = content_type
            db.session.add(b)
            db.session.commit()
            new_attachments.append(b.public_id)

    return new_attachments
Example #8
0
def fake_attachment(db, default_account, message):
    block = Block()
    namespace_id = default_account.namespace.id
    block.namespace_id = namespace_id
    block.filename = 'zambla.txt'
    block.content_type = 'text/plain'
    block.size = 32
    # This is sha256 of an attachment in our test email.
    block.data_sha256 = '27dc8e801f962098fd4a741ccbd6ca24d42805df0b8035cfb881ad6e5a1bf4b2'
    p = Part(block=block, message=message)
    db.session.add(p)
    db.session.commit()
    return p
Example #9
0
def fake_attachment(db, default_account, message):
    block = Block()
    namespace_id = default_account.namespace.id
    block.namespace_id = namespace_id
    block.filename = 'zambla.txt'
    block.content_type = 'text/plain'
    block.size = 32
    # This is sha256 of an attachment in our test email.
    block.data_sha256 = '27dc8e801f962098fd4a741ccbd6ca24d42805df0b8035cfb881ad6e5a1bf4b2'
    p = Part(block=block, message=message)
    db.session.add(p)
    db.session.commit()
    return p
Example #10
0
def file_upload_api():
    all_files = []
    for name, uploaded in request.files.iteritems():
        g.log.info("Processing upload '{0}'".format(name))
        f = Block()
        f.namespace = g.namespace
        f.content_type = uploaded.content_type
        f.filename = uploaded.filename
        f.data = uploaded.read()
        all_files.append(f)

    g.db_session.add_all(all_files)
    g.db_session.commit()  # to generate public_ids

    return g.encoder.jsonify(all_files)
Example #11
0
 def _save_attachment(self, mimepart, content_disposition, content_type,
                      filename, content_id, namespace_id, mid):
     from inbox.models import Part, Block
     block = Block()
     block.namespace_id = namespace_id
     block.filename = _trim_filename(filename, mid=mid)
     block.content_type = content_type
     part = Part(block=block, message=self)
     if content_id:
         content_id = content_id[:255]
     part.content_id = content_id
     part.content_disposition = content_disposition
     data = mimepart.body or ''
     if isinstance(data, unicode):
         data = data.encode('utf-8', 'strict')
     block.data = data