|
@@ -10,7 +10,7 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
|
from pathlib import Path
|
|
from pathlib import Path
|
|
|
from urllib.parse import unquote, urlsplit
|
|
from urllib.parse import unquote, urlsplit
|
|
|
|
|
|
|
|
-from .assets import AssetIntegrityError, AssetValidationResult, validate_generated_bundle
|
|
|
|
|
|
|
+from .assets import AssetIntegrityError, AssetValidationResult, validate_generated_assets, validate_generated_bundle
|
|
|
|
|
|
|
|
|
|
|
|
|
ROOT = Path(__file__).resolve().parents[3]
|
|
ROOT = Path(__file__).resolve().parents[3]
|
|
@@ -30,6 +30,20 @@ GENERATED_FILES = {
|
|
|
"/generated/board-silkscreen-back.svg": "board-silkscreen-back.svg",
|
|
"/generated/board-silkscreen-back.svg": "board-silkscreen-back.svg",
|
|
|
"/generated/board-copper-back.svg": "board-copper-back.svg",
|
|
"/generated/board-copper-back.svg": "board-copper-back.svg",
|
|
|
}
|
|
}
|
|
|
|
|
+GENERATED_V1_1_FILES = {
|
|
|
|
|
+ "/generated-v1_1/board-data.json": "board-data.json",
|
|
|
|
|
+ "/generated-v1_1/board-mask-edge.svg": "board-mask-edge.svg",
|
|
|
|
|
+ "/generated-v1_1/board-silkscreen.svg": "board-silkscreen.svg",
|
|
|
|
|
+ "/generated-v1_1/board-copper.svg": "board-copper.svg",
|
|
|
|
|
+ "/generated-v1_1/board-mask-edge-back.svg": "board-mask-edge-back.svg",
|
|
|
|
|
+ "/generated-v1_1/board-silkscreen-back.svg": "board-silkscreen-back.svg",
|
|
|
|
|
+ "/generated-v1_1/board-copper-back.svg": "board-copper-back.svg",
|
|
|
|
|
+}
|
|
|
|
|
+SCHEMATIC_FILES = {
|
|
|
|
|
+ "/schematics/FCB_FPGA+MCU0611.pdf": "reference/original/schematics/FCB_FPGA+MCU0611.pdf",
|
|
|
|
|
+ "/schematics/v1.1.pdf": "reference/original/schematics/FCB_FPGA+MCU0611.pdf",
|
|
|
|
|
+ "/schematics/fcb_v1_1.pdf": "reference/original/schematics/FCB_FPGA+MCU0611.pdf",
|
|
|
|
|
+}
|
|
|
PHOTO_ASSET = re.compile(r"^board-photo\.(?:png|jpg|jpeg|webp)$")
|
|
PHOTO_ASSET = re.compile(r"^board-photo\.(?:png|jpg|jpeg|webp)$")
|
|
|
|
|
|
|
|
|
|
|
|
@@ -86,6 +100,22 @@ def create_server(
|
|
|
raise AssetIntegrityError(f"unsafe photo asset name: {photo_asset}")
|
|
raise AssetIntegrityError(f"unsafe photo asset name: {photo_asset}")
|
|
|
generated_files[f"/generated/{photo_asset}"] = photo_asset
|
|
generated_files[f"/generated/{photo_asset}"] = photo_asset
|
|
|
|
|
|
|
|
|
|
+ v1_1_dir = generated.parent / "generated-v1_1"
|
|
|
|
|
+ if not v1_1_dir.is_dir():
|
|
|
|
|
+ v1_1_dir = repository / "tools" / "board-viewer" / "generated-v1_1"
|
|
|
|
|
+ v1_1_files: dict[str, Path] = {}
|
|
|
|
|
+ if v1_1_dir.is_dir():
|
|
|
|
|
+ for url_path, rel_name in GENERATED_V1_1_FILES.items():
|
|
|
|
|
+ candidate = v1_1_dir / rel_name
|
|
|
|
|
+ if candidate.is_file():
|
|
|
|
|
+ v1_1_files[url_path] = candidate
|
|
|
|
|
+
|
|
|
|
|
+ schematic_map: dict[str, Path] = {}
|
|
|
|
|
+ for url_path, rel_path in SCHEMATIC_FILES.items():
|
|
|
|
|
+ candidate = repository / rel_path
|
|
|
|
|
+ if candidate.is_file():
|
|
|
|
|
+ schematic_map[url_path] = candidate
|
|
|
|
|
+
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
class Handler(BaseHTTPRequestHandler):
|
|
|
server_version = "BoardViewer/1.0"
|
|
server_version = "BoardViewer/1.0"
|
|
|
sys_version = ""
|
|
sys_version = ""
|
|
@@ -99,8 +129,8 @@ def create_server(
|
|
|
self.send_header("Cross-Origin-Resource-Policy", "same-origin")
|
|
self.send_header("Cross-Origin-Resource-Policy", "same-origin")
|
|
|
self.send_header(
|
|
self.send_header(
|
|
|
"Content-Security-Policy",
|
|
"Content-Security-Policy",
|
|
|
- "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; "
|
|
|
|
|
- "connect-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'none'",
|
|
|
|
|
|
|
+ "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; "
|
|
|
|
|
+ "connect-src 'self'; object-src 'self'; base-uri 'none'; frame-ancestors 'self'; form-action 'none'",
|
|
|
)
|
|
)
|
|
|
self.send_header("Cache-Control", "no-store")
|
|
self.send_header("Cache-Control", "no-store")
|
|
|
|
|
|
|
@@ -117,14 +147,22 @@ def create_server(
|
|
|
self._send_bytes(b"Not Found\n", "text/plain; charset=utf-8", status=HTTPStatus.NOT_FOUND, head=head)
|
|
self._send_bytes(b"Not Found\n", "text/plain; charset=utf-8", status=HTTPStatus.NOT_FOUND, head=head)
|
|
|
|
|
|
|
|
def _serve(self, *, head: bool = False) -> None:
|
|
def _serve(self, *, head: bool = False) -> None:
|
|
|
- path = _request_path(self.path)
|
|
|
|
|
|
|
+ url_parts = urlsplit(self.path)
|
|
|
|
|
+ path = _request_path(url_parts.path)
|
|
|
if path is None:
|
|
if path is None:
|
|
|
self._send_not_found(head=head)
|
|
self._send_not_found(head=head)
|
|
|
return
|
|
return
|
|
|
if path == "/api/status":
|
|
if path == "/api/status":
|
|
|
|
|
+ query_str = url_parts.query or ""
|
|
|
|
|
+ target_bundle = generated
|
|
|
|
|
+ ver = "v1_2"
|
|
|
|
|
+ if "version=v1_1" in query_str and v1_1_dir.is_dir():
|
|
|
|
|
+ target_bundle = v1_1_dir
|
|
|
|
|
+ ver = "v1_1"
|
|
|
try:
|
|
try:
|
|
|
- result = validate_generated_bundle(generated, repository_root=repository)
|
|
|
|
|
|
|
+ result = validate_generated_assets(target_bundle, repository_root=repository)
|
|
|
payload = _runtime_payload(result)
|
|
payload = _runtime_payload(result)
|
|
|
|
|
+ payload["version"] = ver
|
|
|
status = HTTPStatus.OK
|
|
status = HTTPStatus.OK
|
|
|
except AssetIntegrityError as exc:
|
|
except AssetIntegrityError as exc:
|
|
|
payload = {
|
|
payload = {
|
|
@@ -134,6 +172,7 @@ def create_server(
|
|
|
"recommendationsEnabled": False,
|
|
"recommendationsEnabled": False,
|
|
|
"adcCopyEnabled": False,
|
|
"adcCopyEnabled": False,
|
|
|
"message": str(exc),
|
|
"message": str(exc),
|
|
|
|
|
+ "version": ver,
|
|
|
}
|
|
}
|
|
|
status = HTTPStatus.SERVICE_UNAVAILABLE
|
|
status = HTTPStatus.SERVICE_UNAVAILABLE
|
|
|
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
@@ -143,6 +182,10 @@ def create_server(
|
|
|
file_path = web / WEB_FILES[path]
|
|
file_path = web / WEB_FILES[path]
|
|
|
elif path in generated_files:
|
|
elif path in generated_files:
|
|
|
file_path = generated / generated_files[path]
|
|
file_path = generated / generated_files[path]
|
|
|
|
|
+ elif path in v1_1_files:
|
|
|
|
|
+ file_path = v1_1_files[path]
|
|
|
|
|
+ elif path in schematic_map:
|
|
|
|
|
+ file_path = schematic_map[path]
|
|
|
else:
|
|
else:
|
|
|
self._send_not_found(head=head)
|
|
self._send_not_found(head=head)
|
|
|
return
|
|
return
|
|
@@ -152,7 +195,9 @@ def create_server(
|
|
|
self._send_not_found(head=head)
|
|
self._send_not_found(head=head)
|
|
|
return
|
|
return
|
|
|
content_type = mimetypes.guess_type(file_path.name)[0] or "application/octet-stream"
|
|
content_type = mimetypes.guess_type(file_path.name)[0] or "application/octet-stream"
|
|
|
- if file_path.suffix in {".js", ".mjs"}:
|
|
|
|
|
|
|
+ if file_path.suffix == ".pdf":
|
|
|
|
|
+ content_type = "application/pdf"
|
|
|
|
|
+ elif file_path.suffix in {".js", ".mjs"}:
|
|
|
content_type = "text/javascript"
|
|
content_type = "text/javascript"
|
|
|
if file_path.suffix in {".html", ".css", ".js", ".mjs", ".json", ".svg"}:
|
|
if file_path.suffix in {".html", ".css", ".js", ".mjs", ".json", ".svg"}:
|
|
|
content_type += "; charset=utf-8"
|
|
content_type += "; charset=utf-8"
|