diff --git a/README.md b/README.md index 59c42cb..6bf45f6 100644 --- a/README.md +++ b/README.md @@ -76,10 +76,22 @@ curl -X POST "http://localhost:8000/files/upload?path=/tmp/data.csv" \ -F "file=@local_file.csv" ``` +**Via temporary link (no auth needed to upload):** +```bash +# 1. Generate an upload link +curl -X POST "http://localhost:8000/files/upload/link?path=/tmp/data.csv" \ + -H "Authorization: Bearer " +# → {"url": "http://localhost:8000/files/upload/a1b2c3d4..."} + +# 2. Upload to the link (no auth required) +curl -X POST "http://localhost:8000/files/upload/a1b2c3d4..." \ + -F "file=@local_file.csv" +``` + ### Download a File ```bash -curl "http://localhost:8000/files/download?path=/tmp/output.csv" \ +curl "http://localhost:8000/files/download/link?path=/tmp/output.csv" \ -H "Authorization: Bearer " ``` diff --git a/open_terminal/main.py b/open_terminal/main.py index 31fe189..f2793bf 100644 --- a/open_terminal/main.py +++ b/open_terminal/main.py @@ -5,7 +5,7 @@ from typing import Optional from fastapi import Depends, FastAPI, File, HTTPException, Query, Request, UploadFile from fastapi.middleware.cors import CORSMiddleware -from fastapi.responses import FileResponse, StreamingResponse +from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer from pydantic import BaseModel, Field @@ -69,12 +69,13 @@ async def health(): -# Temporary download links: {token: (path, expiry_timestamp)} +# Temporary links: {token: (path, expiry_timestamp)} _download_links: dict[str, tuple[str, float]] = {} +_upload_links: dict[str, tuple[str, float]] = {} @app.get( - "/files/download", + "/files/download/link", summary="Get a file download link", description="Returns a temporary download URL for a file. Link expires after 5 minutes and requires no authentication to use.", dependencies=[Depends(verify_api_key)], @@ -102,11 +103,7 @@ async def get_file_link( @app.get( "/files/download/{token}", - summary="Download via link", - description="Download a file using a temporary token. No authentication required.", - responses={ - 404: {"description": "Invalid or expired download link."}, - }, + include_in_schema=False, ) async def download_file(token: str): import time @@ -157,6 +154,74 @@ async def upload_file( return {"path": path, "size": len(content)} +@app.post( + "/files/upload/link", + summary="Create an upload link", + description="Generate a temporary, unauthenticated upload URL. Link expires after 5 minutes.", + dependencies=[Depends(verify_api_key)], + responses={ + 401: {"description": "Invalid or missing API key."}, + }, +) +async def create_upload_link( + path: str = Query(..., description="Absolute destination path for the uploaded file."), + request: Request = None, +): + import time + import uuid + + token = uuid.uuid4().hex + _upload_links[token] = (path, time.time() + 300) + + base_url = str(request.base_url).rstrip("/") + return {"url": f"{base_url}/files/upload/{token}"} + + +@app.get( + "/files/upload/{token}", + response_class=HTMLResponse, + include_in_schema=False, +) +async def upload_page(token: str): + import time + + entry = _upload_links.get(token) + if not entry or time.time() > entry[1]: + return HTMLResponse("Link expired.", status_code=404) + + return HTMLResponse( + '
' + ' ' + '' + '
' + ) + + +@app.post( + "/files/upload/{token}", + include_in_schema=False, +) +async def upload_file_via_link( + token: str, + file: UploadFile = File(..., description="The file to upload."), +): + import time + + entry = _upload_links.pop(token, None) + if not entry: + raise HTTPException(status_code=404, detail="Invalid or expired upload link") + + path, expiry = entry + if time.time() > expiry: + raise HTTPException(status_code=404, detail="Upload link expired") + + os.makedirs(os.path.dirname(path), exist_ok=True) + content = await file.read() + with open(path, "wb") as f: + f.write(content) + return {"path": path, "size": len(content)} + + @app.post( "/execute", summary="Execute a command", diff --git a/pyproject.toml b/pyproject.toml index c631a49..326d8af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "open-terminal" -version = "0.1.5" -description = "A barebones terminal interaction API" +version = "0.1.7" +description = "A minimal terminal interaction API" readme = "README.md" authors = [ { name = "Timothy Jaeryang Baek", email = "tim@openwebui.com" }