mirror of
https://github.com/kevinveenbirkenbach/homepage.veen.world.git
synced 2025-07-19 15:44:24 +02:00
Implemented SVG handling
This commit is contained in:
parent
246ef1b059
commit
9ada9acb3a
@ -100,6 +100,10 @@ h3.footer-title {
|
|||||||
font-size: 100px;
|
font-size: 100px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
svg {
|
||||||
|
fill: currentColor;
|
||||||
|
}
|
||||||
|
|
||||||
div#navbarNavheader li.nav-item {
|
div#navbarNavheader li.nav-item {
|
||||||
margin-left: 6px;
|
margin-left: 6px;
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,19 @@
|
|||||||
<div class="card h-100 d-flex flex-column">
|
<div class="card h-100 d-flex flex-column">
|
||||||
<div class="card-body d-flex flex-column">
|
<div class="card-body d-flex flex-column">
|
||||||
<div class="card-img-top">
|
<div class="card-img-top">
|
||||||
{# Prioritize image, fallback to icon via onerror #}
|
|
||||||
{% if card.icon.cache %}
|
{% if card.icon.cache %}
|
||||||
<img src="{{ card.icon.cache }}" alt="{{ card.title }}" style="width:100px; height:auto;"
|
{% if card.icon.cache.endswith('.svg') %}
|
||||||
onerror="this.style.display='none'; var icon=this.nextElementSibling; if(icon) icon.style.display='inline-block';">
|
<object type="image/svg+xml" data="{{ card.icon.cache }}" style="width:100px; height:auto;">
|
||||||
{% if card.icon.class %}
|
{% if card.icon.class %}
|
||||||
<i class="{{ card.icon.class }}" style="display:none;"></i>
|
<i class="{{ card.icon.class }}"></i>
|
||||||
|
{% endif %}
|
||||||
|
</object>
|
||||||
|
{% else %}
|
||||||
|
<img src="{{ card.icon.cache }}" alt="{{ card.title }}" style="width:100px; height:auto;"
|
||||||
|
onerror="this.style.display='none'; var icon=this.nextElementSibling; if(icon) icon.style.display='inline-block';">
|
||||||
|
{% if card.icon.class %}
|
||||||
|
<i class="{{ card.icon.class }}" style="display:none;"></i>
|
||||||
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% elif card.icon.class %}
|
{% elif card.icon.class %}
|
||||||
<i class="{{ card.icon.class }}"></i>
|
<i class="{{ card.icon.class }}"></i>
|
||||||
|
@ -1,33 +1,20 @@
|
|||||||
import os
|
import os
|
||||||
import hashlib
|
import hashlib
|
||||||
import requests
|
import requests
|
||||||
|
import mimetypes
|
||||||
|
|
||||||
class CacheManager:
|
class CacheManager:
|
||||||
"""
|
|
||||||
A class to manage caching of files, including creating temporary directories
|
|
||||||
and caching files locally with hashed filenames.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, cache_dir="static/cache"):
|
def __init__(self, cache_dir="static/cache"):
|
||||||
"""
|
|
||||||
Initialize the CacheManager with a cache directory.
|
|
||||||
|
|
||||||
:param cache_dir: The directory where cached files will be stored.
|
|
||||||
"""
|
|
||||||
self.cache_dir = cache_dir
|
self.cache_dir = cache_dir
|
||||||
self._ensure_cache_dir_exists()
|
self._ensure_cache_dir_exists()
|
||||||
|
|
||||||
def _ensure_cache_dir_exists(self):
|
def _ensure_cache_dir_exists(self):
|
||||||
"""
|
"""Ensure the cache directory exists on disk."""
|
||||||
Ensure the cache directory exists. If it doesn't, create it.
|
|
||||||
"""
|
|
||||||
if not os.path.exists(self.cache_dir):
|
if not os.path.exists(self.cache_dir):
|
||||||
os.makedirs(self.cache_dir)
|
os.makedirs(self.cache_dir)
|
||||||
|
|
||||||
def clear_cache(self):
|
def clear_cache(self):
|
||||||
"""
|
"""Remove all files from the cache directory."""
|
||||||
Clear all files in the cache directory.
|
|
||||||
"""
|
|
||||||
if os.path.exists(self.cache_dir):
|
if os.path.exists(self.cache_dir):
|
||||||
for filename in os.listdir(self.cache_dir):
|
for filename in os.listdir(self.cache_dir):
|
||||||
file_path = os.path.join(self.cache_dir, filename)
|
file_path = os.path.join(self.cache_dir, filename)
|
||||||
@ -36,31 +23,39 @@ class CacheManager:
|
|||||||
|
|
||||||
def cache_file(self, file_url):
|
def cache_file(self, file_url):
|
||||||
"""
|
"""
|
||||||
Download a file and store it locally in the cache directory with a hashed filename.
|
Download a file from `file_url` and store it in the cache.
|
||||||
|
If any HTTP error occurs, return "Undefined" instead of raising.
|
||||||
:param file_url: The URL of the file to cache.
|
|
||||||
:return: The local path of the cached file.
|
|
||||||
"""
|
"""
|
||||||
# Generate a hashed filename based on the URL
|
# Compute a short hash suffix for the URL
|
||||||
hash_object = hashlib.blake2s(file_url.encode('utf-8'), digest_size=8)
|
hash_object = hashlib.blake2s(file_url.encode('utf-8'), digest_size=8)
|
||||||
hash_suffix = hash_object.hexdigest()
|
hash_suffix = hash_object.hexdigest()
|
||||||
|
|
||||||
# Determine the base name for the file
|
# Derive a base filename: drop trailing 'download' if present
|
||||||
splitted_file_url = file_url.split("/")
|
url_parts = file_url.rstrip("/").split("/")
|
||||||
base_name = splitted_file_url[-2] if splitted_file_url[-1] == "download" else splitted_file_url[-1]
|
base_name = url_parts[-2] if url_parts[-1] == "download" else url_parts[-1]
|
||||||
|
|
||||||
# Construct the full path for the cached file
|
try:
|
||||||
filename = f"{base_name}_{hash_suffix}.png"
|
# Attempt to download the file (streaming)
|
||||||
|
response = requests.get(file_url, stream=True)
|
||||||
|
response.raise_for_status()
|
||||||
|
except requests.RequestException:
|
||||||
|
# On any network/HTTP error, skip caching and return "Undefined"
|
||||||
|
return "Undefined"
|
||||||
|
|
||||||
|
# Determine file extension from Content-Type header
|
||||||
|
content_type = response.headers.get('Content-Type', '')
|
||||||
|
extension = mimetypes.guess_extension(content_type.split(";")[0].strip())
|
||||||
|
if extension is None:
|
||||||
|
extension = '.png' # default extension
|
||||||
|
|
||||||
|
# Build final filename and full path
|
||||||
|
filename = f"{base_name}_{hash_suffix}{extension}"
|
||||||
full_path = os.path.join(self.cache_dir, filename)
|
full_path = os.path.join(self.cache_dir, filename)
|
||||||
|
|
||||||
# If the file already exists, return the cached path
|
# Write the file to cache if not already present
|
||||||
if os.path.exists(full_path):
|
if not os.path.exists(full_path):
|
||||||
return full_path
|
with open(full_path, "wb") as out_file:
|
||||||
|
for chunk in response.iter_content(chunk_size=1024):
|
||||||
|
out_file.write(chunk)
|
||||||
|
|
||||||
# Download the file and save it locally
|
|
||||||
response = requests.get(file_url, stream=True)
|
|
||||||
if response.status_code == 200:
|
|
||||||
with open(full_path, "wb") as file:
|
|
||||||
for chunk in response.iter_content(1024):
|
|
||||||
file.write(chunk)
|
|
||||||
return full_path
|
return full_path
|
||||||
|
Loading…
x
Reference in New Issue
Block a user