Commit d315f80e authored by kaiyou's avatar kaiyou

First working version for uploads

parent 6f848150
......@@ -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)
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
{% extends "base.html" %}
{% block content %}
<div class="section no-pad-bot" id="index-banner">
<div class="container">
<h1 class="red-text">Upload failed!</h1>
<h2>{{ message }}</h2>
<br><br>
</div>
</div>
{% endblock %}
......@@ -2,11 +2,11 @@
{% block banner_content %}
<h1 class="header center orange-text">Upload your image!</h1>
<form id="upload" action="#">
<form method="post" id="upload" action="/upload" enctype="multipart/form-data">
<div class="file-field input-field">
<div class="btn light-blue">
<span>File</span>
<input type="file">
<input type="file" name="file">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" placeholder="Upload your image">
......@@ -15,7 +15,7 @@
<div class="input-field">
<i class="material-icons prefix">public</i>
<input id="icon_prefix" type="text" class="validate" placeholder="Or paste your url">
<input id="icon_prefix" type="text" class="validate" name="url" placeholder="Or paste your url">
</div>
</form>
{% endblock %}
......@@ -4,6 +4,10 @@
<h1 class="header center orange-text">Upload successful!</h1>
<div class="row center">
<a href="{{ image }}" class="btn-large waves-effect waves-light light-blue">Direct link</a>
<a href="{{ thumb }}" class="btn-large waves-effect waves-light light-blue">Thumbnail</a>
</div>
<div class="row center">
<img src="{{ thumb }}">
</div>
{% endblock %}
......
from tedimg import app, images
import flask
import os
@app.route('/')
......@@ -10,5 +11,32 @@ def index():
@app.route('/show/<path:path>')
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)
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