Trước đây công tắc chỉ chặn tool mạng của agent; lệnh shell chỉ bị proxy giả, còn M365, Teams, nút Test, MCP đang chạy, task script, link đính kèm task, pip tự cài và tài nguyên web trong xem trước HTML vẫn ra mạng tự do. - Cổng chung application/network/network_guard.py, nối vào cấu hình sống ở Composition Root; nhà cung cấp AI (chat, danh sách model, thử model) không đi qua cổng này. - Lệnh shell của agent và task script chạy trong Windows AppContainer không có quyền mạng (macOS: sandbox-exec, Linux: unshare --net); không cô lập được thì từ chối chạy. - Không cấp quyền kế thừa của AppContainer lên thư mục chứa PySide6: Chromium không nạp được Qt6WebEngineCore.dll và tab Graph bị hỏng. - Bật chặn thì dừng MCP đang chạy; tool OneDrive đồng bộ trên máy vẫn dùng. - Mặc định tắt khi mở app lần đầu; nhãn và tooltip 3 ngôn ngữ cập nhật. - Test: tests/test_network_guard_lanes.py (có bài AppContainer thật). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
68 lines
2.3 KiB
Python
68 lines
2.3 KiB
Python
"""Pure helpers for previewing a file (R08-T12, moved out of
|
|
``ui/folder_tab.py`` — that file's module-level functions
|
|
``_read_text``/``_is_probably_text``/``_pptx_available``, lines 1519-1533 and
|
|
1565-1587 of the original 1587-line file). No Qt, no widget state — the
|
|
"is this file text? is pptx editing available?" questions the preview
|
|
manager asks before it decides how to render something.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
_PPTX_READY = None # cached: pptx-editing library available (after auto-install)
|
|
|
|
|
|
def pptx_available() -> bool:
|
|
"""True when python-pptx is importable. If it's MISSING, auto-download &
|
|
install it (via deps.ensure_module) so pptx editing 'just works' — cached
|
|
so the (one-time) install is attempted only once."""
|
|
global _PPTX_READY
|
|
if _PPTX_READY is None:
|
|
try:
|
|
from cowork_local.core.deps import ensure_module
|
|
|
|
ready = ensure_module("pptx", "python-pptx") is not None
|
|
except Exception: # noqa: BLE001
|
|
ready = False
|
|
from ..network import network_guard
|
|
|
|
if ready or not network_guard.is_blocked():
|
|
_PPTX_READY = ready # a refusal under "Block network" is retried later
|
|
return ready
|
|
return _PPTX_READY
|
|
|
|
|
|
def read_text(path: str) -> str:
|
|
"""Đọc tệp dạng văn bản, thay ký tự hỏng thay vì ném lỗi; không đọc được thì
|
|
trả về chuỗi rỗng.
|
|
"""
|
|
try:
|
|
return Path(path).read_text(encoding="utf-8", errors="replace")
|
|
except OSError as exc:
|
|
return f"[could not read file: {exc}]"
|
|
|
|
|
|
def is_probably_text(path: str) -> bool:
|
|
"""Đoán tệp này có phải văn bản không, bằng cách tìm byte NUL trong phần đầu.
|
|
|
|
Đoán sai theo hướng "là văn bản" sẽ hiện một màn hình ký tự rác, nên phép
|
|
thử cố tình bảo thủ.
|
|
"""
|
|
try:
|
|
with open(path, "rb") as f:
|
|
chunk = f.read(4096)
|
|
except OSError:
|
|
return False
|
|
if b"\x00" in chunk:
|
|
return False
|
|
try:
|
|
chunk.decode("utf-8")
|
|
return True
|
|
except UnicodeDecodeError:
|
|
# Latin-ish text still edits fine via errors="replace"; only reject on
|
|
# a hard binary signal (NUL above), so most source files pass.
|
|
return True
|
|
|
|
|
|
__all__ = ["pptx_available", "read_text", "is_probably_text"]
|