Handle errors (e.g. somebody uploading a bad image)
- ID
6f08e32- date
2022-05-05 06:38:06+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
8e06c07- message
Handle errors (e.g. somebody uploading a bad image)- changed files
3 files, 39 additions, 5 deletions
Changed files
webapp/server.py (2482) → webapp/server.py (2943)
diff --git a/webapp/server.py b/webapp/server.py
index 3a35730..9534703 100755
--- a/webapp/server.py
+++ b/webapp/server.py
@@ -3,14 +3,17 @@
import base64
import colorsys
import os
+import secrets
import subprocess
+import sys
import tempfile
-from flask import Flask, redirect, render_template, request
+from flask import Flask, flash, redirect, render_template, request
import wcag_contrast_ratio as contrast
app = Flask(__name__)
+app.secret_key = secrets.token_hex()
VERSION = subprocess.check_output(["dominant_colours", "--version"]).decode("utf8")
@@ -55,10 +58,20 @@ def get_palette():
# when running dominant_colours.
tmp_file.flush()
- result = subprocess.check_output(
- ["dominant_colours", tmp_file.name, "--no-palette", "--max-colours=5"]
- )
- colours = result.decode("utf8").strip().split("\n")
+ proc = subprocess.Popen([
+ 'dominant_colours', tmp_file.name, '--no-palette', '--max-colours=5'
+ ],stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE)
+ stdout, stderr = proc.communicate()
+ return_code = proc.poll()
+
+ if return_code != 0:
+ stderr = stderr.decode(sys.stdin.encoding)
+ flash(f'Something went wrong:<br/>{stderr}')
+ return redirect('/')
+
+ stdout = stdout.decode(sys.stdin.encoding)
+ colours = stdout.strip().split("\n")
with tempfile.NamedTemporaryFile(suffix="jpg") as thumbnail_file:
subprocess.check_call(
webapp/static/style.css (2097) → webapp/static/style.css (2253)
diff --git a/webapp/static/style.css b/webapp/static/style.css
index 5c7b01a..fd4ebcd 100644
--- a/webapp/static/style.css
+++ b/webapp/static/style.css
@@ -130,3 +130,12 @@ img {
height: 42px;
}
}
+
+.errors {
+ background: rgba(208, 28, 17, 0.2);
+ padding: 1em;
+ border: 3px solid #d01c11;
+ color: #d01c11;
+ font-weight: 500;
+ border-radius: 5px;
+}
webapp/templates/base.html (734) → webapp/templates/base.html (1004)
diff --git a/webapp/templates/base.html b/webapp/templates/base.html
index 9015714..437a678 100644
--- a/webapp/templates/base.html
+++ b/webapp/templates/base.html
@@ -11,6 +11,18 @@
<body>
<main>
<h2>find the dominant colours in an image</h2>
+
+
+ {% with messages = get_flashed_messages() %}
+ {% if messages %}
+ <div class="errors">
+ {% for message in messages %}
+ {{ message | safe }}
+ {% endfor %}
+ </div>
+ {% endif %}
+ {% endwith %}
+
{% block content %}
{% endblock %}
</main>