Page MenuHomec4science

play_recording.py
No OneTemporary

File Metadata

Created
Fri, Oct 18, 11:25

play_recording.py

#!/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 glob
import sys
import os.path
#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()
#First we check if we are receiving a recording or metadata
rec_id = form.getvalue('id')
if rec_id is not None:
files = glob.glob(f'{DB_PATH}/{rec_id}_*')
if len(files) == 1:
file = files[0]
_, ext = os.path.splitext(file)
if len(ext) > 1:
with open(file, 'rb') as f:
print(f"Content-Type: audio/{ext[1:]}\n")
sys.stdout.flush()
sys.stdout.buffer.write(f.read())
sys.exit(0)
print(f"""\
Content-Type: text/html\n
<html>
<body>
<p>Unexpected.</p>
</body>
</html>
""")

Event Timeline