NMPhap commited on
Commit
fef30b3
2 Parent(s): fb24ad1 a8e7e7b

Merge branch 'main' of https://github.com/HunyyDev/SE113.O12_KCPM into NMPhap/testing

Browse files
Files changed (3) hide show
  1. app/dependencies.py +6 -8
  2. app/main.py +0 -6
  3. app/routers/video.py +43 -27
app/dependencies.py CHANGED
@@ -16,28 +16,26 @@ def get_current_user(
16
  user_doc_ref = db.collection("user").document(payload["sub"]).get()
17
  if not user_doc_ref.exists:
18
  raise HTTPException(status_code=400, detail="User profile not found")
19
- except ExpiredIdTokenError:
 
20
  raise HTTPException(
21
  status_code=status.HTTP_401_UNAUTHORIZED,
22
  detail="Token expired",
23
  headers={"WWW-Authenticate": "Bearer"},
24
  )
25
- except InvalidIdTokenError:
 
26
  raise HTTPException(
27
  status_code=status.HTTP_401_UNAUTHORIZED,
28
  detail="Invalid token",
29
  headers={"WWW-Authenticate": "Bearer"},
30
  )
31
- except ValueError:
 
32
  raise HTTPException(
33
  status_code=status.HTTP_401_UNAUTHORIZED,
34
  detail="Invalid token",
35
  headers={"WWW-Authenticate": "Bearer"},
36
  )
37
 
38
- except Exception as e:
39
- logger.info(e)
40
- logger.error(e)
41
- raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)
42
-
43
  return payload
 
16
  user_doc_ref = db.collection("user").document(payload["sub"]).get()
17
  if not user_doc_ref.exists:
18
  raise HTTPException(status_code=400, detail="User profile not found")
19
+ except ExpiredIdTokenError as e:
20
+ logger.warning(e)
21
  raise HTTPException(
22
  status_code=status.HTTP_401_UNAUTHORIZED,
23
  detail="Token expired",
24
  headers={"WWW-Authenticate": "Bearer"},
25
  )
26
+ except InvalidIdTokenError as e:
27
+ logger.warning(e)
28
  raise HTTPException(
29
  status_code=status.HTTP_401_UNAUTHORIZED,
30
  detail="Invalid token",
31
  headers={"WWW-Authenticate": "Bearer"},
32
  )
33
+ except ValueError as e:
34
+ logger.warning(e)
35
  raise HTTPException(
36
  status_code=status.HTTP_401_UNAUTHORIZED,
37
  detail="Invalid token",
38
  headers={"WWW-Authenticate": "Bearer"},
39
  )
40
 
 
 
 
 
 
41
  return payload
app/main.py CHANGED
@@ -4,8 +4,6 @@ from fastapi.responses import RedirectResponse
4
  from app.graphdb.main import insert2PersonAndSetFriend, deleteFriend
5
  from .routers import image, video, friend_request, me
6
 
7
- import uvicorn
8
-
9
  app = FastAPI()
10
 
11
  app.include_router(image.router)
@@ -24,7 +22,3 @@ def hello():
24
  async def test():
25
  await insert2PersonAndSetFriend("1", "2")
26
  await deleteFriend("1", "2")
27
-
28
-
29
- if __name__ == "__main__":
30
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
4
  from app.graphdb.main import insert2PersonAndSetFriend, deleteFriend
5
  from .routers import image, video, friend_request, me
6
 
 
 
7
  app = FastAPI()
8
 
9
  app.include_router(image.router)
 
22
  async def test():
23
  await insert2PersonAndSetFriend("1", "2")
24
  await deleteFriend("1", "2")
 
 
 
 
app/routers/video.py CHANGED
@@ -1,12 +1,12 @@
1
  import asyncio
2
- import json
3
- from multiprocessing import Process
4
  import os
5
  import re
6
  import shutil
7
  import time
8
  import aiofiles
9
  import cv2
 
 
10
  from fastapi import (
11
  APIRouter,
12
  Depends,
@@ -40,9 +40,13 @@ async def handleVideoRequest(
40
 
41
  try:
42
  if user["sub"] is None:
43
- return HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="User not found")
 
 
44
  id = str(now())
45
- _, artifact_ref = db.collection("artifacts").add({"name": id + ".mp4", "status": "pending"})
 
 
46
  os.mkdir(id)
47
  async with aiofiles.open(os.path.join(id, "input.mp4"), "wb") as out_file:
48
  while content := await file.read(1024):
@@ -153,38 +157,50 @@ def updateArtifact(artifactId: str, body):
153
  if not artifact_snapshot.exists:
154
  artifact_snapshot.update(body)
155
  sendMessage(artifactId)
 
 
156
  # This function cannot be automation test because the requirement of another device to receive notification
157
  def sendMessage(artifactId: str, message: str = None):
158
  token = []
159
  artifact = db.collection("artifacts").document(artifactId).get()
160
  if not artifact.exists:
161
  return
162
- user_ref = db.collection("user").where(filter=FieldFilter("artifacts", "array-contains", "artifacts/" + artifactId))
 
 
163
  for user in user_ref:
164
- token.append(user.get().to_dict()['deviceId'])
165
  if message is not None:
166
- messaging.MulticastMessage(data={"notification": {
167
- "title": message,
168
- "body":
169
- "Video " +
170
- artifact.name +
171
- " has done inference. Click here to see the video",
172
- },}, android=messaging.AndroidConfig(
 
 
 
173
  notification=messaging.AndroidNotification(
174
- icon='stock_ticker_update',
175
- color='#f45342'
176
- ),))
 
177
  else:
178
- messaging.MulticastMessage(data={"notification": {
179
- "title": "Video " + artifact.name + " has done inference.",
180
- "body":
181
- "Video " +
182
- artifact.name +
183
- " has done inference. Click here to see the video",
184
- },}, android=messaging.AndroidConfig(
 
 
 
185
  notification=messaging.AndroidNotification(
186
- icon='stock_ticker_update',
187
- color='#f45342'
188
- ),))
 
189
  response = messaging.send_multicast(message)
190
- return response.success_count
 
1
  import asyncio
 
 
2
  import os
3
  import re
4
  import shutil
5
  import time
6
  import aiofiles
7
  import cv2
8
+
9
+ from multiprocessing import Process
10
  from fastapi import (
11
  APIRouter,
12
  Depends,
 
40
 
41
  try:
42
  if user["sub"] is None:
43
+ return HTTPException(
44
+ status_code=status.HTTP_400_BAD_REQUEST, detail="User not found"
45
+ )
46
  id = str(now())
47
+ _, artifact_ref = db.collection("artifacts").add(
48
+ {"name": id + ".mp4", "status": "pending"}
49
+ )
50
  os.mkdir(id)
51
  async with aiofiles.open(os.path.join(id, "input.mp4"), "wb") as out_file:
52
  while content := await file.read(1024):
 
157
  if not artifact_snapshot.exists:
158
  artifact_snapshot.update(body)
159
  sendMessage(artifactId)
160
+
161
+
162
  # This function cannot be automation test because the requirement of another device to receive notification
163
  def sendMessage(artifactId: str, message: str = None):
164
  token = []
165
  artifact = db.collection("artifacts").document(artifactId).get()
166
  if not artifact.exists:
167
  return
168
+ user_ref = db.collection("user").where(
169
+ filter=FieldFilter("artifacts", "array-contains", "artifacts/" + artifactId)
170
+ )
171
  for user in user_ref:
172
+ token.append(user.get().to_dict()["deviceId"])
173
  if message is not None:
174
+ messaging.MulticastMessage(
175
+ data={
176
+ "notification": {
177
+ "title": message,
178
+ "body": "Video "
179
+ + artifact.name
180
+ + " has done inference. Click here to see the video",
181
+ },
182
+ },
183
+ android=messaging.AndroidConfig(
184
  notification=messaging.AndroidNotification(
185
+ icon="stock_ticker_update", color="#f45342"
186
+ ),
187
+ ),
188
+ )
189
  else:
190
+ messaging.MulticastMessage(
191
+ data={
192
+ "notification": {
193
+ "title": "Video " + artifact.name + " has done inference.",
194
+ "body": "Video "
195
+ + artifact.name
196
+ + " has done inference. Click here to see the video",
197
+ },
198
+ },
199
+ android=messaging.AndroidConfig(
200
  notification=messaging.AndroidNotification(
201
+ icon="stock_ticker_update", color="#f45342"
202
+ ),
203
+ ),
204
+ )
205
  response = messaging.send_multicast(message)
206
+ return response.success_count