51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import tempfile
|
|
|
|
from flask import request, abort, current_app, Response
|
|
from flask_classful import FlaskView
|
|
import opentracing
|
|
from schemas import SampleSchema
|
|
import json
|
|
import uwsgi
|
|
import pickle
|
|
|
|
class FilterView(FlaskView):
|
|
sampleschema = SampleSchema(many=False)
|
|
|
|
def post(self):
|
|
if current_app.config.get('DROPALL'):
|
|
return Response(status=200)
|
|
|
|
with opentracing.tracer.start_active_span('parseAndValidate'):
|
|
if 'file' not in request.files:
|
|
return abort(400, "no file found")
|
|
else:
|
|
soundfile = request.files['file']
|
|
|
|
if 'description' not in request.form:
|
|
return abort(400, "no description found")
|
|
else:
|
|
description_raw = request.form.get("description")
|
|
|
|
if soundfile.content_type != 'audio/wave':
|
|
current_app.logger.info(f"Input file was not WAV.")
|
|
return abort(415, 'Input file not a wave file.')
|
|
try:
|
|
desc = self.sampleschema.loads(description_raw)
|
|
except Exception as e:
|
|
current_app.logger.exception(e)
|
|
return abort(417, 'Input JSON schema invalid')
|
|
|
|
soundfile_handle, soundfile_path = tempfile.mkstemp(suffix=".wav")
|
|
with open(soundfile_handle, "wb+") as f:
|
|
soundfile.save(f)
|
|
|
|
task = {
|
|
"audio_file_path": soundfile_path,
|
|
"description": desc
|
|
}
|
|
|
|
uwsgi.mule_msg(pickle.dumps(task))
|
|
|
|
return Response(status=200)
|