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 %} +