Skip to content
Snippets Groups Projects
routes.py 2.97 KiB
Newer Older
Lukas Böhm's avatar
Lukas Böhm committed
from urllib.parse import urljoin
from flask import render_template, redirect, url_for, current_app, abort, request, flash
Lukas Böhm's avatar
Lukas Böhm committed
from werkzeug.exceptions import HTTPException
Lukas Böhm's avatar
Lukas Böhm committed
import requests as api
from chiefsend import app
from util import AuthForm, get_password, set_password
Lukas Böhm's avatar
Lukas Böhm committed


@app.route('/privacy')
def privacy():
    return render_template('Privacy.html')


@app.route('/about')
def about():
    return render_template('About.html')


@app.route('/')
def index():
Lukas Böhm's avatar
Lukas Böhm committed
    return render_template('Landing.html')
Lukas Böhm's avatar
Lukas Böhm committed


@app.errorhandler(HTTPException)
def http_error(e):
    return render_template('Error.html', e=e)


@app.route('/public')
def public():
    res = api.get(urljoin(current_app.config['API_URL'], '/shares'))
    if res.status_code != 200:
        abort(res.status_code)
    else:
        shares = res.json()
        return render_template('Public.html', shares=shares)


@app.route('/d/<string:share_id>/')
Lukas Böhm's avatar
Lukas Böhm committed
def download(share_id):
    password = get_password(share_id)
    if password:
        res = api.get(urljoin(current_app.config['API_URL'], f'/share/{share_id}'), auth=(share_id, password))
    else:
        res = api.get(urljoin(current_app.config['API_URL'], f'/share/{share_id}'))

Lukas Böhm's avatar
Lukas Böhm committed
    if res.status_code == 401:
        return redirect(url_for('download_auth', share_id=share_id))
Lukas Böhm's avatar
Lukas Böhm committed
    elif res.status_code != 200:
        abort(res.status_code)
Lukas Böhm's avatar
Lukas Böhm committed
    else:
        share = res.json()
        print(share)
Lukas Böhm's avatar
Lukas Böhm committed
        return render_template('Download.html', up=share)


@app.route('/d/<string:share_id>/auth', methods=['GET', 'POST'])
def download_auth(share_id):
    form = AuthForm()
    if form.validate_on_submit():
        set_password(share_id, form.password.data)
        return redirect(url_for('download', share_id=share_id))
    else:
        for field, error in form.errors.items():
            from markupsafe import Markup
            flash(Markup(f'<b>{field}:</b> {error}'), category='danger')

    flash('Zugriff verweigert.', category='danger')
    return render_template('DownloadAuth.html', form=form)


Lukas Böhm's avatar
Lukas Böhm committed
@app.route('/d/<string:share_id>/f/<string:file_id>')
def media(share_id, file_id):
    password = get_password(share_id)
    if password:
        res = api.get(urljoin(current_app.config['API_URL'], f'/share/{share_id}/attachment/{file_id}'), auth=(share_id, password), stream=True)
    else:
        res = api.get(urljoin(current_app.config['API_URL'], f'/share/{share_id}/attachment/{file_id}'), stream=True)

    if res.status_code == 401:
        return redirect(url_for('download_auth', share_id=share_id))
    elif res.status_code != 200:
        abort(res.status_code)
    else:
        from flask import stream_with_context
        from flask import Response
        return Response(
            stream_with_context(res.raw),
            headers={
                'Content-Disposition': res.headers['Content-Disposition']
            },
            mimetype=res.headers['Content-Type']
        )


Lukas Böhm's avatar
Lukas Böhm committed
@app.route('/upload', methods=['GET', 'POST'])
def upload():
Lukas Böhm's avatar
Lukas Böhm committed
    return render_template('Upload.html')