init
commit
7ea95c85d0
|
|
@ -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 """<form method="post" enctype="multipart/form-data">
|
||||||
|
<input type="file" name="file">
|
||||||
|
<input type="text" name="name">
|
||||||
|
<input type="submit">
|
||||||
|
</form>"""
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/<path>')
|
||||||
|
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()
|
||||||
Loading…
Reference in New Issue