You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

1 week ago
import os
from flask import Flask, render_template, redirect, request
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileField
from wtforms import (
SubmitField,
)
# config me here
1 week ago
APP = Flask(__name__, static_folder="static")
APP.config["IMAGE_FOLDER"] = "static/images"
ALLOWED_FILES = ["jpg", "png", "gif", "webp"]
1 week ago
# don't config these lines
APP.config["SECRET_KEY"] = os.urandom(24)
class ImageUploadForm(FlaskForm):
"""Image upload form."""
image = FileField(
"image:",
validators=[FileAllowed(ALLOWED_FILES, f"Images only, please use any of the following file extensions: {(", ").join(ALLOWED_FILES)}")],
1 week ago
)
submit = SubmitField("Submit")
def saveimage(image):
"""Save the image to the folder"""
image.save(os.path.join(APP.config["IMAGE_FOLDER"], image.filename))
@APP.route("/", methods=["GET", "POST"])
def index():
"""Upload route, a page to upload an image"""
imageuploadform = ImageUploadForm()
if request.method == "POST":
if imageuploadform.validate_on_submit():
saveimage(imageuploadform.image.data)
return render_template("upload.html", imageuploadform=imageuploadform)
if __name__ == "__main__":
APP.debug = True
APP.run(port=5000)