|
19 | 19 | # You should have received a copy of the GNU Affero General Public License |
20 | 20 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
21 | 21 |
|
| 22 | +import hashlib |
22 | 23 | import logging |
| 24 | +import importlib.resources |
23 | 25 |
|
24 | 26 | import collections |
25 | 27 | try: |
|
36 | 38 |
|
37 | 39 | from cms.db.filecacher import FileCacher |
38 | 40 | from cms.server.file_middleware import FileServerMiddleware |
| 41 | +from cms.server.util import Url |
39 | 42 | from .service import Service |
40 | 43 | from .web_rpc import RPCMiddleware |
41 | 44 |
|
|
45 | 48 |
|
46 | 49 | SECONDS_IN_A_YEAR = 365 * 24 * 60 * 60 |
47 | 50 |
|
| 51 | +class StaticFileHasher: |
| 52 | + """ |
| 53 | + Constructs URLs to static files. The result of make() is similar to the |
| 54 | + url() function that's used in the templates, in that it constructs a |
| 55 | + relative URL, but it also adds a "?h=12345678" query parameter which forces |
| 56 | + browsers to reload the resource when it has changed. |
| 57 | + """ |
| 58 | + def __init__(self, files: list[tuple[str, str]]): |
| 59 | + """ |
| 60 | + Initialize. |
| 61 | +
|
| 62 | + files: list of static file locations, each in the format that would be |
| 63 | + passed to SharedDataMiddleware. |
| 64 | + """ |
| 65 | + # Cache of the hashes of files, to prevent re-hashing them on every request. |
| 66 | + self.cache: dict[tuple[str, ...], str] = {} |
| 67 | + # We reverse the order, because in WSGI later-added middlewares |
| 68 | + # override earlier ones, but here we iterate the locations and use the |
| 69 | + # first found match. |
| 70 | + self.static_locations = files[::-1] |
| 71 | + |
| 72 | + def make(self, base_url: Url): |
| 73 | + """ |
| 74 | + Create a new url helper function (called once per request). |
| 75 | +
|
| 76 | + The returned function takes arguments in the same format as `Url`, and |
| 77 | + returns a string in the same format as `Url` except with a hash |
| 78 | + appended as a query string. |
| 79 | + """ |
| 80 | + def inner_func(*paths: str): |
| 81 | + # WebService always serves the static files under /static. |
| 82 | + assert paths[0] == "static" |
| 83 | + |
| 84 | + url_path_part = base_url(*paths) |
| 85 | + |
| 86 | + if paths in self.cache: |
| 87 | + return url_path_part + self.cache[paths] |
| 88 | + |
| 89 | + for module_name, dir in self.static_locations: |
| 90 | + resource = importlib.resources.files(module_name).joinpath(dir, *paths[1:]) |
| 91 | + if resource.is_file(): |
| 92 | + with resource.open('rb') as file: |
| 93 | + hash = hashlib.file_digest(file, hashlib.sha256).hexdigest() |
| 94 | + result = "?h=" + hash[:24] |
| 95 | + break |
| 96 | + else: |
| 97 | + logger.warning(f"Did not find path passed to static_url(): {paths}") |
| 98 | + result = "" |
| 99 | + |
| 100 | + self.cache[paths] = result |
| 101 | + return url_path_part + result |
| 102 | + return inner_func |
48 | 103 |
|
49 | 104 | class WebService(Service): |
50 | 105 | """RPC service with Web server capabilities. |
@@ -78,6 +133,8 @@ def __init__( |
78 | 133 | cache=True, cache_timeout=SECONDS_IN_A_YEAR, |
79 | 134 | fallback_mimetype="application/octet-stream") |
80 | 135 |
|
| 136 | + self.static_file_hasher = StaticFileHasher(static_files) |
| 137 | + |
81 | 138 | self.file_cacher = FileCacher(self) |
82 | 139 | self.wsgi_app = FileServerMiddleware(self.file_cacher, self.wsgi_app) |
83 | 140 |
|
|
0 commit comments