homedocs/app/routes/static.py
2025-04-03 13:51:52 +02:00

96 lines
3.4 KiB
Python

from flask import Blueprint, send_from_directory, current_app
import os
bp = Blueprint("static_assets", __name__)
# @bp.route("/static/libs/tabler-icons/tabler-icons.min.css")
# def tabler_icons():
# """Serve tabler-icons CSS from node_modules or download if missing"""
# icons_path = os.path.join(current_app.static_folder, "libs", "tabler-icons")
#
# # Create directory if it doesn't exist
# if not os.path.exists(icons_path):
# os.makedirs(icons_path)
#
# css_file = os.path.join(icons_path, "tabler-icons.min.css")
#
# # If file doesn't exist, download from CDN
# if not os.path.exists(css_file):
# import requests
#
# try:
# cdn_url = "https://cdn.jsdelivr.net/npm/@tabler/core@1.1.1/dist/css/tabler.min.css"
# response = requests.get(cdn_url)
# if response.status_code == 200:
# with open(css_file, "wb") as f:
# f.write(response.content)
# print(f"Downloaded tabler-icons.min.css from CDN")
# else:
# print(f"Failed to download tabler-icons CSS: {response.status_code}")
# except Exception as e:
# print(f"Error downloading tabler-icons CSS: {e}")
#
# return send_from_directory(icons_path, "tabler-icons.min.css")
@bp.route("/static/css/tabler.min.css")
def tabler_css():
"""Serve tabler CSS from static folder or download if missing"""
css_path = os.path.join(current_app.static_folder, "css")
# Create directory if it doesn't exist
if not os.path.exists(css_path):
os.makedirs(css_path)
css_file = os.path.join(css_path, "tabler.min.css")
# If file doesn't exist, download from CDN
if not os.path.exists(css_file):
import requests
try:
cdn_url = "https://cdn.jsdelivr.net/npm/@tabler/core@1.1.1/dist/css/tabler.min.css"
response = requests.get(cdn_url)
if response.status_code == 200:
with open(css_file, "wb") as f:
f.write(response.content)
print(f"Downloaded tabler.min.css from CDN")
else:
print(f"Failed to download tabler CSS: {response.status_code}")
except Exception as e:
print(f"Error downloading tabler CSS: {e}")
return send_from_directory(css_path, "tabler.min.css")
@bp.route("/static/img/favicon.png")
def favicon():
"""Serve favicon from static folder or create a default one if missing"""
img_path = os.path.join(current_app.static_folder, "img")
# Create directory if it doesn't exist
if not os.path.exists(img_path):
os.makedirs(img_path)
favicon_file = os.path.join(img_path, "favicon.png")
# If file doesn't exist, create a simple one
if not os.path.exists(favicon_file):
# Try to download a default favicon
import requests
try:
# Using a simple placeholder favicon
cdn_url = "https://www.svgrepo.com/show/529863/server-minimalistic.svg"
response = requests.get(cdn_url)
if response.status_code == 200:
with open(favicon_file, "wb") as f:
f.write(response.content)
print(f"Created default favicon.png")
else:
print(f"Failed to download favicon: {response.status_code}")
except Exception as e:
print(f"Error creating favicon: {e}")
return send_from_directory(img_path, "favicon.png")