]> git.ipfire.org Git - pbs.git/commitdiff
packages: Create a convenience function to send file headers
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 10 Jul 2025 12:03:50 +0000 (12:03 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 10 Jul 2025 12:03:50 +0000 (12:03 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/api/packages.py
src/buildservice/packages.py

index 95d409d369685d5202969d5799c4c007563d65d1..6f8f06714a2e3b4aac7299becdb7ec5249fa887b 100644 (file)
@@ -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
index cf4c9cbbc4a7e69802afcca34c0a53678c0f3e0e..ec8a6b6467e423435263293d3e71091fb98c0e3d 100644 (file)
@@ -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):