From: Michael Tremer Date: Thu, 10 Jul 2025 12:03:50 +0000 (+0000) Subject: packages: Create a convenience function to send file headers X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=12530eba44e8083cb381eb66801882dd4095c51c;p=pbs.git packages: Create a convenience function to send file headers Signed-off-by: Michael Tremer --- diff --git a/src/api/packages.py b/src/api/packages.py index 95d409d3..6f8f0671 100644 --- a/src/api/packages.py +++ b/src/api/packages.py @@ -111,20 +111,7 @@ async def download_file( # XXX Check if this is actually downloadable - headers = { - "Content-Type" : file.mimetype or "application/octet-stream", - "Content-Disposition" : "attachment; filename=%s" % file.basename, - - # XXX StreamingResponse does not allow us to set a Content-Length header - # because starlette is getting very confused about how much data is to be - # sent or has been sent already. - #"Content-Length" : "%s" % file.size, - - # This content should not be indexed - "X-Robots-Tag" : "noindex", - } - - return fastapi.responses.StreamingResponse(file.stream(), headers=headers) + return fastapi.responses.StreamingResponse(file.stream(), headers=file.headers) # Add everything to the APIv1 diff --git a/src/buildservice/packages.py b/src/buildservice/packages.py index cf4c9cbb..ec8a6b64 100644 --- a/src/buildservice/packages.py +++ b/src/buildservice/packages.py @@ -796,32 +796,20 @@ class File(sqlmodel.SQLModel, table=True): yield chunk - async def sendfile(self, dst, chunk_size=65536): - """ - Sends the payload of the file into the given file descriptor - """ - src = await self.open() - - return await asyncio.to_thread(self._sendfile, src, dst, chunk_size=chunk_size) - - def _sendfile(self, src, dst, chunk_size=65536): - while True: - chunk = src.read(chunk_size) - if not chunk: - break - - dst.write(chunk) - @property - async def payload(self): - """ - Returns the entire payload at once - """ - # Open the file - f = await self.open() - - # Read everything in a separate thread - return await asyncio.to_thread(f.readall) + def headers(self): + return { + "Content-Type" : self.mimetype or "application/octet-stream", + "Content-Disposition" : "attachment; filename=%s" % self.basename, + + # XXX StreamingResponse does not allow us to set a Content-Length header + # because starlette is getting very confused about how much data is to be + # sent or has been sent already. + #"Content-Length" : "%s" % self.size, + + # This content should not be indexed + "X-Robots-Tag" : "noindex", + } def buildid_to_path(buildid):