From 9df942c7c49a4a486e2426a0121b90373ca2dfe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 26 Jan 2021 15:53:25 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20compression=20on=20zip=20?= =?UTF-8?q?handlers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/server/util/tornado.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/octoprint/server/util/tornado.py b/src/octoprint/server/util/tornado.py index 109a99b7e..a113e92aa 100644 --- a/src/octoprint/server/util/tornado.py +++ b/src/octoprint/server/util/tornado.py @@ -1404,7 +1404,12 @@ class DeprecatedEndpointHandler(CorsSupportMixin, tornado.web.RequestHandler): class StaticZipBundleHandler(CorsSupportMixin, tornado.web.RequestHandler): def initialize( - self, files=None, as_attachment=True, attachment_name=None, access_validation=None + self, + files=None, + as_attachment=True, + attachment_name=None, + access_validation=None, + compress=False, ): if files is None: files = [] @@ -1415,6 +1420,7 @@ class StaticZipBundleHandler(CorsSupportMixin, tornado.web.RequestHandler): self._as_attachment = as_attachment self._attachment_name = attachment_name self._access_validator = access_validation + self._compress = compress def get(self, *args, **kwargs): if self._access_validator is not None: @@ -1445,6 +1451,16 @@ class StaticZipBundleHandler(CorsSupportMixin, tornado.web.RequestHandler): import zipstream + compress_type = zipstream.ZIP_STORED + if self._compress: + try: + import zlib # noqa: F401 + + compress_type = zipstream.ZIP_DEFLATED + except ImportError: + # no zlib, no compression + pass + z = zipstream.ZipFile() for f in self.normalize_files(files): # noinspection PyCompatibility @@ -1454,11 +1470,11 @@ class StaticZipBundleHandler(CorsSupportMixin, tornado.web.RequestHandler): content = f.get("content") if path: - z.write(path, arcname=name, compress_type=zipstream.ZIP_STORED) + z.write(path, arcname=name, compress_type=compress_type) elif iter and name: - z.write_iter(name, iter, compress_type=zipstream.ZIP_STORED) + z.write_iter(name, iter, compress_type=compress_type) elif content and name: - z.writestr(name, content, compress_type=zipstream.ZIP_STORED) + z.writestr(name, content, compress_type=compress_type) for chunk in z: try: @@ -1477,6 +1493,7 @@ class DynamicZipBundleHandler(StaticZipBundleHandler): as_attachment=True, attachment_name=None, access_validation=None, + compress=False, ): if as_attachment and not attachment_name: raise ValueError("attachment name must be set if as_attachment is True") @@ -1486,6 +1503,7 @@ class DynamicZipBundleHandler(StaticZipBundleHandler): self._as_attachment = as_attachment self._attachment_name = attachment_name self._access_validator = access_validation + self._compress = compress def get(self, *args, **kwargs): if self._access_validator is not None: @@ -1539,6 +1557,7 @@ class SystemInfoBundleHandler(StaticZipBundleHandler): self._as_attachment = True self._attachment_name = "octoprint-systeminfo-{datetime}.zip" self._access_validator = access_validation + self._compress = True def get(self, *args, **kwargs): if self._access_validator is not None: