-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (52 loc) · 2.21 KB
/
app.py
File metadata and controls
64 lines (52 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import pathlib
import requests
from fastapi import FastAPI, HTTPException, status, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from loguru import logger
from pydantic import BaseModel
from uuid import uuid4
from config import settings
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS", "PATCH", "DELETE", "PUT"],
allow_headers=["*"],
)
class BasicResponse(BaseModel):
cid: str
ipfs_link: str
@app.post("/start")
def start_record():
try:
response = requests.get(settings.camera_overview_vid_start)
return Response(status_code=status.HTTP_200_OK)
except Exception as e:
logger.error("Could not start the recording process.")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))
@app.get("/stop")
def stop_record():
try:
file_response: FileResponse = requests.get(settings.camera_overview_vid_stop)
filename = uuid4().hex
video_path = pathlib.Path(f"{settings.video_path}/{filename}.mp4")
if not os.path.isdir(settings.video_path):
os.mkdir(settings.video_path)
with open(video_path, "wb") as f:
f.write(file_response.content)
if not settings.ipfs_gateway_uri:
raise ValueError("IPFS_GATEWAY_URI not provided")
with open(video_path, 'rb') as f:
response = requests.post(f"{settings.ipfs_gateway_uri}publish-to-ipfs/upload-file", files={"file_data": f})
if response.status_code != 200:
message = f"Could not post to IPFS. Status code: {response.status_code}; details: {response.json()}"
logger.error(message)
return HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=message)
basic_response = {"ipfs_cid": response.json()["ipfs_cid"], "ipfs_link": response.json()["ipfs_link"]}
return basic_response
except Exception as e:
logger.error("Could not stop the recording process.")
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))