Commit c4f9da83 authored by kaiyou's avatar kaiyou

Handle pasted urls properly

parent 00566d84
...@@ -4,4 +4,10 @@ $(document).ready(function() { ...@@ -4,4 +4,10 @@ $(document).ready(function() {
$("form#upload").submit(); $("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 ...@@ -2,6 +2,7 @@ from tedimg import app
from flask import send_from_directory from flask import send_from_directory
app.config.update( app.config.update(
SITE_NAME="TeDomum Images",
FULL_STORAGE="./tedimg/static/images", FULL_STORAGE="./tedimg/static/images",
THUMB_STORAGE="./tedimg/static/images/thumb", THUMB_STORAGE="./tedimg/static/images/thumb",
FULL_WEB="static/images", FULL_WEB="static/images",
......
...@@ -3,6 +3,8 @@ from PIL import Image ...@@ -3,6 +3,8 @@ from PIL import Image
import os import os
import binascii import binascii
import requests
import io
def get_image(root, name): def get_image(root, name):
...@@ -23,10 +25,14 @@ def image_from_file(file_storage): ...@@ -23,10 +25,14 @@ def image_from_file(file_storage):
def image_from_url(url): def image_from_url(url):
""" Try and download an image from the given 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): def save_with_thumbnail(image, filename):
dest = "." dest = "."
print(filename)
while os.path.exists(os.path.join(app.config["FULL_STORAGE"], dest)): while os.path.exists(os.path.join(app.config["FULL_STORAGE"], dest)):
filename, ext = os.path.splitext(filename) filename, ext = os.path.splitext(filename)
random = binascii.hexlify(os.urandom(3)).decode('utf8') random = binascii.hexlify(os.urandom(3)).decode('utf8')
......
...@@ -18,21 +18,20 @@ def show(path): ...@@ -18,21 +18,20 @@ def show(path):
@app.route('/upload', methods=['POST']) @app.route('/upload', methods=['POST'])
def upload(): def upload():
uploaded = flask.request.files['file']
url = flask.request.form['url'] url = flask.request.form['url']
uploaded = flask.request.files['file']
# Get an image object from the uploaded image or URL # Get an image object from the uploaded image or URL
try: try:
if uploaded: if uploaded:
image = images.image_from_file(uploaded) image = images.image_from_file(uploaded)
filename = os.path.basename(uploaded.filename) filename = os.path.basename(uploaded.filename)
elif url: elif url:
image = images.image_from_file(uploaded) image = images.image_from_url(url)
filename = os.path.basename(uploaded.filename) filename = os.path.basename(url)
else: else:
return flask.render_template("error.html", message="Missing image.") return flask.render_template("error.html", message="Missing image.")
except Exception as error: except Exception as error:
raise return flask.render_template("error.html", message="Could not store your image.")
return flask.render_template("error.html", message="Could not read your image.")
# Save the image to a local file # Save the image to a local file
result = images.save_with_thumbnail(image, filename) result = images.save_with_thumbnail(image, filename)
return flask.redirect("/show/" + result) 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