image file info toegevoegd bij CMD+ENTER
This commit is contained in:
Binary file not shown.
Binary file not shown.
@@ -79,6 +79,19 @@ async def pdf(
|
||||
)
|
||||
|
||||
|
||||
@router.get("/image")
|
||||
async def image(
|
||||
path: str,
|
||||
service: FileOpsService = Depends(get_file_ops_service),
|
||||
) -> StreamingResponse:
|
||||
prepared = service.prepare_image_stream(path=path)
|
||||
return StreamingResponse(
|
||||
prepared["content"],
|
||||
headers=prepared["headers"],
|
||||
media_type=prepared["content_type"],
|
||||
)
|
||||
|
||||
|
||||
@router.get("/thumbnail")
|
||||
async def thumbnail(
|
||||
path: str,
|
||||
|
||||
@@ -92,6 +92,8 @@ class FileInfoResponse(BaseModel):
|
||||
content_type: str | None = None
|
||||
owner: str | None = None
|
||||
group: str | None = None
|
||||
width: int | None = None
|
||||
height: int | None = None
|
||||
|
||||
|
||||
class SettingsResponse(BaseModel):
|
||||
|
||||
Binary file not shown.
@@ -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
|
||||
|
||||
Binary file not shown.
@@ -32,6 +32,15 @@ THUMBNAIL_CONTENT_TYPES = {
|
||||
".png": "image/png",
|
||||
".webp": "image/webp",
|
||||
}
|
||||
IMAGE_CONTENT_TYPES = {
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".png": "image/png",
|
||||
".webp": "image/webp",
|
||||
".gif": "image/gif",
|
||||
".bmp": "image/bmp",
|
||||
".avif": "image/avif",
|
||||
}
|
||||
VIDEO_CONTENT_TYPES = {
|
||||
".mp4": "video/mp4",
|
||||
".mkv": "video/x-matroska",
|
||||
@@ -270,6 +279,8 @@ class FileOpsService:
|
||||
content_type=metadata["content_type"],
|
||||
owner=metadata["owner"],
|
||||
group=metadata["group"],
|
||||
width=metadata["width"],
|
||||
height=metadata["height"],
|
||||
)
|
||||
|
||||
def save(self, path: str, content: str, expected_modified: str) -> SaveResponse:
|
||||
@@ -413,6 +424,39 @@ class FileOpsService:
|
||||
"content": self._filesystem.stream_file(resolved_target.absolute),
|
||||
}
|
||||
|
||||
def prepare_image_stream(self, path: str) -> dict:
|
||||
resolved_target = self._path_guard.resolve_existing_path(path)
|
||||
|
||||
if resolved_target.absolute.is_dir():
|
||||
raise AppError(
|
||||
code="type_conflict",
|
||||
message="Source must be a file",
|
||||
status_code=409,
|
||||
details={"path": resolved_target.relative},
|
||||
)
|
||||
if not resolved_target.absolute.is_file():
|
||||
raise AppError(
|
||||
code="type_conflict",
|
||||
message="Unsupported path type for image",
|
||||
status_code=409,
|
||||
details={"path": resolved_target.relative},
|
||||
)
|
||||
|
||||
content_type = self._image_content_type_for(resolved_target.absolute)
|
||||
if content_type is None:
|
||||
raise AppError(
|
||||
code="unsupported_type",
|
||||
message="File type is not supported for image viewing",
|
||||
status_code=409,
|
||||
details={"path": resolved_target.relative},
|
||||
)
|
||||
|
||||
return {
|
||||
"headers": {"Content-Length": str(int(resolved_target.absolute.stat().st_size))},
|
||||
"content_type": content_type,
|
||||
"content": self._filesystem.stream_file(resolved_target.absolute),
|
||||
}
|
||||
|
||||
def prepare_pdf_stream(self, path: str) -> dict:
|
||||
resolved_target = self._path_guard.resolve_existing_path(path)
|
||||
|
||||
@@ -465,6 +509,10 @@ class FileOpsService:
|
||||
def _thumbnail_content_type_for(path: Path) -> str | None:
|
||||
return THUMBNAIL_CONTENT_TYPES.get(path.suffix.lower())
|
||||
|
||||
@staticmethod
|
||||
def _image_content_type_for(path: Path) -> str | None:
|
||||
return IMAGE_CONTENT_TYPES.get(path.suffix.lower())
|
||||
|
||||
@staticmethod
|
||||
def _pdf_content_type_for(path: Path) -> str | None:
|
||||
return PDF_CONTENT_TYPES.get(path.suffix.lower())
|
||||
|
||||
Reference in New Issue
Block a user