Skip to content

Commit a5afb18

Browse files
committed
refac
1 parent 3103f34 commit a5afb18

1 file changed

Lines changed: 59 additions & 2 deletions

File tree

open_terminal/main.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import asyncio
22
import json
3+
import os
34
from typing import Optional
45

5-
from fastapi import Depends, FastAPI, HTTPException, Query
6+
from fastapi import Depends, FastAPI, HTTPException, Query, Request
67
from fastapi.middleware.cors import CORSMiddleware
7-
from fastapi.responses import StreamingResponse
8+
from fastapi.responses import FileResponse, StreamingResponse
89
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
910
from pydantic import BaseModel, Field
1011

@@ -68,6 +69,62 @@ async def health():
6869
return {"status": "ok"}
6970

7071

72+
73+
# Temporary download links: {token: (path, expiry_timestamp)}
74+
_download_links: dict[str, tuple[str, float]] = {}
75+
76+
77+
@app.get(
78+
"/files",
79+
summary="Get a file download link",
80+
description="Returns a temporary download URL for a file. Link expires after 5 minutes and requires no authentication to use.",
81+
responses={
82+
404: {"description": "File not found."},
83+
401: {"description": "Invalid or missing API key."},
84+
},
85+
)
86+
async def get_file_link(
87+
path: str = Query(..., description="Absolute path to the file."),
88+
request: Request = None,
89+
):
90+
import time
91+
import uuid
92+
93+
if not os.path.isfile(path):
94+
raise HTTPException(status_code=404, detail="File not found")
95+
96+
token = uuid.uuid4().hex
97+
_download_links[token] = (path, time.time() + 300)
98+
99+
base_url = str(request.base_url).rstrip("/")
100+
return {"url": f"{base_url}/files/download/{token}"}
101+
102+
103+
@app.get(
104+
"/files/download/{token}",
105+
summary="Download via link",
106+
description="Download a file using a temporary token. No authentication required.",
107+
responses={
108+
404: {"description": "Invalid or expired download link."},
109+
},
110+
)
111+
async def download_file(token: str):
112+
import time
113+
114+
entry = _download_links.pop(token, None)
115+
if not entry:
116+
raise HTTPException(status_code=404, detail="Invalid or expired download link")
117+
118+
path, expiry = entry
119+
if time.time() > expiry:
120+
raise HTTPException(status_code=404, detail="Download link expired")
121+
122+
if not os.path.isfile(path):
123+
raise HTTPException(status_code=404, detail="File not found")
124+
125+
return FileResponse(path)
126+
127+
71128
@app.post(
72129
"/execute",
73130
summary="Execute a command",

0 commit comments

Comments
 (0)