♻ Minor refactoring/fixing prior to merge of #3497

* url_for in preload instead of (potentially wrong due to prefixes)
    absolute URLs to /static/...
  * error resilience in case of any issues during .gz generation
  * bug fix in mime type detection
  * code formatting & naming here and there
This commit is contained in:
Gina Häußge committed 2020-04-07 12:45:25 +02:00
1 parent 99072cd63d
commit fde070665d
3 files changed
+30 -16

No files matched your search

+14 -6
View File
@@ -949,8 +949,12 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin,
called with the response handler as parameter. May return ``None`` to prevent the ETag response header
from being set. If not provided the last modified time of the file in question will be used as returned
by ``get_content_version``.
is_pre_compressed (bool): if the file is expected to be pre-compressed, i.e, if there is a file in the same
directory with the same name, but with '.gz' appended and gzip-encoded
name_generator (function): Callback to call for generating the value of the attachment file name header. Will be
called with the requested path as parameter.
mime_type_guesser (function): Callback to guess the mime type to use for the content type encoding of the
response. Will be called with the requested path on disk as parameter.
is_pre_compressed (bool): if the file is expected to be pre-compressed, i.e, if there is a file in the same
directory with the same name, but with '.gz' appended and gzip-encoded
"""
def initialize(self, path, default_filename=None, as_attachment=False, allow_client_caching=True,
@@ -967,8 +971,8 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin,
self._is_pre_compressed = is_pre_compressed
def should_use_precompressed(self):
return (self._is_pre_compressed and
"gzip" in self.request.headers.get("Accept-Encoding"))
return (self._is_pre_compressed
and "gzip" in self.request.headers.get("Accept-Encoding"))
def get(self, path, include_body=True):
if self._access_validation is not None:
@@ -979,7 +983,7 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin,
if "cookie" in self.request.arguments:
self.set_cookie(self.request.arguments["cookie"][0], "true", path="/")
if self.should_use_precompressed():
if self.should_use_precompressed() and os.path.exists(os.path.join(self.root, path + ".gz")):
self.set_header("Content-Encoding", "gzip")
path = path + ".gz"
@@ -1001,6 +1005,7 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin,
self.set_header("Cache-Control", "max-age=0, must-revalidate, private")
self.set_header("Expires", "-1")
@property
def original_absolute_path(self):
"""The path of the uncompressed file corresponding to the compressed file"""
if self._is_pre_compressed:
@@ -1013,6 +1018,7 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin,
else:
return self.get_content_version(self.absolute_path)
# noinspection PyAttributeOutsideInit
def get_content_type(self):
if self._mime_type_guesser is not None:
type = self._mime_type_guesser(self.original_absolute_path)
@@ -1021,11 +1027,13 @@ class LargeResponseHandler(RequestlessExceptionLoggingMixin,
correct_absolute_path = None
try:
# reset self.absolute_path temporarily
if self.should_use_precompressed():
correct_absolute_path = self.absolute_path
self.absolute_path = self.original_absolute_path()
self.absolute_path = self.original_absolute_path
return tornado.web.StaticFileHandler.get_content_type(self)
finally:
# restore self.absolute_path
if self.should_use_precompressed() and correct_absolute_path is not None:
self.absolute_path = correct_absolute_path
+12 -7
View File
@@ -5,8 +5,8 @@ __author__ = "Gina Häußge <gina@octoprint.org>"
__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
__copyright__ = "Copyright (C) 2017 The OctoPrint Project - Released under terms of the AGPLv3 License"
import logging
import gzip
import os
import re
try:
@@ -118,12 +118,17 @@ class GzipFile(Filter):
# webassets requires us to output a "str", but we can't do that since gzip
# provides binary outputs.
#
# We work around that by outputting the gziped file to another path
if kwargs.get("output_path", None):
output_path = kwargs["output_path"]
gziped_output_path = output_path + ".gz"
with open(gziped_output_path, "wb") as gziped_output:
gziped_output.write(gzip.compress(data.encode("utf8"), compresslevel=9))
# We work around that by outputting the gzipped file to another path
output_path = kwargs.get("output_path", None)
if output_path:
gzipped_output_path = output_path + ".gz"
try:
with open(gzipped_output_path, "wb") as gzipped_output:
gzipped_output.write(gzip.compress(data.encode("utf8"), compresslevel=9))
except Exception:
logging.getLogger(__name__).exception("Error writing gzipped "
"output of {} to {}".format(output_path,
gzipped_output_path))
class ChainedHunk(BaseHunk):
@@ -4,6 +4,7 @@
{% assets "css_plugins" %}<link rel="preload" as="style" href="{{ ASSET_URL }}">{% endassets %}
{% assets "less_core" %}<link rel="preload" as="style" href="{{ ASSET_URL }}">{% endassets %}
{% assets "less_plugins" %}<link rel="preload" as="style" href="{{ ASSET_URL }}">{% endassets %}
<link rel="preload" as="font" href="/static/vendor/font-awesome-4.7.0/fonts/fontawesome-webfont.woff2?v=4.7.0" type="font/woff2" crossorigin="anonymous">
<link rel="preload" as="font" href="/static/vendor/font-awesome-4.7.0/fonts/fontawesome-webfont.woff?v=4.7.0" type="font/woff" crossorigin="anonymous">
<link rel="preload" as="font" href="/static/vendor/font-awesome-3.2.1/fonts/fontawesome-webfont.woff?v=3.2.1" type="font/woff" crossorigin="anonymous">
<link rel="preload" as="font" href="{{ url_for('static', filename='vendor/font-awesome-4.7.0/fonts/fontawesome-webfont.woff2?v=4.7.0') }}" type="font/woff2" crossorigin="anonymous">
<link rel="preload" as="font" href="{{ url_for('static', filename='vendor/font-awesome-4.7.0/fonts/fontawesome-webfont.woff?v=4.7.0') }}" type="font/woff" crossorigin="anonymous">
<link rel="preload" as="font" href="{{ url_for('static', filename='vendor/font-awesome-3.2.1/fonts/fontawesome-webfont.woff?v=3.2.1') }}" type="font/woff" crossorigin="anonymous">