Commit c4f9da83 authored by kaiyou's avatar kaiyou

Handle pasted urls properly

parent 00566d84
......@@ -4,4 +4,10 @@ $(document).ready(function() {
$("form#upload").submit();
});
$("form#upload input[type=text]").on('paste', function() {
setTimeout(function () {
$("form#upload").submit();
}, 100);
});
});
......@@ -2,6 +2,7 @@ from tedimg import app
from flask import send_from_directory
app.config.update(
SITE_NAME="TeDomum Images",
FULL_STORAGE="./tedimg/static/images",
THUMB_STORAGE="./tedimg/static/images/thumb",
FULL_WEB="static/images",
......
......@@ -3,6 +3,8 @@ from PIL import Image
import os
import binascii
import requests
import io
def get_image(root, name):
......@@ -23,10 +25,14 @@ def image_from_file(file_storage):
def image_from_url(url):
""" Try and download an image from the given url.
"""
response = requests.get(url)
image = Image.open(io.BytesIO(response.content))
return image
def save_with_thumbnail(image, filename):
dest = "."
print(filename)
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')
......
......@@ -18,21 +18,20 @@ def show(path):
@app.route('/upload', methods=['POST'])
def upload():
uploaded = flask.request.files['file']
url = flask.request.form['url']
uploaded = flask.request.files['file']
# 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)
image = images.image_from_url(url)
filename = os.path.basename(url)
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.")
return flask.render_template("error.html", message="Could not store your image.")
# Save the image to a local file
result = images.save_with_thumbnail(image, filename)
return flask.redirect("/show/" + result)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment