|
1 | 1 | from typing import Annotated
|
2 | 2 |
|
3 | 3 | from annotated_types import Len
|
4 |
| -from fastapi import APIRouter, status |
5 |
| -from fastapi.responses import JSONResponse |
| 4 | +from fastapi import APIRouter, Path, Response, status |
6 | 5 |
|
7 | 6 | from pastemc.models.common import FileObjectResponse, ObjectNotFound
|
8 | 7 | from pastemc.utils.s3api import list_objects
|
|
16 | 15 | summary="Get File URL by file_id",
|
17 | 16 | responses={404: {"model": ObjectNotFound}},
|
18 | 17 | )
|
19 |
| -async def fetch_file(file_id: Annotated[str, Len(10, 26)]): |
| 18 | +async def fetch_file( |
| 19 | + response: Response, |
| 20 | + file_id: Annotated[ |
| 21 | + str, |
| 22 | + Len(10, 26), |
| 23 | + Path( |
| 24 | + description="this field accepts:\n- full ULID (26 digits)\n- partial ULID, with timestamp (at least 10 digits)", |
| 25 | + examples=["01HRKVWPKNYNQKB5F209DZ85B7", "01HRKVWPKN"], |
| 26 | + ), |
| 27 | + ], |
| 28 | +): |
20 | 29 | if len(file_id) == 26: # full ULID
|
21 | 30 | return FileObjectResponse.public(file_id=file_id)
|
22 |
| - else: # partial ULID, with timestamp (1-10 digits) |
| 31 | + else: # partial ULID, with timestamp (at least 10 digits) |
23 | 32 | if objects_list := await list_objects(prefix=file_id):
|
24 | 33 | return FileObjectResponse.public(file_id=objects_list[0].object_name)
|
25 | 34 | else:
|
26 |
| - return JSONResponse( |
27 |
| - ObjectNotFound.make(file_id), status_code=status.HTTP_404_NOT_FOUND |
28 |
| - ) |
| 35 | + response.code = status.HTTP_404_NOT_FOUND |
| 36 | + return ObjectNotFound.make(file_id) |
0 commit comments