Beispiel #1
0
def paste():
    """Paste and redirect."""
    text = request.form['text']

    # TODO: make this better
    assert 0 <= len(text) <= ONE_MB, len(text)

    with UploadedFile.from_text(text) as uf:
        get_backend().store_object(uf)

    lang = request.form['language']
    if lang != 'rendered-markdown':
        with HtmlToStore.from_html(
                render_template(
                    'paste.html',
                    text=text,
                    highlighter=get_highlighter(text, lang),
                    raw_url=app.config['FILE_URL'].format(name=uf.name),
                )) as paste_obj:
            get_backend().store_html(paste_obj)
    else:
        with HtmlToStore.from_html(
                render_template(
                    'markdown.html',
                    text=text,
                    raw_url=app.config['FILE_URL'].format(name=uf.name),
                )) as paste_obj:
            get_backend().store_html(paste_obj)

    url = app.config['HTML_URL'].format(name=paste_obj.name)
    return redirect(url)
Beispiel #2
0
 def view_paste():
     return render_template(
         'paste.html',
         text=(TESTING_DIR / 'files' / 'code.py').open().read(),
         highlighter=get_highlighter('', 'python'),
         edit_url='#edit',
         raw_url='#raw',
     )
Beispiel #3
0
 def view_ansi_color():
     text = (TESTING_DIR / 'files' / 'ansi-color').open().read()
     return render_template(
         'paste.html',
         text=text,
         highlighter=get_highlighter(text, None),
         edit_url='#edit',
         raw_url='#raw',
     )
Beispiel #4
0
 def view_paste():
     return render_template(
         'paste.html',
         text=(TESTING_DIR / 'files' / 'code.py').open().read(),
         highlighter=get_highlighter('', 'python', None),
         edit_url='#edit',
         raw_url='#raw',
         styles=STYLES_BY_CATEGORY,
     )
Beispiel #5
0
 def view_diff():
     text = (TESTING_DIR / 'files' / 'python.diff').open().read()
     return render_template(
         'paste.html',
         text=text,
         highlighter=get_highlighter(text, None),
         edit_url='#edit',
         raw_url='#raw',
     )
Beispiel #6
0
def upload():
    """Process an upload and return JSON status."""
    uploaded_files = []
    for f in request.files.getlist('file'):
        try:
            with UploadedFile.from_http_file(f) as uf:
                get_backend().store_object(uf)

                # If it looks like text, make a pastebin as well.
                pb = None
                if uf.num_bytes < ONE_MB and not uf.probably_binary:
                    # We can't know for sure it's utf8, so this might fail.
                    # If so, we just won't make a pastebin for this file.
                    try:
                        text = uf.full_content.decode('utf8')
                    except UnicodeDecodeError:
                        pass
                    else:
                        with HtmlToStore.from_html(
                                render_template(
                                    'paste.html',
                                    text=text,
                                    highlighter=get_highlighter(text, None),
                                    raw_url=app.config['FILE_URL'].format(
                                        name=uf.name),
                                )) as pb:
                            get_backend().store_html(pb)

            uploaded_files.append((uf, pb))
        except FileTooLargeError:
            return jsonify({
                'success':
                False,
                'error':
                '{} exceeded the maximum file size limit of {}'.format(
                    f.filename,
                    human_size(app.config['MAX_UPLOAD_SIZE']),
                ),
            })

    with HtmlToStore.from_html(
            render_template(
                'details.html',
                uploads=uploaded_files,
            )) as details_obj:
        get_backend().store_html(details_obj)

    url = app.config['HTML_URL'].format(name=details_obj.name)

    if 'json' in request.args:
        return jsonify({
            'success': True,
            'redirect': url,
        })
    else:
        return redirect(url)
Beispiel #7
0
 def view_ansi_color():
     text = (TESTING_DIR / 'files' / 'ansi-color').open().read()
     return render_template(
         'paste.html',
         text=text,
         highlighter=get_highlighter(text, None, None),
         edit_url='#edit',
         raw_url='#raw',
         styles=STYLES_BY_CATEGORY,
     )
Beispiel #8
0
def paste():
    """Paste and redirect."""
    text = request.form['text']

    with contextlib.ExitStack() as ctx:
        objects = []

        # Browsers always send \r\n for the pasted text, which leads to bad
        # newlines when curling the raw text (#28).
        try:
            uf = ctx.enter_context(
                UploadedFile.from_text(text.replace('\r\n', '\n')))
        except FileTooLargeError as ex:
            num_bytes, = ex.args
            return 'Exceeded the max upload size of {} (tried to paste {})'.format(
                human_size(app.config['MAX_UPLOAD_SIZE']),
                human_size(num_bytes),
            ), 413
        objects.append(uf)

        lang = request.form['language']
        if lang != 'rendered-markdown':
            paste_obj = ctx.enter_context(
                HtmlToStore.from_html(
                    render_template(
                        'paste.html',
                        text=text,
                        highlighter=get_highlighter(text, lang),
                        raw_url=app.config['FILE_URL'].format(name=uf.name),
                        styles=STYLES_BY_CATEGORY,
                    )))
            objects.append(paste_obj)
        else:
            paste_obj = ctx.enter_context(
                HtmlToStore.from_html(
                    render_template(
                        'markdown.html',
                        text=text,
                        raw_url=app.config['FILE_URL'].format(name=uf.name),
                    )))
            objects.append(paste_obj)

        upload_objects(objects)

    return redirect(paste_obj.url)
Beispiel #9
0
def paste():
    """Paste and redirect."""
    text = request.form['text']

    # TODO: make this better
    assert 0 <= len(text) <= ONE_MB, len(text)

    with contextlib.ExitStack() as ctx:
        objects = []

        # Browsers always send \r\n for the pasted text, which leads to bad
        # newlines when curling the raw text (#28).
        uf = ctx.enter_context(
            UploadedFile.from_text(text.replace('\r\n', '\n')))
        objects.append(uf)

        lang = request.form['language']
        if lang != 'rendered-markdown':
            paste_obj = ctx.enter_context(
                HtmlToStore.from_html(
                    render_template(
                        'paste.html',
                        text=text,
                        highlighter=get_highlighter(text, lang),
                        raw_url=app.config['FILE_URL'].format(name=uf.name),
                    )))
            objects.append(paste_obj)
        else:
            paste_obj = ctx.enter_context(
                HtmlToStore.from_html(
                    render_template(
                        'markdown.html',
                        text=text,
                        raw_url=app.config['FILE_URL'].format(name=uf.name),
                    )))
            objects.append(paste_obj)

        upload_objects(objects)

    return redirect(paste_obj.url)
Beispiel #10
0
def test_get_highlighter_pygments(text, language, filename, expected):
    h = get_highlighter(text, language, filename)
    assert isinstance(h, PygmentsHighlighter)
    assert type(h.lexer) is type(expected)
Beispiel #11
0
def upload():
    """Process an upload and return JSON status."""
    uploaded_files = []

    with contextlib.ExitStack() as ctx:
        objects = []

        for f in request.files.getlist('file'):
            try:
                uf = ctx.enter_context(UploadedFile.from_http_file(f))
                objects.append(uf)

                # If it looks like text, make a pastebin as well.
                pb = None
                if uf.num_bytes < ONE_MB and not uf.probably_binary:
                    # We can't know for sure it's utf8, so this might fail.
                    # If so, we just won't make a pastebin for this file.
                    try:
                        text = uf.full_content.decode('utf8')
                    except UnicodeDecodeError:
                        pass
                    else:
                        pb = ctx.enter_context(HtmlToStore.from_html(render_template(
                            'paste.html',
                            text=text,
                            highlighter=get_highlighter(text, None),
                            raw_url=app.config['FILE_URL'].format(name=uf.name),
                            styles=STYLES_BY_CATEGORY,
                        )))
                        objects.append(pb)

                uploaded_files.append((uf, pb))
            except FileTooLargeError as ex:
                num_bytes, = ex.args
                return jsonify({
                    'success': False,
                    'error': '{} ({}) exceeded the maximum file size limit of {}'.format(
                        f.filename,
                        human_size(num_bytes),
                        human_size(app.config['MAX_UPLOAD_SIZE']),
                    ),
                }), 413

        details_obj = ctx.enter_context(
            HtmlToStore.from_html(render_template(
                'details.html',
                uploads=uploaded_files,
            )),
        )
        objects.append(details_obj)

        upload_objects(objects)

    if 'json' in request.args:
        return jsonify({
            'success': True,
            'redirect': details_obj.url,
            'uploaded_files': {
                uf.human_name: {
                    'raw': uf.url,
                    'paste': pb.url if pb is not None else None,
                }
                for uf, pb in uploaded_files
            },
        })
    else:
        return redirect(details_obj.url)
Beispiel #12
0
def paste():
    """Paste and redirect."""
    text = request.form['text']
    # Browsers always send \r\n for the pasted text, which leads to bad
    # newlines when curling the raw text (#28).
    transformed_text = text.replace('\r\n', '\n')

    with contextlib.ExitStack() as ctx:
        objects = []

        # Raw text object
        try:
            uf = ctx.enter_context(UploadedFile.from_text(transformed_text))
        except FileTooLargeError as ex:
            num_bytes, = ex.args
            return 'Exceeded the max upload size of {} (tried to paste {})'.format(
                human_size(app.config['MAX_UPLOAD_SIZE']),
                human_size(num_bytes),
            ), 413
        objects.append(uf)

        # HTML view (Markdown or paste)
        lang = request.form['language']
        if lang != 'rendered-markdown':
            highlighter = get_highlighter(text, lang)
            lang_title = highlighter.name
            paste_obj = ctx.enter_context(HtmlToStore.from_html(render_template(
                'paste.html',
                text=text,
                highlighter=highlighter,
                raw_url=app.config['FILE_URL'].format(name=uf.name),
                styles=STYLES_BY_CATEGORY,
            )))
            objects.append(paste_obj)
        else:
            lang_title = 'Rendered Markdown'
            paste_obj = ctx.enter_context(HtmlToStore.from_html(render_template(
                'markdown.html',
                text=text,
                raw_url=app.config['FILE_URL'].format(name=uf.name),
            )))
            objects.append(paste_obj)

        # Metadata JSON object
        metadata = {
            'server_version': version,
            'uploaded_files': {
                'html': paste_obj.url,
                'raw': uf.url,
            },
            'timestamp': time.time(),
            'upload_type': 'paste',
            'paste_details': {
                'language': {
                    'title': lang_title,
                },
                'num_lines': len(transformed_text.splitlines()),
                'raw_text': transformed_text,
            },
        }
        metadata_obj = ctx.enter_context(UploadedFile.from_text(
            json.dumps(metadata, indent=4, sort_keys=True),
            human_name='metadata.json',
        ))
        objects.append(metadata_obj)

        upload_objects(objects, metadata_url=metadata_obj.url)

    if 'json' in request.args:
        return jsonify({
            'success': True,
            'redirect': paste_obj.url,
            'uploaded_files': {
                'paste': {
                    'raw': uf.url,
                    'paste': paste_obj.url,
                    'metadata': metadata_obj.url,
                },
            },
        })
    else:
        return redirect(paste_obj.url)
Beispiel #13
0
def test_get_highlighter_diff(text, language, expected):
    h = get_highlighter(text, language)
    assert isinstance(h, DiffHighlighter)
    assert type(h.lexer) is type(expected)
Beispiel #14
0
def test_get_highlighter_pygments(text, language, expected):
    h = get_highlighter(text, language)
    assert isinstance(h, PygmentsHighlighter)
    assert type(h.lexer) is type(expected)
Beispiel #15
0
def test_get_highlighter_diff(text, language, expected):
    h = get_highlighter(text, language, None)
    assert isinstance(h, DiffHighlighter)
    assert type(h.lexer) is type(expected)
Beispiel #16
0
def paste():
    """Paste and redirect."""
    text = request.form['text']
    # Browsers always send \r\n for the pasted text, which leads to bad
    # newlines when curling the raw text (#28).
    transformed_text = text.replace('\r\n', '\n')

    with contextlib.ExitStack() as ctx:
        objects = []

        # Raw text object
        try:
            uf = ctx.enter_context(UploadedFile.from_text(transformed_text))
        except FileTooLargeError as ex:
            num_bytes, = ex.args
            return 'Exceeded the max upload size of {} (tried to paste {})'.format(
                human_size(app.config['MAX_UPLOAD_SIZE']),
                human_size(num_bytes),
            ), 413
        objects.append(uf)

        # HTML view (Markdown or paste)
        lang = request.form['language']
        if lang != 'rendered-markdown':
            highlighter = get_highlighter(text, lang, None)
            lang_title = highlighter.name
            paste_obj = ctx.enter_context(
                HtmlToStore.from_html(
                    render_template(
                        'paste.html',
                        text=text,
                        highlighter=highlighter,
                        raw_url=app.config['FILE_URL'].format(name=uf.name),
                        styles=STYLES_BY_CATEGORY,
                    )))
            objects.append(paste_obj)
        else:
            lang_title = 'Rendered Markdown'
            paste_obj = ctx.enter_context(
                HtmlToStore.from_html(
                    render_template(
                        'markdown.html',
                        text=text,
                        raw_url=app.config['FILE_URL'].format(name=uf.name),
                    )))
            objects.append(paste_obj)

        # Metadata JSON object
        metadata = {
            'server_version': version,
            'uploaded_files': {
                'html': paste_obj.url,
                'raw': uf.url,
            },
            'timestamp': time.time(),
            'upload_type': 'paste',
            'paste_details': {
                'language': {
                    'title': lang_title,
                },
                'num_lines': len(transformed_text.splitlines()),
                'raw_text': transformed_text,
            },
        }
        metadata_obj = ctx.enter_context(
            UploadedFile.from_text(
                json.dumps(metadata, indent=4, sort_keys=True),
                human_name='metadata.json',
            ))
        objects.append(metadata_obj)

        upload_objects(objects, metadata_url=metadata_obj.url)

    if 'json' in request.args:
        return jsonify({
            'success': True,
            'redirect': paste_obj.url,
            'uploaded_files': {
                'paste': {
                    'raw': uf.url,
                    'paste': paste_obj.url,
                    'metadata': metadata_obj.url,
                },
            },
        })
    else:
        return redirect(paste_obj.url)
Beispiel #17
0
def upload():
    """Process an upload and return JSON status."""
    uploaded_files = []

    with contextlib.ExitStack() as ctx:
        objects = []

        for f in request.files.getlist('file'):
            try:
                uf = ctx.enter_context(UploadedFile.from_http_file(f))
                objects.append(uf)

                # If it looks like text, make a pastebin as well.
                pb = None
                if uf.num_bytes < ONE_MB and not uf.probably_binary:
                    # We can't know for sure it's utf8, so this might fail.
                    # If so, we just won't make a pastebin for this file.
                    try:
                        text = uf.full_content.decode('utf8')
                    except UnicodeDecodeError:
                        pass
                    else:
                        pb = ctx.enter_context(
                            HtmlToStore.from_html(
                                render_template(
                                    'paste.html',
                                    text=text,
                                    highlighter=get_highlighter(
                                        text, None, uf.human_name),
                                    raw_url=app.config['FILE_URL'].format(
                                        name=uf.name),
                                    styles=STYLES_BY_CATEGORY,
                                )))
                        objects.append(pb)

                uploaded_files.append((uf, pb))
            except FileTooLargeError as ex:
                num_bytes, = ex.args
                return jsonify({
                    'success':
                    False,
                    'error':
                    '{} ({}) exceeded the maximum file size limit of {}'.
                    format(
                        f.filename,
                        human_size(num_bytes),
                        human_size(app.config['MAX_UPLOAD_SIZE']),
                    ),
                }), 413
            except ExtensionForbiddenError as ex:
                extension, = ex.args
                return jsonify({
                    'success':
                    False,
                    'error':
                    'Sorry, files with the extension ".{}" are not allowed.'.
                    format(extension),
                }), 403

        details_obj = ctx.enter_context(
            HtmlToStore.from_html(
                render_template(
                    'details.html',
                    uploads=uploaded_files,
                )), )
        objects.append(details_obj)

        upload_objects(objects)

    if 'json' in request.args:
        return jsonify({
            'success': True,
            'redirect': details_obj.url,
            'uploaded_files': {
                uf.human_name: {
                    'raw': uf.url,
                    'paste': pb.url if pb is not None else None,
                }
                for uf, pb in uploaded_files
            },
        })
    else:
        return redirect(details_obj.url)