Commit d315f80e authored by kaiyou's avatar kaiyou
Browse files

First working version for uploads

parent 6f848150
......@@ -2,8 +2,10 @@ from tedimg import app
from flask import send_from_directory
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 =
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)["FULL_STORAGE"], dest))
thumb_size = app.config["THUMB_SIZE"]
image.thumbnail((thumb_size, thumb_size))["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>
{% 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">
<input type="file">
<input type="file" name="file">
<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">
{% 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 class="row center">
<img src="{{ thumb }}">
{% endblock %}
from tedimg import app, images
import flask
import os
......@@ -10,5 +11,32 @@ def index():
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(
image=image, thumb=thumb,
@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
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)
return flask.render_template("error.html", message="Missing image.")
except Exception as error:
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