fix
This commit is contained in:
@@ -43,7 +43,6 @@ let
|
|||||||
import cairosvg
|
import cairosvg
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
CONFIG_PATH = os.environ.get("FAVICON_CONFIG", "/config/config.json")
|
CONFIG_PATH = os.environ.get("FAVICON_CONFIG", "/config/config.json")
|
||||||
LOGO_PATH = os.environ.get("FAVICON_LOGO", "/assets/logo.svg")
|
LOGO_PATH = os.environ.get("FAVICON_LOGO", "/assets/logo.svg")
|
||||||
CACHE_DIR = Path(os.environ.get("FAVICON_CACHE_DIR", "/cache"))
|
CACHE_DIR = Path(os.environ.get("FAVICON_CACHE_DIR", "/cache"))
|
||||||
@@ -51,27 +50,22 @@ let
|
|||||||
LISTEN_PORT = int(os.environ.get("FAVICON_PORT", "8080"))
|
LISTEN_PORT = int(os.environ.get("FAVICON_PORT", "8080"))
|
||||||
DEFAULT_CACHE_CONTROL = "public, max-age=86400"
|
DEFAULT_CACHE_CONTROL = "public, max-age=86400"
|
||||||
|
|
||||||
|
|
||||||
with open(CONFIG_PATH, "r", encoding="utf-8") as fh:
|
with open(CONFIG_PATH, "r", encoding="utf-8") as fh:
|
||||||
APP_CONFIG = json.load(fh)
|
APP_CONFIG = json.load(fh)
|
||||||
with open(LOGO_PATH, "rb") as fh:
|
with open(LOGO_PATH, "rb") as fh:
|
||||||
LOGO_BYTES = fh.read()
|
LOGO_BYTES = fh.read()
|
||||||
LOGO_HASH = hashlib.sha256(LOGO_BYTES).hexdigest()
|
LOGO_HASH = hashlib.sha256(LOGO_BYTES).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
def _normalize_host(host):
|
def _normalize_host(host):
|
||||||
return (host or "").split(":", 1)[0].strip().lower()
|
return (host or "").split(":", 1)[0].strip().lower()
|
||||||
|
|
||||||
|
|
||||||
def _pick_profile(host):
|
def _pick_profile(host):
|
||||||
mappings = APP_CONFIG.get("mappings", {})
|
mappings = APP_CONFIG.get("mappings", {})
|
||||||
return mappings.get(host) or APP_CONFIG.get("default")
|
return mappings.get(host) or APP_CONFIG.get("default")
|
||||||
|
|
||||||
|
|
||||||
def _color(value, fallback):
|
def _color(value, fallback):
|
||||||
return value if isinstance(value, str) and value else fallback
|
return value if isinstance(value, str) and value else fallback
|
||||||
|
|
||||||
|
|
||||||
def _tinted_logo_data_uri(color):
|
def _tinted_logo_data_uri(color):
|
||||||
svg = LOGO_BYTES.decode("utf-8")
|
svg = LOGO_BYTES.decode("utf-8")
|
||||||
svg = re.sub(
|
svg = re.sub(
|
||||||
@@ -112,7 +106,6 @@ let
|
|||||||
)
|
)
|
||||||
return "data:image/svg+xml;base64," + base64.b64encode(svg.encode("utf-8")).decode("ascii")
|
return "data:image/svg+xml;base64," + base64.b64encode(svg.encode("utf-8")).decode("ascii")
|
||||||
|
|
||||||
|
|
||||||
def _border_radius():
|
def _border_radius():
|
||||||
value = APP_CONFIG.get("borderRadius", "8")
|
value = APP_CONFIG.get("borderRadius", "8")
|
||||||
text = str(value).strip()
|
text = str(value).strip()
|
||||||
@@ -120,7 +113,6 @@ let
|
|||||||
return text
|
return text
|
||||||
return f"{text}px"
|
return f"{text}px"
|
||||||
|
|
||||||
|
|
||||||
def _render_svg(profile):
|
def _render_svg(profile):
|
||||||
bg = _color(profile.get("background"), "#111827")
|
bg = _color(profile.get("background"), "#111827")
|
||||||
fg = _color(profile.get("foreground"), "#f8fafc")
|
fg = _color(profile.get("foreground"), "#f8fafc")
|
||||||
@@ -133,17 +125,8 @@ let
|
|||||||
<image href="{logo_data_uri}" x="0" y="0" width="{canvas}" height="{canvas}" preserveAspectRatio="xMidYMid meet" />
|
<image href="{logo_data_uri}" x="0" y="0" width="{canvas}" height="{canvas}" preserveAspectRatio="xMidYMid meet" />
|
||||||
</svg>"""
|
</svg>"""
|
||||||
|
|
||||||
|
|
||||||
def _cache_key(host, profile):
|
|
||||||
payload = json.dumps(
|
|
||||||
{"host": host, "profile": profile, "logo": LOGO_HASH},
|
|
||||||
sort_keys=True,
|
|
||||||
).encode("utf-8")
|
|
||||||
return hashlib.sha256(payload).hexdigest()
|
|
||||||
|
|
||||||
|
|
||||||
def _generate_asset(host, profile):
|
def _generate_asset(host, profile):
|
||||||
cache_name = f"{_cache_key(host, profile)}.ico"
|
cache_name = f"{host}.ico"
|
||||||
target = CACHE_DIR / cache_name
|
target = CACHE_DIR / cache_name
|
||||||
if target.exists():
|
if target.exists():
|
||||||
return target
|
return target
|
||||||
@@ -156,7 +139,6 @@ let
|
|||||||
image.close()
|
image.close()
|
||||||
return target
|
return target
|
||||||
|
|
||||||
|
|
||||||
class Handler(BaseHTTPRequestHandler):
|
class Handler(BaseHTTPRequestHandler):
|
||||||
server_version = "favicon-router/1.0"
|
server_version = "favicon-router/1.0"
|
||||||
|
|
||||||
@@ -186,7 +168,6 @@ let
|
|||||||
def log_message(self, fmt, *args):
|
def log_message(self, fmt, *args):
|
||||||
print("%s - - [%s] %s" % (self.address_string(), self.log_date_time_string(), fmt % args))
|
print("%s - - [%s] %s" % (self.address_string(), self.log_date_time_string(), fmt % args))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
httpd = ThreadingHTTPServer((LISTEN_HOST, LISTEN_PORT), Handler)
|
httpd = ThreadingHTTPServer((LISTEN_HOST, LISTEN_PORT), Handler)
|
||||||
httpd.serve_forever()
|
httpd.serve_forever()
|
||||||
|
|||||||
Reference in New Issue
Block a user