Beamlnwza commited on
Commit
1c3d77b
·
1 Parent(s): 64396e4

view index fix

Browse files
Files changed (2) hide show
  1. src/endpoints/view.py +27 -3
  2. src/models/view.py +15 -0
src/endpoints/view.py CHANGED
@@ -1,6 +1,8 @@
1
  from fastapi import APIRouter
2
 
3
  from src.libs.s3 import s3resource
 
 
4
  from uuid import UUID
5
 
6
  router = APIRouter(
@@ -16,10 +18,32 @@ async def status():
16
 
17
 
18
  @router.get("/")
19
- async def view(user: UUID, index: int | None = None):
20
  s3 = s3resource()
21
  bucket = s3.Bucket('pimthaigans-image-container')
 
22
  objs = list(bucket.objects.filter(Prefix=f'{user}/'))
 
 
 
 
 
23
  if index:
24
- return objs[index].key
25
- return [obj.key for obj in objs]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import APIRouter
2
 
3
  from src.libs.s3 import s3resource
4
+ from src.models.view import ViewResult, ViewImage
5
+ from src.models.main import User, Method
6
  from uuid import UUID
7
 
8
  router = APIRouter(
 
18
 
19
 
20
  @router.get("/")
21
+ async def view(user: UUID, index: int | None = None) -> ViewResult:
22
  s3 = s3resource()
23
  bucket = s3.Bucket('pimthaigans-image-container')
24
+
25
  objs = list(bucket.objects.filter(Prefix=f'{user}/'))
26
+ path_objs = [obj.key for obj in objs]
27
+
28
+ if len(path_objs) == 0:
29
+ return ViewResult(user=User(uuid=user), method=Method.index, result=None)
30
+
31
  if index:
32
+ result: ViewResult = await view_index(User(uuid=user), index, path_objs)
33
+ return result
34
+
35
+ result: ViewResult = await view_all(User(uuid=user), path_objs)
36
+ return result
37
+
38
+ async def view_index(user: User, index: int, path_objs) -> ViewResult:
39
+ imgs = [obj for obj in path_objs if obj.endswith(f'{str(index).zfill(2)}.png')]
40
+ if len(imgs) > 0:
41
+ return ViewResult(user=user, method=Method.index, result=[ViewImage(index=index, image_url=imgs[0])])
42
+
43
+ return ViewResult(user=user, method=Method.index, result=None)
44
+
45
+ async def view_all(user: User, path_objs) -> ViewResult:
46
+ for index in range(0, 88):
47
+ imgs = [obj for obj in path_objs if obj.endswith(f'{str(index).zfill(2)}.png')]
48
+ if len(imgs) > 0:
49
+ return ViewResult(user=user, method=Method.all, result=[ViewImage(index=index, image_url=imgs[0])])
src/models/view.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+
3
+ from pydantic import BaseModel
4
+
5
+ from src.models.main import User, Method
6
+
7
+
8
+ class ViewImage(BaseModel):
9
+ index: int
10
+ image_url: str
11
+
12
+ class ViewResult(BaseModel):
13
+ user: User
14
+ method: Method
15
+ result: List[ViewImage] | None