From 1b5249daf3940779039b0b8dcd31a2c79f18a518 Mon Sep 17 00:00:00 2001 From: Roy Olav Purser Date: Wed, 9 Jun 2021 15:01:19 +0200 Subject: [PATCH] minify css --- backend/sources.py | 2 +- backend/stream.py | 16 ++++++++++------ backend/style.js | 43 ++++++++++++++++++++++++++----------------- frontend/index.html | 2 +- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/backend/sources.py b/backend/sources.py index c77d084..278ef53 100755 --- a/backend/sources.py +++ b/backend/sources.py @@ -43,7 +43,7 @@ else: except Exception as e: print(e) else: - with open("/app/chromecast.css", "w") as f: + with open("/app/setup/chromecast.css", "w") as f: f.write(resp.text) with open("/app/version/chromecast.txt", "w") as f: f.write(chromecast_version) diff --git a/backend/stream.py b/backend/stream.py index 81ba639..462ebd1 100755 --- a/backend/stream.py +++ b/backend/stream.py @@ -171,9 +171,7 @@ try: with open("/app/favicon.png", "rb") as f: favicon = f.read() with open("/app/style.css", "r") as f: - custom_style_raw = bytes(f.read().strip(), "utf-8") - b64 = str(base64.b64encode(custom_style_raw), "ascii") - custom_style = f'data:text/css;charset=utf-8;base64,{b64}' + custom_style = f.read() except Exception as e: logger.info(e) @@ -210,7 +208,6 @@ class MainHandler(tornado.web.RequestHandler): data["videojs_version"] = videojs_version data["chromecast_version"] = chromecast_version data["font_awesome_version"] = font_awesome_version - data["custom_style"] = custom_style rendered_html = template_html.generate(data=data, meta=provider_data.meta(), title=provider_data.title()) self.write(rendered_html) else: @@ -222,18 +219,25 @@ class MainHandler(tornado.web.RequestHandler): async def head(self): await self.handle_any(False) -class FileHandler(tornado.web.RequestHandler): +class PlaylistHandler(tornado.web.RequestHandler): def get(self): self.set_header("Content-Type", "text/plain; charset=utf-8") self.write(playlist) + class IconHandler(tornado.web.RequestHandler): def get(self): self.set_header("Content-Type", "image/png") self.write(favicon) + +class StyleHandler(tornado.web.RequestHandler): + def get(self): + self.set_header("Content-Type", "text/css; charset=utf-8") + self.write(custom_style) try: handlers = [] - handlers.append((tornado.routing.PathMatches("/sources.m3u8"), FileHandler)) + handlers.append((tornado.routing.PathMatches("/sources.m3u8"), PlaylistHandler)) handlers.append((tornado.routing.PathMatches("/favicon.ico"), IconHandler)) + handlers.append((tornado.routing.PathMatches("/style.css"), StyleHandler)) handlers.append((tornado.routing.AnyMatches(), MainHandler)) app_web = tornado.web.Application(handlers) app_web.listen(8080) diff --git a/backend/style.js b/backend/style.js index 93d3192..90c035b 100644 --- a/backend/style.js +++ b/backend/style.js @@ -3,24 +3,33 @@ const fs = require("fs"); const css = require("css"); -fs.readFile("/app/chromecast.css", "utf-8", (err, data) => { - const ast = css.parse(data); - for(const rule of ast.stylesheet.rules) { - const decls = []; - if(Symbol.iterator in Object(rule.declarations)) { - for(const decl of rule.declarations) { - const isNotUrl = !(decl.value.toLowerCase().startsWith("url")); - const isNotContent = !(decl.property.toLowerCase() == "content"); - if(isNotContent && isNotUrl) { - decls.push(decl); - } - } - rule.declarations = decls; - } +fs.readFile("/app/style.css", "utf-8", (err, a) => { + let data = ""; + if(err === null) { + data = a + "\n"; } - fs.appendFile("/app/style.css", css.stringify(ast, {"compress": true}), (err) => { - if(err !== null) { - console.log(err); + fs.readFile("/app/setup/chromecast.css", "utf-8", (err, b) => { + if(err === null) { + data += b + "\n"; } + const ast = css.parse(data); + for(const rule of ast.stylesheet.rules) { + const decls = []; + if(Symbol.iterator in Object(rule.declarations)) { + for(const decl of rule.declarations) { + const isNotUrl = !(decl.value.toLowerCase().startsWith("url")); + const isNotContent = !(decl.property.toLowerCase() == "content"); + if(isNotContent && isNotUrl) { + decls.push(decl); + } + } + rule.declarations = decls; + } + } + fs.writeFile("/app/style.css", css.stringify(ast, {"compress": true}), (err) => { + if(err !== null) { + console.log(err); + } + }); }); }); diff --git a/frontend/index.html b/frontend/index.html index 3103bad..d36d298 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -10,6 +10,6 @@ - +