From 59b540737e5cdf718158720c46bab2d996ce47b2 Mon Sep 17 00:00:00 2001
From: Markus Willman <mpewil@utu.fi>
Date: Thu, 22 Feb 2018 14:48:24 +0200
Subject: [PATCH] better post request argument handling - white space changes -
 exception handling tweaks - remove unneeded files

---
 .dockerignore                               |  1 +
 .gitignore                                  |  5 +++
 client/.gitkeep                             |  0
 client/haarcascade/.gitkeep                 |  0
 contrib/src/web/emotion_gender_processor.py |  2 +-
 contrib/src/web/faces.py                    | 45 ++++++++++++---------
 6 files changed, 33 insertions(+), 20 deletions(-)
 delete mode 100644 client/.gitkeep
 delete mode 100644 client/haarcascade/.gitkeep

diff --git a/.dockerignore b/.dockerignore
index 578118d..df21201 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -6,6 +6,7 @@
 .gitlab-ci.yml
 Dockerfile
 README.md
+client
 
 # temp files
 *.pyc
diff --git a/.gitignore b/.gitignore
index 5fbfdab..84c287f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,8 @@
 *.pyc
 *~
 \#*\#
+
+# client temp files
+client/images/*.*
+client/error.log
+client/debug.log
diff --git a/client/.gitkeep b/client/.gitkeep
deleted file mode 100644
index e69de29..0000000
diff --git a/client/haarcascade/.gitkeep b/client/haarcascade/.gitkeep
deleted file mode 100644
index e69de29..0000000
diff --git a/contrib/src/web/emotion_gender_processor.py b/contrib/src/web/emotion_gender_processor.py
index 6b81edd..eb960be 100644
--- a/contrib/src/web/emotion_gender_processor.py
+++ b/contrib/src/web/emotion_gender_processor.py
@@ -1,4 +1,4 @@
-# this is a slightly modified version of the src/web/emotion_gender_processor.py to provide control over paths
+# this is a slightly modified version of the src/web/emotion_gender_processor.py to provide control over paths and other changes (see README.md)
 
 import os, logging, json
 
diff --git a/contrib/src/web/faces.py b/contrib/src/web/faces.py
index bacf31c..7416d1b 100644
--- a/contrib/src/web/faces.py
+++ b/contrib/src/web/faces.py
@@ -1,4 +1,4 @@
-# this is a slightly modified version of the src/web/faces.py to account for uwsgi import requirements
+# this is a slightly modified version of the src/web/faces.py to account for uwsgi import requirements and other changes (see README.md)
 
 import logging, requests, os, weakref
 
@@ -17,16 +17,15 @@ if not os.path.exists(dirname):
 logging.basicConfig(filename = os.path.join(dirname, 'debug.log'), level = logging.DEBUG)
 
 # PostgreSQL table classes:
-database = PostgresqlDatabase('stop', user='stop', password='PT52lRecp4NBKQrZT9',
-                           host='database')
+database = PostgresqlDatabase('stop', user='stop', password='PT52lRecp4NBKQrZT9', host='database')
 
 # NOTE: most docker images run in UTC by default
 def getCurrentTime(format = None):
-	ts = datetime.now(tzlocal())
-	if not format:
-		return ts
+    ts = datetime.now(tzlocal())
+    if not format:
+        return ts
 
-	return ts.strftime(format)
+    return ts.strftime(format)
 
 class UnknownField(object):
     def __init__(self, *_, **__): pass
@@ -58,8 +57,9 @@ class FileCleaner(object):
 
     def _deleteTask(self, wref):
         filepath = self.weak_references[wref]
-        logging.debug('Deleting request result: {0}'.format(filepath))
-        os.remove(filepath)
+        if filepath:
+            logging.debug('Deleting request result: {0}'.format(filepath))
+            os.remove(filepath)
 
 def initFlask():
     app = Flask(__name__)
@@ -97,8 +97,13 @@ def upload(version, type = 'mem'):
         if not processor.isValid():
             abort(503)
 
-        stopCode = request.args.get('stop_code', default = '', type = str)
-        detectEmotion = request.args.get('detect_emotion', default = 0, type = int)
+        stopCode = request.args.get('stop_code', default = None, type = str)
+        if stopCode == None:
+            stopCode = request.form.get('stop_code', default = 'unknown', type = str)
+        detectEmotion = request.args.get('detect_emotion', default = None, type = int)
+        if detectEmotion == None:
+            detectEmotion = request.form.get('detect_emotion', default = 0, type = int)
+
         result = processor.processImage(request.files['image'].read(), result_fname = getRequestID(request), type = type, detect_emotion = detectEmotion != 0)
 
         # VH Insert result into database
@@ -106,7 +111,7 @@ def upload(version, type = 'mem'):
             i = 0
             ts = getCurrentTime() # only one value for current time per request
             for face in result:
-                Gender_Stats.create(t = stopCode if stopCode else "unknown", t_ts = ts, rowid = i, gender_text = face["gender"])
+                Gender_Stats.create(t = stopCode, t_ts = ts, rowid = i, gender_text = face["gender"])
                 i += 1
 
         # by default avoid all disk I/O by using the result held in memory, explicit type argument will still roundtrip to disk
@@ -137,6 +142,9 @@ def upload(version, type = 'mem'):
         return response
     except Exception as err:
         logging.error('An error has occurred whilst processing the file: "{0}", from: {1}'.format(err, stopCode))
+        if resultFile:
+            os.remove(resultFile)
+
         abort(400)
 
 @app.route('/api/v<int:version>/classifyImage/', methods=['GET'])
@@ -153,7 +161,7 @@ def dowload(version, type = 'mem'):
         if not processor.isValid():
             abort(503)
 
-        stopCode = request.args.get('stop_code', default = '', type = str)
+        stopCode = request.args.get('stop_code', default = 'unknown', type = str)
         detectEmotion = request.args.get('detect_emotion', default = 0, type = int)
         result = processor.processImage(requests.get(uri).content, result_fname = getRequestID(request), type = type, detect_emotion = detectEmotion != 0)
 
@@ -162,7 +170,7 @@ def dowload(version, type = 'mem'):
             i = 0
             ts = getCurrentTime() # only one value for current time per request
             for face in result:
-                Gender_Stats.create(t = stopCode if stopCode else "unknown", t_ts = ts, rowid = i, gender_text = face["gender"])
+                Gender_Stats.create(t = stopCode, t_ts = ts, rowid = i, gender_text = face["gender"])
                 i += 1
 
         # by default avoid all disk I/O by using the result held in memory, explicit type argument will still roundtrip to disk
@@ -186,17 +194,16 @@ def dowload(version, type = 'mem'):
         if response == None:
             raise Exception('Unsupported type requested.')
 
-        
         cleaner.cleanup(response, resultFile)
         # require clients to explicitly handle caching results, HTTP caches are unpredictable
         response.headers.extend({'Cache-Control': 'no-cache', 'Expires': '0'})
-        
-        
-        return response
 
-        
+        return response
     except Exception as err:
         logging.error('An error has occurred whilst processing the file: "{0}", from: {1}'.format(err, stopCode))
+        if resultFile:
+            os.remove(resultFile)
+
         abort(400)
 
 @app.errorhandler(400)
-- 
GitLab