From: Michael Tremer Date: Fri, 10 Mar 2023 16:54:11 +0000 (+0000) Subject: packages: Fix viewing and downloading files X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f9eefdb27f6ebc1a31ed8539166414069d7aae3;p=pbs.git packages: Fix viewing and downloading files Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/packages.py b/src/buildservice/packages.py index 19e89694..95f66f64 100644 --- a/src/buildservice/packages.py +++ b/src/buildservice/packages.py @@ -635,12 +635,10 @@ class File(base.Object): return type or "application/octet-stream" - # Payload + # Send Payload - async def get_payload(self): - """ - Fetches and returns the payload - """ + @property + async def payload(self): # Open the package p = await self.package.open() @@ -649,3 +647,18 @@ class File(base.Object): # Read the payload in a separate thread return await asyncio.to_thread(func) + + async def sendfile(self, f, chunk_size=102400): + """ + Sends the payload of the file into the given file descriptor + """ + # XXX This should ideally not load the entire payload into memory at once + + # Fetch the payload + payload = await self.payload + + # Send the payload in chunks, so that we do not overwhelm Tornado + for i in range(0, self.size, chunk_size): + block = payload[i:i+chunk_size] + + f.write(block) diff --git a/src/web/__init__.py b/src/web/__init__.py index d2095ec8..005c0c49 100644 --- a/src/web/__init__.py +++ b/src/web/__init__.py @@ -115,9 +115,9 @@ class Application(tornado.web.Application): (r"/packages", packages.IndexHandler), (r"/packages/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})", packages.ShowHandler), (r"/packages/([\w\-\+]+)", packages.NameHandler), - (r"/package/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})/download(.*)", + (r"/packages/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})/download(.*)", packages.FileDownloadHandler), - (r"/package/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})/view(.*)", + (r"/packages/([\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12})/view(.*)", packages.FileViewHandler), # Builds diff --git a/src/web/packages.py b/src/web/packages.py index a0f4ab35..b828f73b 100644 --- a/src/web/packages.py +++ b/src/web/packages.py @@ -69,19 +69,13 @@ class FileDownloadHandler(base.BaseHandler): # Send the filename self.set_header("Content-Disposition", - "attachment; filename=%s" % os.path.basename(file.name)) + "attachment; filename=%s" % os.path.basename(file.path)) # These pages should not be indexed self.add_header("X-Robots-Tag", "noindex") - # XXX this should probably not be done in one large operation. - # Instead we should send the file chunk by chunk. - - # Fetch the payload - payload = await file.get_payload() - - # Send the payload to the client - self.finish(payload) + # Send the payload + await file.sendfile(self) class FileViewHandler(base.BaseHandler): @@ -102,10 +96,8 @@ class FileViewHandler(base.BaseHandler): # These pages should not be indexed self.add_header("X-Robots-Tag", "noindex") - # Fetch payload - payload = await file.get_payload() - - self.render("packages/view-file.html", package=package, file=file, payload=payload) + self.render("packages/view-file.html", package=package, + file=file, payload=await file.payload) class DependenciesModule(ui_modules.UIModule):