diff --git a/runserver.py b/runserver.py index 92520a04ce75b9368d8cb0377161fbc3a50f061d..ad46b365fc5e140ca73b992af38268e5ac737655 100644 --- a/runserver.py +++ b/runserver.py @@ -2,8 +2,10 @@ from tedimg import app from flask import send_from_directory app.config.update( - STORAGE_PATH="./tedimg/static/images", - WEB_PATH="/static/images" + FULL_STORAGE="./tedimg/static/images", + THUMB_STORAGE="./tedimg/static/images/thumb", + FULL_WEB="static/images", + THUMB_WEB="static/images/thumb" ) app.run(debug=True) diff --git a/tedimg/images.py b/tedimg/images.py index efc7e64979ab830a061af3303a12b25f8d3cfa30..feef32c016753d9cd11fb58596ff89ed64e9c561 100644 --- a/tedimg/images.py +++ b/tedimg/images.py @@ -1,10 +1,38 @@ from tedimg import app +from PIL import Image -from os import path +import os +import binascii -def get_image(name): +def get_image(root, name): """ Try and get basic image attributes. """ - filename = path.basename(name) - return path.join(app.config["WEB_PATH"], filename) + filename = os.path.basename(name) + return (root + os.path.join(app.config["FULL_WEB"], filename), + root + os.path.join(app.config["THUMB_WEB"], filename)) + + +def image_from_file(file_storage): + """ Try and read the uploaded file. + """ + image = Image.open(file_storage) + return image + + +def image_from_url(url): + """ Try and download an image from the given url. + """ + + +def save_with_thumbnail(image, filename): + dest = "." + while os.path.exists(os.path.join(app.config["FULL_STORAGE"], dest)): + filename, ext = os.path.splitext(filename) + random = binascii.hexlify(os.urandom(3)).decode('utf8') + dest = "%s-%s%s" % (filename, random, ext) + image.save(os.path.join(app.config["FULL_STORAGE"], dest)) + thumb_size = app.config["THUMB_SIZE"] + image.thumbnail((thumb_size, thumb_size)) + image.save(os.path.join(app.config["THUMB_STORAGE"], dest)) + return dest diff --git a/tedimg/templates/error.html b/tedimg/templates/error.html new file mode 100644 index 0000000000000000000000000000000000000000..92dc8c95713cfe3df9520f21afcbef159d8525fd --- /dev/null +++ b/tedimg/templates/error.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} + +{% block content %} +
+
+

Upload failed!

+

{{ message }}

+

+
+
+{% endblock %} diff --git a/tedimg/templates/index.html b/tedimg/templates/index.html index 52e2a47423788d220ac9dacb1a79b4a7becbc42a..e61058ed6382edbc08e035091e096f2c1c7febb3 100644 --- a/tedimg/templates/index.html +++ b/tedimg/templates/index.html @@ -2,11 +2,11 @@ {% block banner_content %}

Upload your image!

-
+
File - +
@@ -15,7 +15,7 @@
public - +
{% endblock %} diff --git a/tedimg/templates/show.html b/tedimg/templates/show.html index e781801e72998993e2ad58e0d33713b3af31e449..0b99636756b130bb6fc1b635413459166a8f41d2 100644 --- a/tedimg/templates/show.html +++ b/tedimg/templates/show.html @@ -4,6 +4,10 @@

Upload successful!

+
+
{% endblock %} diff --git a/tedimg/views.py b/tedimg/views.py index 83d791a660724d2892dda359509e3f0d8ac18473..f69e2f25509daa3e0f5330ff4d65571218d32915 100644 --- a/tedimg/views.py +++ b/tedimg/views.py @@ -1,6 +1,7 @@ from tedimg import app, images import flask +import os @app.route('/') @@ -10,5 +11,32 @@ def index(): @app.route('/show/') def show(path): - image = images.get_image(path) - return flask.render_template("show.html", image=image) + root = flask.url_for("index", _external=True) + image, thumb = images.get_image(root, path) + return flask.render_template( + "show.html", + image=image, thumb=thumb, + thumb_size=app.config["THUMB_SIZE"] + ) + + +@app.route('/upload', methods=['POST']) +def upload(): + uploaded = flask.request.files['file'] + url = flask.request.form['url'] + # Get an image object from the uploaded image or URL + try: + if uploaded: + image = images.image_from_file(uploaded) + filename = os.path.basename(uploaded.filename) + elif url: + image = images.image_from_file(uploaded) + filename = os.path.basename(uploaded.filename) + else: + return flask.render_template("error.html", message="Missing image.") + except Exception as error: + raise + return flask.render_template("error.html", message="Could not read your image.") + # Save the image to a local file + result = images.save_with_thumbnail(image, filename) + return flask.redirect("/show/" + result)