# #
###############################################################################
-import asyncio
import fastapi
from . import app
if not file:
raise fastapi.HTTPException(404, "Could not find debuginfo in %s" % package)
- # Create a helper function that will stream the file chunk by chunk
- async def stream():
- f = await file.open()
-
- while True:
- chunk = await asyncio.to_thread(f.read, 128 * 1024)
- if not chunk:
- break
-
- yield chunk
-
# Stream the payload
return fastapi.responses.StreamingResponse(
- stream(), media_type="application/octet-stream")
+ file.stream(), media_type="application/octet-stream")
# #
###############################################################################
-import asyncio
import fastapi
import pydantic
return [file async for file in await package.get_files()]
+# XXX This endpoint need some ratelimiting applied
+
@app.get("/packages/{uuid:uuid}/download/{path:path}", include_in_schema=False)
async def download_file(
path: str,
"X-Robots-Tag" : "noindex",
}
- # Create a helper function that will stream the file chunk by chunk
- async def stream():
- f = await file.open()
-
- while True:
- chunk = await asyncio.to_thread(f.read, 128 * 1024)
- if not chunk:
- break
-
- yield chunk
-
return fastapi.responses.StreamingResponse(
- stream(), media_type=file.mimetype, headers=headers,
+ file.stream(), media_type=file.mimetype, headers=headers,
)
# Read the payload in a separate thread
return await asyncio.to_thread(func)
+ async def stream(self, chunk_size=128 * 1024):
+ """
+ A helper function to stream the payload chunk by chunk
+ """
+ # Open the file
+ f = await self.open()
+
+ # Read a chunk of up to chunk size bytes and return it
+ # until we have read everything.
+ while True:
+ chunk = await asyncio.to_thread(f.read, chunk_size)
+ if not chunk:
+ break
+
+ yield chunk
+
async def sendfile(self, dst, chunk_size=65536):
"""
Sends the payload of the file into the given file descriptor