From 6aac4191f842aff5cbee5bb45bdd4ffad15cba85 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Mon, 24 Dec 2018 14:29:11 +0100 Subject: [PATCH] Hash dashboard resources --- esphomeyaml/dashboard/dashboard.py | 25 ++++++++++++++++++++-- esphomeyaml/dashboard/templates/index.html | 22 +++++++++---------- esphomeyaml/dashboard/templates/login.html | 8 +++---- 3 files changed, 38 insertions(+), 17 deletions(-) diff --git a/esphomeyaml/dashboard/dashboard.py b/esphomeyaml/dashboard/dashboard.py index 4b6df5d5ad..a5cb1e8163 100644 --- a/esphomeyaml/dashboard/dashboard.py +++ b/esphomeyaml/dashboard/dashboard.py @@ -296,7 +296,8 @@ class MainRequestHandler(BaseHandler): 'https://esphomelib.com/esphomeyaml/' self.render("templates/index.html", entries=entries, - version=version, begin=begin, docs_link=docs_link) + version=version, begin=begin, docs_link=docs_link, + get_static_file_url=get_static_file_url) def _ping_func(filename, address): @@ -413,7 +414,8 @@ class LoginHandler(BaseHandler): docs_link = 'https://beta.esphomelib.com/esphomeyaml/' if 'b' in version else \ 'https://esphomelib.com/esphomeyaml/' - self.render("templates/login.html", version=version, docs_link=docs_link, error=error) + self.render("templates/login.html", version=version, docs_link=docs_link, error=error, + get_static_file_url=get_static_file_url) def post_hassio_login(self): import requests @@ -451,6 +453,21 @@ class LoginHandler(BaseHandler): self.redirect("/") +_STATIC_FILE_HASHES = {} + + +def get_static_file_url(name): + static_path = os.path.join(os.path.dirname(__file__), 'static') + if name in _STATIC_FILE_HASHES: + hash_ = _STATIC_FILE_HASHES[name] + else: + path = os.path.join(static_path, name) + with open(path, 'rb') as f_handle: + hash_ = hash(f_handle.read()) + _STATIC_FILE_HASHES[name] = hash_ + return u'/static/{}?hash={}'.format(name, hash_) + + def make_app(debug=False): def log_function(handler): if handler.get_status() < 400: @@ -493,6 +510,10 @@ def make_app(debug=False): (r"/wizard.html", WizardRequestHandler), (r'/static/(.*)', StaticFileHandler, {'path': static_path}), ], debug=debug, cookie_secret=COOKIE_SECRET, log_function=log_function) + + if debug: + _STATIC_FILE_HASHES.clear() + return app diff --git a/esphomeyaml/dashboard/templates/index.html b/esphomeyaml/dashboard/templates/index.html index e92770eca7..230504fd89 100644 --- a/esphomeyaml/dashboard/templates/index.html +++ b/esphomeyaml/dashboard/templates/index.html @@ -4,17 +4,17 @@ ESPHome Dashboard - - - - + + + + - - - - + + + + - + @@ -469,8 +469,8 @@ - - + + {% if len(entries) == 0 %} +