fix exif parsing, this is basically 1.0
All checks were successful
Datadog Software Composition Analysis / Datadog SBOM Generation and Upload (push) Successful in 16s
Datadog Secrets Scanning / Datadog Static Analyzer (push) Successful in 15s
Datadog Static Analysis / Datadog Static Analyzer (push) Successful in 23s

This commit is contained in:
Tanishq Dubey 2025-03-11 22:56:40 -04:00
parent c6f36d0408
commit 1fc99dd2ce
3 changed files with 23 additions and 8 deletions

View File

@ -8,7 +8,7 @@ from src.rendering.markdown import (
) )
from enum import Enum from enum import Enum
from PIL import Image from PIL import Image, ExifTags
from datetime import datetime from datetime import datetime
import frontmatter import frontmatter
@ -95,15 +95,22 @@ class TemplateHelpers:
if k == "image": if k == "image":
img = Image.open(file_path) img = Image.open(file_path)
exif = img._getexif() exif = img._getexif()
# Conver exif to dict
orientation = exif.get(274, 1) if exif else 1 orientation = exif.get(274, 1) if exif else 1
width, height = img.width, img.height width, height = img.width, img.height
if orientation in [5, 6, 7, 8]: if orientation in [5, 6, 7, 8]:
width, height = height, width width, height = height, width
exif = {
ExifTags.TAGS[k]: v
for k, v in img._getexif().items()
if k in ExifTags.TAGS
}
return ImageMetadata( return ImageMetadata(
width=width, width=width,
height=height, height=height,
alt=file_path.name, alt=file_path.name,
exif=img.info, exif=exif,
) )
elif k == "document": elif k == "document":
ret = None ret = None
@ -111,8 +118,9 @@ class TemplateHelpers:
ret = FileMetadata(None) ret = FileMetadata(None)
if file_path.suffix[1:].lower() == "md": if file_path.suffix[1:].lower() == "md":
ret.typeMeta = MarkdownMetadata({}, "", "") ret.typeMeta = MarkdownMetadata({}, "", "")
ret.typeMeta.frontmatter = frontmatter.load(file_path) content, c_frontmatter, obj = render_markdown(file_path)
ret.typeMeta.content = render_markdown(file_path) ret.typeMeta.frontmatter = c_frontmatter
ret.typeMeta.content = content
ret.typeMeta.rawContent = read_raw_markdown(file_path) ret.typeMeta.rawContent = read_raw_markdown(file_path)
ret.typeMeta.rawText = rendered_markdown_to_plain_text( ret.typeMeta.rawText = rendered_markdown_to_plain_text(
ret.typeMeta.content ret.typeMeta.content
@ -165,6 +173,12 @@ class TemplateHelpers:
if f.suffix[1:].lower() in v: if f.suffix[1:].lower() in v:
t.categories.append(k) t.categories.append(k)
t.metadata = self._build_metadata_for_file(f, t.categories) t.metadata = self._build_metadata_for_file(f, t.categories)
if "image" in t.categories:
# Adjust date_modified and date_created to be the date the image was taken from exif if available
if t.metadata.exif and "DateTimeOriginal" in t.metadata.exif:
t.date_modified = t.metadata.exif["DateTimeOriginal"]
t.date_created = t.metadata.exif["DateTimeOriginal"]
ret.append(t) ret.append(t)
ret = self._filter_hidden_files(ret) ret = self._filter_hidden_files(ret)
return ret return ret

View File

@ -10,11 +10,11 @@ mistune_render = mistune.create_markdown(
) )
def render_markdown(path: Path) -> str: def render_markdown(path: Path):
with open(path, "r", encoding="utf-8") as f: with open(path, "r", encoding="utf-8") as f:
obj = frontmatter.load(f) obj = frontmatter.load(f)
res = mistune_render(obj.content) content = mistune_render(obj.content)
return res return content, obj.metadata, obj
def read_raw_markdown(path: Path) -> str: def read_raw_markdown(path: Path) -> str:

View File

@ -204,7 +204,7 @@ def render_page(
content = "" content = ""
if "document" in category and type == "file": if "document" in category and type == "file":
content = render_markdown(target_file) content, c_frontmatter, obj = render_markdown(target_file)
if not (template_path / "base.html").exists(): if not (template_path / "base.html").exists():
raise Exception("Base template not found") raise Exception("Base template not found")
@ -218,6 +218,7 @@ def render_page(
content=content, content=content,
styles=styles, styles=styles,
currentPath=str(relative_path), currentPath=str(relative_path),
metadata=c_frontmatter if "document" in category and type == "file" else None,
) )
return content return content