forked from cyclotruc/gitingest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload.py
29 lines (24 loc) · 941 Bytes
/
download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from fastapi import HTTPException, APIRouter
from fastapi.responses import Response
from config import TMP_BASE_PATH
import os
router = APIRouter()
@router.get("/download/{digest_id}")
async def download_ingest(digest_id: str):
try:
# Find the first .txt file in the directory
directory = f"{TMP_BASE_PATH}/{digest_id}"
txt_files = [f for f in os.listdir(directory) if f.endswith('.txt')]
if not txt_files:
raise FileNotFoundError("No .txt file found")
with open(f"{directory}/{txt_files[0]}", "r") as f:
content = f.read()
return Response(
content=content,
media_type="text/plain",
headers={
"Content-Disposition": f"attachment; filename={txt_files[0]}"
}
)
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Digest not found")