Skip to content

Commit b9d2ecb

Browse files
feat: use request.app.url_for (#446)
add chore:refractor Update reverse.py add add add add Update reverse.py add add beat back add add feat: use request.app.url_path_for Update login.py add
1 parent bf18a65 commit b9d2ecb

6 files changed

Lines changed: 18 additions & 21 deletions

File tree

src/backend/app/routes/http/login.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,3 @@ async def login_endpoint(
4949
)
5050

5151
return Token(access_token=token_string, token_type="bearer")
52-
53-

src/backend/app/routes/http/reverse.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from urllib.parse import quote
55

66
from botocore.exceptions import ClientError
7-
from fastapi import APIRouter, Header, HTTPException, UploadFile, status, Request
7+
from fastapi import APIRouter, Header, HTTPException, Request, UploadFile, status
88
from fastapi.responses import StreamingResponse
99
from sqlmodel import col, select
1010
from types_aiobotocore_s3.type_defs import CompletedPartTypeDef
@@ -20,8 +20,8 @@
2020
RoomOut,
2121
)
2222
from app.settings import settings
23-
from app.states.room import RoomState
2423
from app.states.app import UploadProgress
24+
from app.states.room import RoomState
2525
from app.tasks.clean_file import delete_expired_file
2626
from app.tasks.persist_file import persist_file_record
2727

@@ -86,7 +86,6 @@ async def add_host(
8686
async def upload_file_to_room(
8787
room_id: str,
8888
file: UploadFile,
89-
file: UploadFile,
9089
request: Request,
9190
s3: S3Dep,
9291
x_host_token: str = Header(),
@@ -208,13 +207,13 @@ async def upload_file_to_room(
208207
filename=filename,
209208
size=uploaded_size,
210209
uploaded_at=now,
211-
download_url=f"{settings.ROOT_PATH}/download/{file_key}",
210+
download_url=str(request.app.url_path_for("download_files", key=file_key)),
212211
)
213212

214213
# Persist in room state + fan-out event via pub/sub (atomic append)
215214
added = await RoomState.add_file(room_id, entry)
216215
if not added:
217-
# Room vanished mid-upload clean up the S3 object
216+
# Room vanished mid-upload clean up the S3 object
218217
await s3.delete_object(Bucket=settings.RUSTFS_BUCKET_NAME, Key=file_key)
219218
# Remove in-flight upload from global state
220219
await UploadProgress.cancel(upload_key=file_key)

src/backend/app/routes/ws/reverse.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
async def _stream_file_to_ws(ws: WebSocket, entry: RoomFileEntry) -> None:
2121
"""Fetch a file from RustFS and relay every chunk to *ws*.
2222
23-
Mirrors the streaming pattern in ``download.py`` the client receives raw
23+
Mirrors the streaming pattern in ``download.py`` - the client receives raw
2424
bytes without ever knowing about the backing S3 store.
2525
"""
2626
session = aioboto3.Session()
@@ -46,7 +46,7 @@ async def _stream_file_to_ws(ws: WebSocket, entry: RoomFileEntry) -> None:
4646
)
4747
return
4848

49-
# Header tells the client which file is coming and how large it is
49+
# Header - tells the client which file is coming and how large it is
5050
await ws.send_text(
5151
json.dumps(
5252
{
@@ -58,14 +58,14 @@ async def _stream_file_to_ws(ws: WebSocket, entry: RoomFileEntry) -> None:
5858
)
5959
)
6060

61-
# Binary chunks same as ``async for chunk in s3_response["Body"]``
61+
# Binary chunks - same as ``async for chunk in s3_response["Body"]``
6262
try:
6363
async for chunk in s3_response["Body"]:
6464
await ws.send_bytes(chunk)
6565
finally:
6666
s3_response["Body"].close()
6767

68-
# Footer signals the client that this file is complete
68+
# Footer - signals the client that this file is complete
6969
await ws.send_text(json.dumps({"type": "file_end", "key": entry.key}))
7070

7171

@@ -87,15 +87,15 @@ async def room_ws(ws: WebSocket, room_id: str, host_token: str | None = None):
8787

8888
await ws.accept()
8989

90-
# ── subscribe FIRST so no events are lost between snapshot and listen ──
90+
# subscribe FIRST so no events are lost between snapshot and listen
9191
sub_client = aioredis.from_url(
9292
settings.REDIS_ENDPOINT, encoding="utf-8", decode_responses=True
9393
)
9494
pubsub = sub_client.pubsub()
9595
channel = RoomState.channel_for(room_id)
9696
await pubsub.subscribe(channel)
9797

98-
# ── snapshot (re-read after subscribe to guarantee consistency) ───
98+
# snapshot (re-read after subscribe to guarantee consistency)
9999
room = await RoomState.get(room_id)
100100
if room is None:
101101
await pubsub.unsubscribe(channel)
@@ -137,14 +137,14 @@ async def _listen_events() -> None:
137137
except Exception:
138138
continue
139139

140-
# Host destroyed the room notify client and close
140+
# Host destroyed the room - notify client and close
141141
if data.get("type") == "room_destroyed":
142142
await ws.send_text(json.dumps({"type": "room_destroyed"}))
143143
file_queue.put_nowait(None)
144144
await ws.close(code=4001, reason="Room destroyed by host")
145145
return
146146

147-
# Host count changed forward directly
147+
# Host count changed - forward directly
148148
if data.get("type") == "host_count":
149149
await ws.send_text(json.dumps(data))
150150
continue
@@ -157,7 +157,7 @@ async def _listen_events() -> None:
157157

158158
if event.event == "file_added":
159159
if event.file.key in seen_keys:
160-
# Already queued from the snapshot skip duplicate
160+
# Already queued from the snapshot - skip duplicate
161161
continue
162162
seen_keys.add(event.file.key)
163163
file_queue.put_nowait(event.file)
@@ -174,7 +174,7 @@ async def _listen_events() -> None:
174174
except WebSocketDisconnect, asyncio.CancelledError:
175175
pass
176176

177-
# ── streamer — pulls from queue and streams one file at a time ───
177+
# pulls from queue and streams one file at a time
178178
async def _stream_queued_files() -> None:
179179
try:
180180
while True:
@@ -189,7 +189,7 @@ async def _stream_queued_files() -> None:
189189
stream_task = asyncio.create_task(_stream_queued_files())
190190

191191
try:
192-
# Keep alive read (and discard) client pings / messages
192+
# Keep alive - read (and discard) client pings / messages
193193
while True:
194194
await ws.receive_text()
195195
except WebSocketDisconnect:

src/backend/app/routes/ws/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async def state_ws(ws: WebSocket):
1515
current: AppState = await AppState.get()
1616
await ws.send_text(current.model_dump_json())
1717

18-
# Keep the connection alive read (and discard) client pings/messages
18+
# Keep the connection alive - read (and discard) client pings/messages
1919
while True:
2020
await ws.receive_text()
2121
except WebSocketDisconnect:

src/backend/app/schemas/reverse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RoomOut(BaseModel):
2121

2222

2323
class RoomCreateOut(RoomOut):
24-
"""Returned only once at room creation includes the host secret."""
24+
"""Returned only once at room creation - includes the host secret."""
2525

2626
host_token: str
2727

src/backend/app/states/room.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ async def add_file(cls, room_id: str, entry: RoomFileEntry) -> bool:
164164
# Key doesn't exist (room expired) or path missing
165165
return False
166166

167-
# Fan-out notification every subscribed instance picks this up
167+
# Fan-out notification - every subscribed instance picks this up
168168
event = RoomFileEvent(event="file_added", file=entry)
169169
await cls._publish(_room_channel(room_id), event.model_dump_json())
170170
return True

0 commit comments

Comments
 (0)