Add some CI for security testing
Some checks failed
Datadog Software Composition Analysis / Datadog SBOM Generation and Upload (push) Successful in 16s
Datadog Static Analysis / Datadog Static Analyzer (push) Failing after 0s

This commit is contained in:
2025-02-20 20:58:44 -05:00
parent b2ed4cc4e5
commit 0a3abf439f
5 changed files with 144 additions and 39 deletions

View File

@ -1,32 +1,39 @@
from dataclasses import dataclass
from src.config.config import Configuration
from src.rendering import GENERIC_FILE_MAPPING
from src.rendering.markdown import render_markdown
from enum import Enum
from thumbhash import image_to_thumbhash
from PIL import Image
from datetime import datetime
import frontmatter
@dataclass
class ImageMetadata:
path: str
width: int
height: int
alt: str
thumbhash: str
# exif attributes
exif: dict
@dataclass
class MarkdownMetadata:
fontmatter: dict
content: str
preview: str
@dataclass
class FileMetadata:
path: str
first_hundred_chars: str
typeMeta: MarkdownMetadata | None
@dataclass
class TemplateFile:
name: str
path: str
proper_name: str
extension: str
categories: list[str]
date_modified: str
@ -37,10 +44,47 @@ class TemplateFile:
is_dir: bool
def format_date(timestamp):
return datetime.fromtimestamp(timestamp).strftime("%Y-%m-%d")
class TemplateHelpers:
def __init__(self, config: Configuration):
self.config: Configuration = config
def build_metadata_for_file(self, path: str, categories: list[str] = []):
"""Builds metadata for a file"""
file_path = self.config.content_dir / path
for k in categories:
if k == "image":
img = Image.open(file_path)
exif = img._getexif()
orientation = exif.get(274, 1) if exif else 1
width, height = img.width, img.height
if orientation in [5, 6, 7, 8]:
width, height = height, width
return ImageMetadata(
width=width,
height=height,
alt=file_path.name,
exif=img.info,
)
elif k == "document":
ret = None
with open(file_path, "r") as fdoc:
ret = FileMetadata(None)
if file_path.suffix[1:].lower() == "md":
ret.typeMeta = MarkdownMetadata({}, "", "")
ret.typeMeta.fontmatter = frontmatter.load(file_path)
ret.typeMeta.content = render_markdown(file_path)
ret.typeMeta.preview = ret.typeMeta.content[:100]
if "#" in ret.typeMeta.preview:
ret.typeMeta.preview = ret.typeMeta.preview.split("#")[0]
return ret
return None
def get_folder_contents(self, path: str = ""):
"""Returns the contents of a folder as a list of TemplateFile objects
@ -52,45 +96,25 @@ class TemplateHelpers:
for f in files:
t = TemplateFile(
name=f.name,
path=str(f.relative_to(self.config.content_dir)),
proper_name=f.stem,
extension=f.suffix.lower(),
categories=[],
date_modified=f.stat().st_mtime,
date_created=f.stat().st_ctime,
date_modified=format_date(f.stat().st_mtime),
date_created=format_date(f.stat().st_ctime),
size_kb=f.stat().st_size / 1024,
metadata=None,
dir_item_count=len(list(f.glob("*"))) if f.is_dir() else 0,
is_dir=f.is_dir(),
)
if f.is_file():
# Build metadata depending on the mapping in GENERIC_FILE_MAPPING
for k, v in GENERIC_FILE_MAPPING.items():
if f.suffix[1:].lower() in v:
t.categories.append(k)
if k == "image":
img = Image.open(f)
exif = img._getexif()
orientation = exif.get(274, 1) if exif else 1
width, height = img.width, img.height
if orientation in [5, 6, 7, 8]:
width, height = height, width
t.metadata = ImageMetadata(
path=str(f.relative_to(self.config.content_dir)),
width=width,
height=height,
alt=f.name,
thumbhash=image_to_thumbhash(img),
exif=img.info,
)
elif k == "document":
with open(f, "r") as fdoc:
t.metadata = FileMetadata(
path=str(f.relative_to(self.config.content_dir)),
first_hundred_chars=fdoc.read(100),
)
t.metadata = self.build_metadata_for_file(f, t.categories)
ret.append(t)
return ret
def get_sibling_content_files(self, path: str = ""):
search_contnet_path = self.config.content_dir / path
files = search_contnet_path.glob("*")

View File

@ -43,4 +43,4 @@ def generate_thumbnail(image_path, resize_percent, min_width):
img.save(thumbnail_io, format=img_format)
thumbnail_io.seek(0)
return (thumbnail_io, img_format)
return (thumbnail_io.getvalue(), img_format)