image file info toegevoegd bij CMD+ENTER

This commit is contained in:
kodi
2026-03-13 11:37:27 +01:00
parent 05569576a7
commit 018c3dcd94
18 changed files with 726 additions and 0 deletions
+115
View File
@@ -2,6 +2,7 @@ from __future__ import annotations
import shutil
import mimetypes
import struct
import grp
import pwd
from datetime import datetime, timezone
@@ -23,6 +24,7 @@ class FilesystemAdapter:
group = None
content_type, _ = mimetypes.guess_type(path.name)
width, height = self._image_dimensions(path) if path.is_file() else (None, None)
return {
"name": path.name,
"size": int(stat.st_size) if path.is_file() else None,
@@ -31,6 +33,8 @@ class FilesystemAdapter:
"group": group,
"content_type": content_type,
"extension": path.suffix.lower() or None,
"width": width,
"height": height,
}
def list_directory(self, directory: Path, show_hidden: bool) -> tuple[list[dict], list[dict]]:
@@ -159,3 +163,114 @@ class FilesystemAdapter:
def modified_iso(path: Path) -> str:
stat = path.stat()
return datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat().replace("+00:00", "Z")
def _image_dimensions(self, path: Path) -> tuple[int | None, int | None]:
suffix = path.suffix.lower()
try:
if suffix == ".png":
return self._png_dimensions(path)
if suffix in {".jpg", ".jpeg"}:
return self._jpeg_dimensions(path)
if suffix == ".gif":
return self._gif_dimensions(path)
if suffix == ".bmp":
return self._bmp_dimensions(path)
if suffix == ".webp":
return self._webp_dimensions(path)
if suffix == ".avif":
return self._avif_dimensions(path)
except (OSError, ValueError, struct.error):
return None, None
return None, None
@staticmethod
def _png_dimensions(path: Path) -> tuple[int | None, int | None]:
with path.open("rb") as handle:
header = handle.read(24)
if len(header) < 24 or header[:8] != b"\x89PNG\r\n\x1a\n":
return None, None
return struct.unpack(">II", header[16:24])
@staticmethod
def _jpeg_dimensions(path: Path) -> tuple[int | None, int | None]:
with path.open("rb") as handle:
if handle.read(2) != b"\xff\xd8":
return None, None
while True:
marker_prefix = handle.read(1)
if not marker_prefix:
return None, None
if marker_prefix != b"\xff":
continue
marker = handle.read(1)
while marker == b"\xff":
marker = handle.read(1)
if not marker or marker in {b"\xd8", b"\xd9"}:
return None, None
segment_length_bytes = handle.read(2)
if len(segment_length_bytes) != 2:
return None, None
segment_length = struct.unpack(">H", segment_length_bytes)[0]
if segment_length < 2:
return None, None
if marker in {b"\xc0", b"\xc1", b"\xc2", b"\xc3", b"\xc5", b"\xc6", b"\xc7", b"\xc9", b"\xca", b"\xcb", b"\xcd", b"\xce", b"\xcf"}:
payload = handle.read(5)
if len(payload) != 5:
return None, None
height, width = struct.unpack(">HH", payload[1:5])
return width, height
handle.seek(segment_length - 2, 1)
@staticmethod
def _gif_dimensions(path: Path) -> tuple[int | None, int | None]:
with path.open("rb") as handle:
header = handle.read(10)
if len(header) < 10 or header[:6] not in {b"GIF87a", b"GIF89a"}:
return None, None
width, height = struct.unpack("<HH", header[6:10])
return width, height
@staticmethod
def _bmp_dimensions(path: Path) -> tuple[int | None, int | None]:
with path.open("rb") as handle:
header = handle.read(26)
if len(header) < 26 or header[:2] != b"BM":
return None, None
width, height = struct.unpack("<ii", header[18:26])
return abs(width), abs(height)
@staticmethod
def _webp_dimensions(path: Path) -> tuple[int | None, int | None]:
with path.open("rb") as handle:
header = handle.read(64)
if len(header) < 30 or header[:4] != b"RIFF" or header[8:12] != b"WEBP":
return None, None
chunk = header[12:16]
if chunk == b"VP8 " and len(header) >= 30:
width, height = struct.unpack("<HH", header[26:30])
return width & 0x3FFF, height & 0x3FFF
if chunk == b"VP8L" and len(header) >= 25:
bits = struct.unpack("<I", header[21:25])[0]
width = (bits & 0x3FFF) + 1
height = ((bits >> 14) & 0x3FFF) + 1
return width, height
if chunk == b"VP8X" and len(header) >= 30:
width = 1 + int.from_bytes(header[24:27], "little")
height = 1 + int.from_bytes(header[27:30], "little")
return width, height
return None, None
@staticmethod
def _avif_dimensions(path: Path) -> tuple[int | None, int | None]:
with path.open("rb") as handle:
data = handle.read(256 * 1024)
if b"ftypavif" not in data and b"ftypavis" not in data:
return None, None
index = data.find(b"ispe")
if index == -1 or index + 20 > len(data):
return None, None
width = int.from_bytes(data[index + 12:index + 16], "big")
height = int.from_bytes(data[index + 16:index + 20], "big")
if width <= 0 or height <= 0:
return None, None
return width, height