Skip to content

Commit a6df118

Browse files
committed
refac
1 parent bf00a38 commit a6df118

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,21 @@ Output streams as JSONL:
6565

6666
**From URL:**
6767
```bash
68-
curl -X POST "http://localhost:8000/files/upload?url=https://example.com/data.csv&path=/tmp/data.csv" \
68+
curl -X POST "http://localhost:8000/files/upload?url=https://example.com/data.csv&dir=/tmp" \
6969
-H "Authorization: Bearer <api-key>"
7070
```
7171

7272
**Direct upload:**
7373
```bash
74-
curl -X POST "http://localhost:8000/files/upload?path=/tmp/data.csv" \
74+
curl -X POST "http://localhost:8000/files/upload?dir=/tmp" \
7575
-H "Authorization: Bearer <api-key>" \
7676
-F "file=@local_file.csv"
7777
```
7878

7979
**Via temporary link (no auth needed to upload):**
8080
```bash
8181
# 1. Generate an upload link
82-
curl -X POST "http://localhost:8000/files/upload/link?path=/tmp/data.csv" \
82+
curl -X POST "http://localhost:8000/files/upload/link?dir=/tmp" \
8383
-H "Authorization: Bearer <api-key>"
8484
# → {"url": "http://localhost:8000/files/upload/a1b2c3d4..."}
8585

@@ -88,6 +88,8 @@ curl -X POST "http://localhost:8000/files/upload/a1b2c3d4..." \
8888
-F "file=@local_file.csv"
8989
```
9090

91+
Filename is automatically derived from the uploaded file or URL.
92+
9193
### Download a File
9294

9395
```bash

open_terminal/main.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,27 @@ async def download_file(token: str):
132132
},
133133
)
134134
async def upload_file(
135-
path: str = Query(..., description="Absolute destination path for the file."),
135+
dir: str = Query(..., description="Destination directory for the file."),
136136
url: Optional[str] = Query(None, description="URL to download the file from. If omitted, expects a multipart file upload."),
137137
file: Optional[UploadFile] = File(None, description="The file to upload (if no URL provided)."),
138138
):
139139
if url:
140140
import httpx
141+
from urllib.parse import urlparse
141142

142143
async with httpx.AsyncClient(follow_redirects=True) as client:
143144
resp = await client.get(url)
144145
resp.raise_for_status()
145146
content = resp.content
147+
filename = os.path.basename(urlparse(url).path) or "download"
146148
elif file:
147149
content = await file.read()
150+
filename = file.filename or "upload"
148151
else:
149152
raise HTTPException(status_code=400, detail="Provide either 'url' or a file upload.")
150153

151-
os.makedirs(os.path.dirname(path), exist_ok=True)
154+
os.makedirs(dir, exist_ok=True)
155+
path = os.path.join(dir, filename)
152156
with open(path, "wb") as f:
153157
f.write(content)
154158
return {"path": path, "size": len(content)}
@@ -164,14 +168,14 @@ async def upload_file(
164168
},
165169
)
166170
async def create_upload_link(
167-
path: str = Query(..., description="Absolute destination path for the uploaded file."),
171+
dir: str = Query(..., description="Destination directory for the uploaded file."),
168172
request: Request = None,
169173
):
170174
import time
171175
import uuid
172176

173177
token = uuid.uuid4().hex
174-
_upload_links[token] = (path, time.time() + 300)
178+
_upload_links[token] = (dir, time.time() + 300)
175179

176180
base_url = str(request.base_url).rstrip("/")
177181
return {"url": f"{base_url}/files/upload/{token}"}
@@ -211,11 +215,13 @@ async def upload_file_via_link(
211215
if not entry:
212216
raise HTTPException(status_code=404, detail="Invalid or expired upload link")
213217

214-
path, expiry = entry
218+
dir, expiry = entry
215219
if time.time() > expiry:
216220
raise HTTPException(status_code=404, detail="Upload link expired")
217221

218-
os.makedirs(os.path.dirname(path), exist_ok=True)
222+
filename = file.filename or "upload"
223+
os.makedirs(dir, exist_ok=True)
224+
path = os.path.join(dir, filename)
219225
content = await file.read()
220226
with open(path, "wb") as f:
221227
f.write(content)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "open-terminal"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
description = "A minimal terminal interaction API"
55
readme = "README.md"
66
authors = [

0 commit comments

Comments
 (0)