commit 7ea95c85d07d37cdcaebd15ac6399616fe296ab0 Author: clemens Date: Thu Nov 30 14:40:04 2017 +0100 init diff --git a/app.py b/app.py new file mode 100644 index 0000000..221e4fd --- /dev/null +++ b/app.py @@ -0,0 +1,53 @@ +import os +import random + +from flask import Flask, request, url_for, flash, redirect +from werkzeug.utils import secure_filename + +from util import allowed_file + +UPLOAD_FOLDER = 'uploads/' + +app = Flask(__name__) +app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +app.debug = True + + +@app.route('/', methods=['GET', 'POST']) +def upload_file(): + if request.method == 'POST': + if "file" not in request.files or "name" not in request.form: + flash("No file part") + return redirect(request.url) + file = request.files['file'] + if file.filename == "": + flash("No selected file") + return redirect(request.url) + if file and allowed_file(file.filename): + filename = secure_filename(file.filename) + path = os.path.join(app.config['UPLOAD_FOLDER'], request.form["name"]) + if not os.path.exists(path): + os.mkdir(path) + file.save(os.path.join(path, filename)) + return redirect(url_for('upload_file', filename=filename)) + + return """
+ + + +
""" + + +@app.route('/') +def get_image(path): + path = os.path.join(app.config['UPLOAD_FOLDER'], path) + entries = [] + if os.path.exists(path): + entries = os.listdir(path) + if not entries: + return 0 + return str(random.choice(entries)) + + +if __name__ == '__main__': + app.run() diff --git a/util.py b/util.py new file mode 100644 index 0000000..f9e6ec3 --- /dev/null +++ b/util.py @@ -0,0 +1,7 @@ +ALLOWED_EXTENSIONS = ['png', 'jpg', 'jpeg'] + + +def allowed_file(filename): + return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS + +