This commit is contained in:
@ -1,15 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
from app import rabbitmq_channel
|
||||
import logging
|
||||
import json
|
||||
import rstr
|
||||
from flask_restful import Resource, reqparse
|
||||
from werkzeug.datastructures import FileStorage
|
||||
from xeger import Xeger
|
||||
from flask_restful import Resource
|
||||
from flask import request
|
||||
import requests
|
||||
import filetype
|
||||
from db import db
|
||||
from models import SampleMetadata
|
||||
from schemas import *
|
||||
from rabbitmqqueue import rabbitmq_channel
|
||||
from config import *
|
||||
|
||||
"""
|
||||
@ -35,35 +33,33 @@ class SampleResource(Resource):
|
||||
Post request send to the endpoint
|
||||
:return:
|
||||
"""
|
||||
parse = reqparse.RequestParser()
|
||||
parse.add_argument('soundFile', type=FileStorage, location='soundFile')
|
||||
parse.add_argument('description', type=str, location='description')
|
||||
args = parse.parse_args()
|
||||
soundFile = args['soundFile']
|
||||
description = args['description']
|
||||
kind = filetype.guess(soundFile)
|
||||
if kind.mime != 'wav':
|
||||
if 'file' not in request.files:
|
||||
return {"status": "error", "message": "no file found"}, 469
|
||||
else:
|
||||
soundfile = request.files['file']
|
||||
|
||||
if 'date' not in request.form:
|
||||
return {"status": "error", "message": "no date found"}, 470
|
||||
else:
|
||||
date = request.form.get("date")
|
||||
|
||||
if 'device_id' not in request.form:
|
||||
return {"status": "error", "message": "no device_id found"}, 471
|
||||
else:
|
||||
device_id = request.form.get("device_id")
|
||||
|
||||
kind = filetype.guess(soundfile)
|
||||
if kind is None or kind.mime != 'wav':
|
||||
LOGGER.error(
|
||||
"Input file was not WAV. Recieved metadata: {}",
|
||||
description)
|
||||
f"Input file was not WAV. Recieved metadata: device_id: {device_id}")
|
||||
return {'status': 'error', 'message': 'Input file not WAV.'}, 415
|
||||
try:
|
||||
desc = json.loads(description)
|
||||
except Exception as e:
|
||||
LOGGER.exception(e)
|
||||
return {'status': 'error',
|
||||
'message': 'Input JSON could not be parsed'}, 400
|
||||
validate_errors = SampleSchema().validate(desc)
|
||||
if validate_errors:
|
||||
LOGGER.error(
|
||||
"Input JSON did not conform to schema. It was: {}", desc)
|
||||
return {'status': 'error',
|
||||
'message': 'Input JSON schema invalid'}, 417
|
||||
generated_tag = rstr.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$', 2, 32)
|
||||
|
||||
xeger = Xeger(limit=32)
|
||||
generated_tag = xeger.xeger(r'^[a-zA-Z]+[0-9a-zA-Z_]*$')
|
||||
|
||||
record = SampleMetadata(
|
||||
device_id=desc['device_id'],
|
||||
device_date=desc['date'],
|
||||
device_id=device_id,
|
||||
device_date=date,
|
||||
tag=generated_tag)
|
||||
try:
|
||||
db.session.add(record)
|
||||
@ -73,7 +69,7 @@ class SampleResource(Resource):
|
||||
'tag': (None, generated_tag),
|
||||
'file': (
|
||||
'wave.wav',
|
||||
soundFile,
|
||||
soundfile,
|
||||
kind.mime)})
|
||||
rabbitmq_channel.basic_publish(
|
||||
exchange=RABBITMQ_EXCHANGE,
|
||||
@ -93,7 +89,7 @@ class SampleResource(Resource):
|
||||
Get all stored items
|
||||
:return:
|
||||
"""
|
||||
samples = SampleMetadata.query.get.all()
|
||||
samples = SampleMetadata.query.all()
|
||||
return {"status": "ok", "message": samples}, 200
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user