2020-02-10 17:37:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2019-01-20 18:37:45 +00:00
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2016-2019 jkrehm andy29485 OzzieIsaacs
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-08-13 16:21:19 +00:00
|
|
|
# Inspired by https://github.com/ChrisTM/Flask-CacheBust
|
|
|
|
# Uses query strings so CSS font files are found without having to resort to absolute URLs
|
|
|
|
|
|
|
|
import os
|
2019-07-13 18:45:48 +00:00
|
|
|
import hashlib
|
|
|
|
|
|
|
|
from . import logger
|
|
|
|
|
|
|
|
|
|
|
|
log = logger.create()
|
2017-08-13 16:21:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def init_cache_busting(app):
|
|
|
|
"""
|
|
|
|
Configure `app` to so that `url_for` adds a unique query string to URLs generated
|
|
|
|
for the `'static'` endpoint.
|
|
|
|
|
|
|
|
This allows setting long cache expiration values on static resources
|
|
|
|
because whenever the resource changes, so does its URL.
|
|
|
|
"""
|
|
|
|
|
2017-08-19 15:32:00 +00:00
|
|
|
static_folder = os.path.join(app.static_folder, '') # path to the static file folder, with trailing slash
|
2017-08-13 16:21:19 +00:00
|
|
|
|
|
|
|
hash_table = {} # map of file hashes
|
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
log.debug('Computing cache-busting values...')
|
2017-08-13 16:21:19 +00:00
|
|
|
# compute file hashes
|
2017-09-17 05:15:08 +00:00
|
|
|
for dirpath, __, filenames in os.walk(static_folder):
|
2017-08-13 16:21:19 +00:00
|
|
|
for filename in filenames:
|
|
|
|
# compute version component
|
|
|
|
rooted_filename = os.path.join(dirpath, filename)
|
2022-04-03 18:26:43 +00:00
|
|
|
try:
|
|
|
|
with open(rooted_filename, 'rb') as f:
|
|
|
|
file_hash = hashlib.md5(f.read()).hexdigest()[:7] # nosec
|
|
|
|
# save version to tables
|
|
|
|
file_path = rooted_filename.replace(static_folder, "")
|
|
|
|
file_path = file_path.replace("\\", "/") # Convert Windows path to web path
|
|
|
|
hash_table[file_path] = file_hash
|
|
|
|
except PermissionError:
|
|
|
|
log.error("No permission to access {} file.".format(rooted_filename))
|
2017-08-13 16:21:19 +00:00
|
|
|
|
2019-07-13 18:45:48 +00:00
|
|
|
log.debug('Finished computing cache-busting values')
|
2017-08-13 16:21:19 +00:00
|
|
|
|
|
|
|
def bust_filename(filename):
|
|
|
|
return hash_table.get(filename, "")
|
|
|
|
|
|
|
|
def unbust_filename(filename):
|
|
|
|
return filename.split("?", 1)[0]
|
|
|
|
|
|
|
|
@app.url_defaults
|
2021-03-15 12:48:05 +00:00
|
|
|
# pylint: disable=unused-variable
|
2017-08-13 16:21:19 +00:00
|
|
|
def reverse_to_cache_busted_url(endpoint, values):
|
|
|
|
"""
|
|
|
|
Make `url_for` produce busted filenames when using the 'static' endpoint.
|
|
|
|
"""
|
2017-08-19 16:09:12 +00:00
|
|
|
if endpoint == "static":
|
|
|
|
file_hash = bust_filename(values["filename"])
|
|
|
|
if file_hash:
|
|
|
|
values["q"] = file_hash
|
2017-08-13 16:21:19 +00:00
|
|
|
|
|
|
|
def debusting_static_view(filename):
|
|
|
|
"""
|
|
|
|
Serve a request for a static file having a busted name.
|
|
|
|
"""
|
|
|
|
return original_static_view(filename=unbust_filename(filename))
|
|
|
|
|
|
|
|
# Replace the default static file view with our debusting view.
|
2017-08-19 16:09:12 +00:00
|
|
|
original_static_view = app.view_functions["static"]
|
|
|
|
app.view_functions["static"] = debusting_static_view
|