from flask import send_file, redirect, url_for, abort
from werkzeug.utils import secure_filename
from app import app
from config import V5Config
import os

# These routes are used in development
# In production, those files should be served by the web server (nginx)

@app.route('/avatar/<filename>')
def avatar(filename):
    filename = secure_filename(filename)  # No h4ckers allowed
    filepath = os.path.join(V5Config.DATA_FOLDER, "avatars", filename)
    return send_file(filepath)

@app.route('/fichiers/<path>/<name>')
def attachment(path, name):
    file = os.path.join(V5Config.DATA_FOLDER, "attachments",
        secure_filename(path), secure_filename(name))
    if os.path.isfile(file):
        return send_file(file)
    else:
        abort(404)
