diff --git a/cgi-bin/upload.py b/cgi-bin/upload.py index 1167e0f..8489e9c 100755 --- a/cgi-bin/upload.py +++ b/cgi-bin/upload.py @@ -1,48 +1,61 @@ #!/usr/bin/env python3 """ Created on Sat Mar 28 19:29:28 2020 This module implements a CGI service for uploading voice records in the coughvid web application. @author: T. Teijeiro """ import cgi import datetime +import json #TODO disable cgitb in production import cgitb cgitb.enable() #Point this variable to the folder where the data will be stored DB_PATH = '/tmp' form = cgi.FieldStorage() -# Get filename here. -fileitem = form['audio_data'] -latitude = form.getvalue('lat') -longitude = form.getvalue('lon') +#First we check if we are receiving a recording or metadata mimeType = form.getvalue('mimeType') -ext = 'wav' -if 'ogg' in mimeType: - ext = 'ogg' -elif 'webm' in mimeType: - ext = 'webm' -label = 1 -data = fileitem.file.read() -#We limit the file size to 1MB, to prevent possible flooding attacks. -if len(data) < 1000000: - # Test if the file was uploaded - filename = f'{datetime.datetime.now().isoformat()}_{latitude}_{longitude}_{label}.{ext}' - with open(f'{DB_PATH}/{filename}', 'wb') as fout: - fout.write(data) - message = 'The recording was uploaded successfully' -else: - message = 'Record too long.' +if mimeType == "json": + uid = form.getvalue('uuid') + if uid is not None and uid != 'null': + with open(f'{DB_PATH}/{uid}.json', 'w') as fout: + dct = {k:form.getvalue(k) for k in form.keys()} + json.dump(dct, fout) + message = 'The metadata was properly stored' + else: + message = 'Not valid UUID' +else: + # Get filename here. + fileitem = form['audio_data'] + latitude = form.getvalue('lat') + longitude = form.getvalue('lon') + mimeType = form.getvalue('mimeType') + uid = form.getvalue('uuid') + ext = 'wav' + if 'ogg' in mimeType: + ext = 'ogg' + elif 'webm' in mimeType: + ext = 'webm' + data = fileitem.file.read() + #We limit the file size to 1MB, to prevent possible flooding attacks. + if len(data) < 1000000: + # Test if the file was uploaded + filename = f'{uid}_{datetime.datetime.now().isoformat()}_{latitude}_{longitude}.{ext}' + with open(f'{DB_PATH}/{filename}', 'wb') as fout: + fout.write(data) + message = 'The recording was uploaded successfully' + else: + message = 'Record too long.' print(f"""\ Content-Type: text/html\n
{message}
""") diff --git a/index.html b/index.html index fd0d19b..08e1f4c 100644 --- a/index.html +++ b/index.html @@ -1,530 +1,580 @@