Commit 6181ae9f authored by Schwaighofer Helga's avatar Schwaighofer Helga

Exception Handeling fixed

parent 21828daa
......@@ -11,7 +11,7 @@ from io import BytesIO
app = Flask(__name__)
# To enable logging for flask-cors,
app.logger.setLevel(logging.DEBUG)
app.logger.setLevel(logging.INFO)
CORS(app)
......@@ -23,12 +23,20 @@ def index():
# Post Route - Receive advice from supporting shellfish
@app.route('/advice', methods=['POST'])
def advice():
try:
image_json = json.dumps(request.get_json())
data = json.loads(image_json)
seperatingPosition = data['image'].find(',')
image_bytes = data['image'][seperatingPosition+1:]
image_json = json.dumps(request.get_json())
data = json.loads(image_json)
seperatingPosition = data['image'].find(',')
image_bytes = data['image'][seperatingPosition+1:]
image_metadata = data['image'][:seperatingPosition]
# find() returns -1 in case str is not found
if image_metadata.find('image') == -1:
app.logger.error('File is not an image')
user_image = None
msg = jsonify({"message": "bad-type"})
return make_response(msg, 400)
try:
im = Image.open(BytesIO(base64.b64decode(image_bytes)))
im.save('temp.png','PNG')
......@@ -46,8 +54,8 @@ def advice():
return make_response(msg, 200)
except:
user_image = None
msg = jsonify({"message": "bad-type"})
return make_response(msg, 400)
msg = jsonify({"message": "general-error"})
return make_response(msg, 401)
def main():
......
......@@ -30,7 +30,7 @@ header {
background-color: #f1f1f1;
}
#upload-photo {
#open-filesystem {
opacity: 0;
position: absolute;
z-index: -1;
......
......@@ -21,7 +21,10 @@ function previewFile() {
function uploadFile() {
var uploadFileButton = document.getElementById('upload-file');
var openFileButton = document.getElementById('open-filesystem-btn');
uploadFileButton.classList.add('is-loading');
openFileButton.classList.add('is-disabled');
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://127.0.0.1:5000/advice');
xhr.setRequestHeader('Content-Type', 'application/json');
......@@ -33,6 +36,7 @@ function uploadFile() {
document.getElementById('shelly').src="./static/images/Shelly_"+response.emotion+".png";
document.getElementById('p1').innerHTML = "I can see your "+response.emotion+"! Take my advice: ";
document.getElementById('p2').innerHTML = response.advice;
openFileButton.classList.remove('is-disabled');
}
else {
if (xhr.status === 400){
......@@ -46,6 +50,7 @@ function uploadFile() {
document.getElementById('shelly').src="./static/images/Shelly_neutral.png";
document.getElementById('p1').innerHTML = "I'm Shelly, the Supporting Shellfish.";
document.getElementById('p2').innerHTML = "Please upload a picture of your face.";
openFileButton.classList.remove('is-disabled');
}
};
......
This image diff could not be displayed because it is too large. You can view the blob instead.
......@@ -30,8 +30,8 @@
<img id="img_preview" src="">
</div>
<div class="bottom">
<label for="upload-photo" class="button">Browse for image ...</label>
<input type="file" name="photo" id="upload-photo" accept="image/*" onchange="previewFile()" />
<label for="open-filesystem" id="open-filesystem-btn" class="button">Browse for image ...</label>
<input type="file" name="photo" id="open-filesystem" accept="image/*" onchange="previewFile()" />
<button class="button" type="button" onclick="uploadFile()" id="upload-file" style="margin-top:-5px;" disabled>Upload File</button>
<div class="alert is-error" id="bad-type-error" style="display:none; margin-top: 10px">The uploaded file is not an image.</div>
<div class="alert is-error" id="general-error" style="display:none; margin-top: 10px">Oh no! Something went wrong. Please try again.</div>
......
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