Exemple #1
0
def get(sid=None, sha1=None, label=None, namespace=None, lexer=None, handler=None, formatter=None):
    cur, name, path = _get_paste(model.get_content, sid, sha1, label, namespace)

    paste = next(cur)
    if paste.get('sunset'):
        max_age = parse_sunset(**paste) - datetime.utcnow()
        request.max_age = max_age.seconds

    content = model._get(paste.get('content'))

    if paste.get('redirect'):
        content = content.decode('utf-8')
        return redirect(content, content)

    mimetype, _ = guess_type(name)
    if not mimetype:
        mimetype = paste.get('mimetype', 'text/plain')

    headers = paste.get('headers', {})

    if lexer is not None:
        return highlight(content, lexer, formatter)
    if handler is not None:
        return _handler.get(handler, content, mimetype, path=path)

    response = BaseResponse(content, mimetype=mimetype)
    response.headers.extend(headers)

    return response
Exemple #2
0
def get(sid=None, sha1=None, label=None, namespace=None, lexer=None, handler=None, formatter=None):
    cur, name, path = _get_paste(model.get_content, sid, sha1, label, namespace)

    if not cur or not cur.count():
        return StatusResponse("not found", 404)

    paste = next(cur)
    if paste.get('sunset'):
        request.max_age = parse_sunset(**paste) - datetime.utcnow()
        print('max_age', request.max_age)
        if request.max_age < timedelta():
            uuid = UUID(hex=paste['_id'])
            paste = invalidate(uuid=uuid)
            result = model.delete(uuid=uuid)
            return PasteResponse(paste, "expired", code=410)
        else:
            request.max_age = request.max_age.seconds

    content = model._get(paste.get('content'))

    if paste.get('redirect'):
        content = content.decode('utf-8')
        return redirect(content, content)

    mimetype, _ = guess_type(name)
    if not mimetype:
        mimetype = paste.get('mimetype', 'text/plain')

    if lexer != None:
        return highlight(content, lexer, formatter)
    if handler != None:
        return _handler.get(handler, content, mimetype, path=path)

    return BaseResponse(content, mimetype=mimetype)
Exemple #3
0
def get(sid=None, sha1=None, label=None, namespace=None, lexer=None, handler=None, formatter=None):
    cur, name, path = _get_paste(model.get_content, sid, sha1, label, namespace)

    if not cur or not cur.count():
        return StatusResponse("not found", 404)

    paste = next(cur)
    if paste.get('sunset'):
        request.max_age = parse_sunset(**paste) - datetime.utcnow()
        if request.max_age < timedelta():
            uuid = UUID(hex=paste['_id'])
            paste = invalidate(uuid=uuid)
            model.delete(uuid=uuid)
            return PasteResponse(paste, "expired", code=410)
        else:
            request.max_age = request.max_age.seconds

    content = model._get(paste.get('content'))

    if paste.get('redirect'):
        content = content.decode('utf-8')
        return redirect(content, content)

    mimetype, _ = guess_type(name)
    if not mimetype:
        mimetype = paste.get('mimetype', 'text/plain')

    if lexer is not None:
        return highlight(content, lexer, formatter)
    if handler is not None:
        return _handler.get(handler, content, mimetype, path=path)

    return BaseResponse(content, mimetype=mimetype)
Exemple #4
0
def get(sid=None, sha1=None, label=None, lexer=None, handler=None):
    cur = None
    if sid:
        sid, name, value = sid
        cur = model.get_content(**{
            '$or' : [
                {
                    'digest': {
                        '$regex': '{}$'.format(sid)
                    }
                },
                {
                    'label' : value
                }
            ],
            'private': {
                '$exists': False
            }
        })
    if sha1:
        digest, name = sha1[:2]
        cur = model.get_content(digest = digest).hint([('digest', 1)])
    if label:
        label, name = label
        cur = model.get_content(label = label).hint([('label', 1)])

    if not cur or not cur.count():
        return "Not found.\n", 404

    paste = cur.__next__()
    content = model._get(paste.get('content'))

    if paste.get('redirect'):
        content = content.decode('utf-8')
        return redirect(content, '{}\n'.format(content))

    mimetype, _ = guess_type(name)

    if lexer != None:
        return highlight(content, lexer)
    if handler != None:
        return _handler.get(handler, content, mimetype)
    if mimetype:
        return Response(content, mimetype=mimetype)

    return content
Exemple #5
0
def get(b66=None, sha1=None, lexer=None, handler=None):
    content = None
    if b66:
        id, name = b66
        content = model.get_content(id)
    if sha1:
        digest, name = sha1
        content = model.get_content_digest(digest)

    if not content:
        return "Not found.\n", 404

    mimetype, _ = guess_type(name)

    if lexer != None:
        return highlight(content, lexer)
    if handler != None:
        return _handler.get(handler, content)
    if mimetype:
        return Response(content, mimetype=mimetype)

    return content
Exemple #6
0
def get(sid=None, sha1=None, label=None, namespace=None, lexer=None, handler=None, formatter=None):
    cur, name, path = _get_paste(model.get_content, sid, sha1, label, namespace)

    if not cur or not cur.count():
        return StatusResponse("not found", 404)

    paste = next(cur)
    if paste.get('sunset'):
        request.max_age = (paste['date'] + timedelta(seconds=paste['sunset'])) - datetime.utcnow()
        if request.max_age < timedelta():
            request.max_age = 0
        else:
            request.max_age = request.max_age.seconds

        if paste['date'] + timedelta(seconds=paste['sunset']) < datetime.utcnow():
            max_age = datetime.utcnow() - (paste['date'] + timedelta(seconds=paste['sunset']))
            uuid = UUID(hex=paste['_id'])
            paste = invalidate(uuid=uuid)
            result = model.delete(uuid=uuid)
            if not result['n']:
                return StatusResponse("this should not happen", 500)
            return PasteResponse(paste, "expired")

    content = model._get(paste.get('content'))

    if paste.get('redirect'):
        content = content.decode('utf-8')
        return redirect(content, content)

    mimetype, _ = guess_type(name)
    if not mimetype:
        mimetype = paste.get('mimetype', 'text/plain')

    if lexer != None:
        return highlight(content, lexer, formatter)
    if handler != None:
        return _handler.get(handler, content, mimetype, path=path)

    return Response(content, mimetype=mimetype)
Exemple #7
0
def get(b66=None, sha1=None, lexer=None, handler=None):
    content = None
    if b66:
        id, name = b66
        content = model.get_content(id)
    if sha1:
        digest, name = sha1
        content = model.get_content_digest(digest)

    if not content:
        return "Not found.\n", 404

    mimetype, _ = guess_type(name)

    if lexer != None:
        return highlight(content, lexer)
    if handler != None:
        return _handler.get(handler, content)
    if mimetype:
        return Response(content, mimetype=mimetype)

    return content
Exemple #8
0
def get(sid=None, sha1=None, label=None, lexer=None, handler=None):
    cur = None
    if sid:
        sid, name, value = sid
        cur = model.get_content(**{
            '$or' : [
                {
                    'digest': {
                        '$regex': '{}$'.format(sid)
                    }
                },
                {
                    'label' : value
                }
            ],
            'private': {
                '$exists': False
            }
        })
    if sha1:
        digest, name = sha1[:2]
        cur = model.get_content(digest = digest).hint([('digest', 1)])
    if label:
        label, name = label
        cur = model.get_content(label = label).hint([('label', 1)])

    if not cur or not cur.count():
        return "Not found.\n", 404

    paste = cur.__next__()
    if paste.get('sunset'):
        request.max_age = (paste['date'] + timedelta(seconds=paste['sunset'])) - datetime.utcnow()
        if request.max_age < timedelta():
            request.max_age = 0
        else:
            request.max_age = request.max_age.seconds

        if paste['date'] + timedelta(seconds=paste['sunset']) < datetime.utcnow():
            max_age = datetime.utcnow() - (paste['date'] + timedelta(seconds=paste['sunset']))
            uuid = UUID(hex=paste['_id'])
            paste = invalidate(uuid)
            result = model.delete(uuid)
            if not result['n']:
                return "This should not happen.", 500
            return complex_response(paste, status="expired")

    content = model._get(paste.get('content'))

    if paste.get('redirect'):
        content = content.decode('utf-8')
        return redirect(content, '{}\n'.format(content))

    mimetype, _ = guess_type(name)
    if not mimetype:
        mimetype = paste.get('mimetype')

    if lexer != None:
        return highlight(content, lexer)
    if handler != None:
        return _handler.get(handler, content, mimetype)
    if mimetype:
        return Response(content, mimetype=mimetype)

    return content