|
| 1 | +From 585771ca61fd6da449cce31425c2ffab322b0dd8 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Dylan McCall <dylan@endlessos.org> |
| 3 | +Date: Thu, 4 May 2023 17:52:48 -0700 |
| 4 | +Subject: [PATCH] Break up DynamicWhiteNoise code |
| 5 | + |
| 6 | +Instead of generating everything in the initializer, we will use some |
| 7 | +more specific functions. This makes it easier to create a subclass of |
| 8 | +DynamicWhiteNoise, for example to support platforms that require special |
| 9 | +libraries for file access. |
| 10 | +--- |
| 11 | + kolibri/utils/kolibri_whitenoise.py | 61 ++++++++++++++++++++++------- |
| 12 | + 1 file changed, 46 insertions(+), 15 deletions(-) |
| 13 | + |
| 14 | +diff --git a/kolibri/utils/kolibri_whitenoise.py b/kolibri/utils/kolibri_whitenoise.py |
| 15 | +index 828324cf17..4acf1dab21 100644 |
| 16 | +--- a/kolibri/utils/kolibri_whitenoise.py |
| 17 | ++++ b/kolibri/utils/kolibri_whitenoise.py |
| 18 | +@@ -8,6 +8,7 @@ from wsgiref.headers import Headers |
| 19 | + from django.contrib.staticfiles import finders |
| 20 | + from django.core.files.storage import FileSystemStorage |
| 21 | + from django.utils._os import safe_join |
| 22 | ++from django.utils.functional import cached_property |
| 23 | + from six.moves.urllib.parse import parse_qs |
| 24 | + from six.moves.urllib.parse import urljoin |
| 25 | + from whitenoise import WhiteNoise |
| 26 | +@@ -206,33 +207,57 @@ class DynamicWhiteNoise(WhiteNoise): |
| 27 | + } |
| 28 | + kwargs.update(whitenoise_settings) |
| 29 | + super(DynamicWhiteNoise, self).__init__(application, **kwargs) |
| 30 | +- self.dynamic_finder = FileFinder(dynamic_locations or []) |
| 31 | ++ self._dynamic_locations = dynamic_locations |
| 32 | ++ self._writable_locations = writable_locations |
| 33 | ++ self._app_paths = app_paths |
| 34 | ++ |
| 35 | ++ if static_prefix is not None and not static_prefix.endswith("/"): |
| 36 | ++ raise ValueError("Static prefix must end in '/'") |
| 37 | ++ self.static_prefix = static_prefix |
| 38 | ++ |
| 39 | ++ @cached_property |
| 40 | ++ def app_path_check(self): |
| 41 | ++ # Generate a regex to check if a path patches one of our app paths |
| 42 | ++ return ( |
| 43 | ++ re.compile("^({})".format("|".join(self._app_paths))) |
| 44 | ++ if self._app_paths |
| 45 | ++ else None |
| 46 | ++ ) |
| 47 | ++ |
| 48 | ++ @cached_property |
| 49 | ++ def dynamic_finder(self): |
| 50 | ++ return FileFinder(self._dynamic_locations or []) |
| 51 | ++ |
| 52 | ++ @cached_property |
| 53 | ++ def dynamic_check(self): |
| 54 | + # Generate a regex to check if a path matches one of our dynamic |
| 55 | + # location prefixes |
| 56 | +- self.dynamic_check = ( |
| 57 | ++ return ( |
| 58 | + re.compile("^({})".format("|".join(self.dynamic_finder.prefixes))) |
| 59 | + if self.dynamic_finder.prefixes |
| 60 | + else None |
| 61 | + ) |
| 62 | +- self.writable_locations = {} |
| 63 | +- if dynamic_locations: |
| 64 | +- for index in writable_locations: |
| 65 | ++ |
| 66 | ++ @cached_property |
| 67 | ++ def writable_locations(self): |
| 68 | ++ result = {} |
| 69 | ++ if self._dynamic_locations: |
| 70 | ++ for index in self._writable_locations: |
| 71 | + try: |
| 72 | + prefix, root = self.dynamic_finder.locations[index] |
| 73 | +- self.writable_locations[prefix] = root |
| 74 | ++ result[prefix] = root |
| 75 | + except IndexError: |
| 76 | + pass |
| 77 | +- self.writable_check = ( |
| 78 | ++ return result |
| 79 | ++ |
| 80 | ++ @cached_property |
| 81 | ++ def writable_check(self): |
| 82 | ++ # Generate a regex to check if a path matches a writable location |
| 83 | ++ return ( |
| 84 | + re.compile("^({})".format("|".join(self.writable_locations.keys()))) |
| 85 | + if self.writable_locations |
| 86 | + else None |
| 87 | + ) |
| 88 | +- self.app_path_check = ( |
| 89 | +- re.compile("^({})".format("|".join(app_paths))) if app_paths else None |
| 90 | +- ) |
| 91 | +- if static_prefix is not None and not static_prefix.endswith("/"): |
| 92 | +- raise ValueError("Static prefix must end in '/'") |
| 93 | +- self.static_prefix = static_prefix |
| 94 | + |
| 95 | + def __call__(self, environ, start_response): |
| 96 | + path = decode_path_info(environ.get("PATH_INFO", "")) |
| 97 | +@@ -308,13 +333,16 @@ class DynamicWhiteNoise(WhiteNoise): |
| 98 | + headers["Access-Control-Allow-Origin"] = "*" |
| 99 | + if self.add_headers_function: |
| 100 | + self.add_headers_function(headers, path, url) |
| 101 | +- return EndRangeStaticFile( |
| 102 | ++ return self._create_end_range_static_file( |
| 103 | + path, |
| 104 | + headers.items(), |
| 105 | + stat_cache=stat_cache, |
| 106 | + encodings={"gzip": path + ".gz", "br": path + ".br"}, |
| 107 | + ) |
| 108 | + |
| 109 | ++ def _create_end_range_static_file(self, path, headers, **kwargs): |
| 110 | ++ return EndRangeStaticFile(path, headers, **kwargs) |
| 111 | ++ |
| 112 | + def get_streaming_static_file(self, url, remote_baseurl): |
| 113 | + """ |
| 114 | + Vendor this function from source to substitute in our |
| 115 | +@@ -334,8 +362,11 @@ class DynamicWhiteNoise(WhiteNoise): |
| 116 | + if self.add_headers_function: |
| 117 | + self.add_headers_function(headers, path, url) |
| 118 | + headers["Content-Encoding"] = "" |
| 119 | +- return StreamingStaticFile( |
| 120 | ++ return self._create_streaming_range_static_file( |
| 121 | + os.path.join(local_dir, path), |
| 122 | + headers, |
| 123 | + urljoin(remote_baseurl, url.lstrip("/")), |
| 124 | + ) |
| 125 | ++ |
| 126 | ++ def _create_streaming_range_static_file(self, path, headers, remote_url, **kwargs): |
| 127 | ++ return StreamingStaticFile(path, headers, remote_url, **kwargs) |
| 128 | +-- |
| 129 | +2.40.1 |
| 130 | + |
0 commit comments