Skip to content

Commit

Permalink
Restrict upload format to ogg and add format conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Microwave-WYB committed May 21, 2024
1 parent bb72e1d commit c81f2dc
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions phone_sensors/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from pathlib import Path
from uuid import UUID

from fastapi import Depends, FastAPI, Form, UploadFile
from fastapi import Depends, FastAPI, Form, HTTPException, UploadFile
from fastapi.responses import RedirectResponse
from pydub import AudioSegment
from redis import Redis

from phone_sensors.birdnet import submit_analyze_audio_job
Expand Down Expand Up @@ -51,7 +52,7 @@ async def upload(
Data will be queued to be processed by the server.
Returns a job ID in 128-bit UUID format.
"""
metadata = SensorStatus(
status = SensorStatus(
sensor_id=sensor_id,
timestamp=timestamp,
lat=lat,
Expand All @@ -60,7 +61,16 @@ async def upload(
battery=battery,
temperature=temperature,
)
file_path = Path(tempfile.mktemp(suffix=".wav"))
file_path.write_bytes(await audio_file.read())
job_id = submit_analyze_audio_job(redis_conn, file_path, sensor_status=metadata)
status.add_coordinates()
audio_data = await audio_file.read()
file_name = audio_file.filename or f"{datetime.datetime.now(datetime.UTC).isoformat()}.wav"
if not file_name.endswith(".ogg"):
raise HTTPException(status_code=400, detail="Only .ogg files are supported.")
file_path = Path(tempfile.gettempdir()) / file_name
with open(file_path, "wb") as file:
file.write(audio_data)
audio = AudioSegment.from_wav(file_path)
audio.export(file_path, format="wav")

job_id = submit_analyze_audio_job(redis_conn, file_path, status)
return job_id

0 comments on commit c81f2dc

Please sign in to comment.