C:\TripServer\backups\admin_server\admin_server_backup_20260703_172839.py
# -*- coding: utf-8 -*-
from flask import Flask, request, redirect, send_from_directory, make_response, Response, send_file
from werkzeug.utils import secure_filename
import os
import time
import sys
import re
import shutil
import py_compile
import threading
import html as html_lib
import glob
import zipfile
import subprocess
import ipaddress
import json
import urllib.request
app = Flask(__name__)
ADMIN_PANEL_VERSION = "TripServer Admin v34.17 D drive downloads full access"
ADMIN_STARTED_AT = time.strftime("%Y-%m-%d %H:%M:%S")
BASE_DIR = r"C:\TripServer"
ADMIN_HOST = "0.0.0.0"
ADMIN_PORT = 8000
CONTROL_PORT = 8010
CONTROL_DIR = os.path.join(BASE_DIR, "control_server")
CONTROL_SCRIPT_PATH = os.path.join(CONTROL_DIR, "tripserver_control_server.py")
CONTROL_PENDING_ADMIN_PATH = os.path.join(CONTROL_DIR, "pending_admin_server.py")
CONTROL_REQUEST_PATH = os.path.join(CONTROL_DIR, "request.json")
CONTROL_STATUS_PATH = os.path.join(CONTROL_DIR, "status.json")
CONTROL_LOG_PATH = os.path.join(CONTROL_DIR, "tripserver_control_server.log")
CONTROL_BOOTSTRAP_LOG_PATH = os.path.join(BASE_DIR, "tripserver_control_bootstrap.log")
CONTROL_TASK_NAME = "TripServerControlServer"
ADMIN_FAILSAFE_STATUS_PATH = os.path.join(BASE_DIR, "admin_failsafe_status.json")
PRESENTATION_MUSIC_UPLOAD_STATUS_PATH = os.path.join(BASE_DIR, "presentation_music_upload_status.json")
# Local File Manager roots are discovered at runtime on the Windows server.
# Access is intentionally limited to discovered Downloads folders and TripServer folders.
LOCAL_FILE_ROOTS_CACHE = None
LOCAL_FILE_ROOTS_CACHE_TS = 0
LOCAL_FILE_ALLOWED_ROOT_NAMES = {"downloads", "tripserver"}
LOCAL_FILE_TEXT_EXTENSIONS = {".txt", ".log", ".json", ".md", ".py", ".html", ".htm", ".css", ".js", ".csv", ".bat", ".ps1", ".xml"}
LOCAL_FILE_PREVIEW_MAX_BYTES = 1024 * 1024
LOCAL_FILE_ZIP_MAX_BYTES = 120 * 1024 * 1024
LOCAL_FILE_LIST_LIMIT = 700
FOLDERS = {
"JAPAN": os.path.join(BASE_DIR, "JAPAN"),
"USA": os.path.join(BASE_DIR, "USA"),
"Moneyapp": os.path.join(BASE_DIR, "Moneyapp"),
"MoneyHome": os.path.join(BASE_DIR, "MoneyHome"),
"TripForm": os.path.join(BASE_DIR, "TripForm"),
}
APP_FOLDERS = {"JAPAN", "USA", "Moneyapp", "MoneyHome", "TripForm"}
JAPAN_PRESENTATION_FILES = {
"flight_intro": "japan-flight-intro.html",
"japan_experience": "japan-experience.html",
}
USA_PRESENTATION_FILES = {
"usa_presentation": "usa2027-presentation.html",
"usa_presentation_tv": "usa2027-presentation-tv.html",
}
USA_EXTRA_HTML_FILES = {
"usa_trip_tv": "usa2027-tv.html",
}
USA_MUSIC_FILES = {
"usa_music": os.path.join("media", "usa-presentation", "music", "usa-roadtrip-theme.mp3"),
"usa_music_part2": os.path.join("media", "usa-presentation", "music", "usa-part2-theme.mp3"),
}
USA_IMAGE_KEYS = ({"intro", "budget", "roadtrip-map", "summary", "extra-big-sur", "extra-diner", "extra-death-valley", "extra-mono-lake"} | {f"day-{i:02d}" for i in range(1, 37)})
USA_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tif", ".tiff"}
JAPAN_MUSIC_TARGETS = {
# The Japan experience presentation contains several fallback paths. Keep all aliases synced.
"japan_experience_music": [
os.path.join("media", "japan-experience", "music", "japan-theme.mp3"),
os.path.join("media", "music", "japan-theme.mp3"),
"japan-theme.mp3",
],
"japan_flight_music": [
os.path.join("media", "japan-flight", "music", "flight-intro.mp3"),
],
}
# Presentation music sync rules.
# v34.14 keeps existing music upload routes as the single source of truth.
# Music buttons in the UI now upload the file, measure it, and synchronize the relevant presentation section.
# Japan main song syncs all japan-experience slides. USA part 1/2 sync only their own PART_*_SECONDS values.
PRESENTATION_MUSIC_SYNC_RULES = {
"japan_experience_music": {
"label": "יפן קיץ 26 — שיר מצגת ראשית",
"trip": "JAPAN",
"presentation": "japan-experience.html",
"music_targets": JAPAN_MUSIC_TARGETS["japan_experience_music"],
"scope": "כל שקפי מצגת יפן",
"future_behavior": "פעיל כעת: אחרי העלאת שיר מצגת יפן, המערכת מודדת את אורך השיר ומסנכרנת את כל שקפי japan-experience.html.",
"enabled_now": True,
},
"japan_flight_music": {
"label": "יפן קיץ 26 — מוזיקת פתיח טיסה",
"trip": "JAPAN",
"presentation": "japan-flight-intro.html",
"music_targets": JAPAN_MUSIC_TARGETS["japan_flight_music"],
"scope": "פתיח הטיסה בלבד",
"future_behavior": "העלאת שיר תישמר לפתיח הטיסה בלבד; לא מסנכרנת את מצגת יפן הראשית.",
"enabled_now": False,
},
"usa_music_part1": {
"label": "ארצות הברית קיץ 27 — שיר חלק 1",
"trip": "USA",
"presentation": "usa2027-presentation.html / usa2027-presentation-tv.html",
"music_targets": [USA_MUSIC_FILES["usa_music"]],
"scope": "שקפים 1–23 בלבד",
"future_behavior": "פעיל כעת: העלאת שיר חלק 1 מסנכרנת רק את מקטע 1 ולא משנה את מקטע 2.",
"enabled_now": True,
},
"usa_music_part2": {
"label": "ארצות הברית קיץ 27 — שיר חלק 2",
"trip": "USA",
"presentation": "usa2027-presentation.html / usa2027-presentation-tv.html",
"music_targets": [USA_MUSIC_FILES["usa_music_part2"]],
"scope": "שקפים 24–44 בלבד",
"future_behavior": "פעיל כעת: העלאת שיר חלק 2 מסנכרנת רק את מקטע 2 ולא משנה את מקטע 1.",
"enabled_now": True,
},
}
AUDIO_UPLOAD_EXTENSIONS = {".mp3", ".wav", ".m4a", ".aac", ".flac", ".ogg", ".opus", ".wma", ".webm", ".mp4"}
JAPAN_IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tif", ".tiff"}
JAPAN_IMAGES_ZIP_MAX_BYTES = 130 * 1024 * 1024
JAPAN_IMAGES_ZIP_MAX_FILES = 220
JAPAN_IMAGES_ZIP_MAX_UNCOMPRESSED_BYTES = 220 * 1024 * 1024
JAPAN_IMAGES_ZIP_MAX_SINGLE_FILE_BYTES = 18 * 1024 * 1024
TRIP_PACKAGE_ZIP_MAX_BYTES = 180 * 1024 * 1024
TRIP_PACKAGE_ZIP_MAX_FILES = 320
TRIP_PACKAGE_ZIP_MAX_UNCOMPRESSED_BYTES = 260 * 1024 * 1024
TRIP_PACKAGE_ZIP_MAX_SINGLE_FILE_BYTES = 35 * 1024 * 1024
TRIP_PACKAGE_EXTENSIONS = {".html", ".htm", ".jpg", ".jpeg", ".png", ".webp", ".mp3", ".css", ".js", ".json", ".txt", ".svg"}
ADMIN_SERVER_PATH = os.path.abspath(__file__)
ADMIN_BACKUP_DIR = os.path.join(BASE_DIR, "backups", "admin_server")
ADMIN_UPLOAD_TMP_DIR = os.path.join(BASE_DIR, "tmp", "admin_server_uploads")
ADMIN_MAX_UPLOAD_BYTES = 2 * 1024 * 1024
ADMIN_MAX_PACKAGE_UPLOAD_BYTES = 8 * 1024 * 1024
USA_IMAGES_ZIP_MAX_BYTES = 80 * 1024 * 1024
USA_IMAGES_ZIP_MAX_FILES = 160
USA_IMAGES_ZIP_MAX_UNCOMPRESSED_BYTES = 140 * 1024 * 1024
USA_IMAGES_ZIP_MAX_SINGLE_FILE_BYTES = 12 * 1024 * 1024
ADMIN_RESTART_CMD_PATH = os.path.join(BASE_DIR, "restart_admin_server.cmd")
ADMIN_RESTART_LOG_PATH = os.path.join(BASE_DIR, "admin_restart.log")
ADMIN_RESTART_HELPER_PATH = os.path.join(BASE_DIR, "restart_admin_server_helper.py")
UPLOAD_BACKUP_DIR = os.path.join(BASE_DIR, "backups", "uploaded_files")
# External watchdog. This is intentionally separate from the Flask process: if the admin
# server is down, Windows Task Scheduler can still run this script every minute and bring
# the admin back without walking to the physical computer.
WATCHDOG_TASK_NAME = os.environ.get("TRIPSERVER_WATCHDOG_TASK", "TripServerAdminWatchdog")
WATCHDOG_SCRIPT_PATH = os.path.join(BASE_DIR, "tripserver_admin_watchdog.py")
WATCHDOG_LOG_PATH = os.path.join(BASE_DIR, "tripserver_watchdog.log")
WATCHDOG_STATE_PATH = os.path.join(BASE_DIR, "tripserver_watchdog_state.txt")
WATCHDOG_INTERVAL_MINUTES = int(os.environ.get("TRIPSERVER_WATCHDOG_INTERVAL_MINUTES", "5"))
WATCHDOG_MIN_RESTART_INTERVAL_SECONDS = int(os.environ.get("TRIPSERVER_WATCHDOG_RESTART_COOLDOWN", "90"))
# v27: self-update/restart is launched through Windows Task Scheduler, not as a
# direct child of the running Flask process. This prevents the old admin process
# from killing the helper that is supposed to bring the new admin back online.
RESTART_TASK_NAME = os.environ.get("TRIPSERVER_RESTART_TASK", "TripServerAdminRestartOnce")
def get_pythonw_executable(preferred=None):
"""Return a no-console Python executable for Windows scheduled tasks when available.
Running the watchdog every few minutes with python.exe can flash a black console
window and steal focus. pythonw.exe runs the same script without a console.
On non-Windows or if pythonw.exe is not available, fall back to the normal
interpreter.
"""
exe = preferred or sys.executable or "python"
if os.name != "nt":
return exe
try:
base = os.path.basename(exe).lower()
folder = os.path.dirname(exe)
if base == "pythonw.exe" and os.path.exists(exe):
return exe
if folder:
candidate = os.path.join(folder, "pythonw.exe")
if os.path.exists(candidate):
return candidate
# Last-resort common launcher name. Do not validate here because it may be in PATH.
return "pythonw.exe"
except Exception:
return exe
# Admin log viewer. Only these fixed paths are exposed in the UI; user-supplied paths are never accepted.
LOG_FILE_REGISTRY = {
"restart": {"label": "לוג אתחול פאנל", "path": ADMIN_RESTART_LOG_PATH, "note": "Restart helper, העלאת admin חדש, שחזור גיבוי ופעולות מחשב."},
"control": {"label": "לוג שרת בקרה", "path": CONTROL_LOG_PATH, "note": "שרת הבקרה החיצוני שמפעיל ומעדכן את האדמין הראשי."},
"control_bootstrap": {"label": "לוג התקנת שרת בקרה", "path": CONTROL_BOOTSTRAP_LOG_PATH, "note": "כתיבת והפעלת שרת הבקרה."},
"watchdog": {"label": "לוג legacy", "path": WATCHDOG_LOG_PATH, "note": "שאריות לוג ממנגנונים ישנים, אם קיימות. לא חלק מהפאנל הראשי."},
}
LOG_TAIL_DEFAULT_LINES = 120
LOG_TAIL_MAX_LINES = 500
# Smart media optimizer settings.
# Images uploaded to fixed slide slots are always stored as real .jpg files.
# Audio uploaded to fixed music slots is always stored as .mp3 when FFmpeg is available.
IMAGE_OPTIMIZER_MAX_LONG_EDGE = int(os.environ.get("TRIPSERVER_IMAGE_MAX_LONG_EDGE", "1920"))
IMAGE_OPTIMIZER_TARGET_BYTES = int(os.environ.get("TRIPSERVER_IMAGE_TARGET_BYTES", str(1100 * 1024)))
IMAGE_OPTIMIZER_QUALITIES = tuple(int(q) for q in os.environ.get("TRIPSERVER_IMAGE_QUALITIES", "88,86,84,82,80,78").split(",") if q.strip())
AUDIO_UPLOAD_MAX_BYTES = int(os.environ.get("TRIPSERVER_AUDIO_MAX_BYTES", str(80 * 1024 * 1024)))
AUDIO_MP3_BITRATE = os.environ.get("TRIPSERVER_AUDIO_MP3_BITRATE", "192k")
AUDIO_MP3_SAMPLE_RATE = os.environ.get("TRIPSERVER_AUDIO_MP3_SAMPLE_RATE", "44100")
MEDIA_AUDIO_ACCEPT = ".mp3,.wav,.m4a,.aac,.flac,.ogg,.opus,.wma,.webm,.mp4,audio/*,video/mp4"
MEDIA_IMAGE_ACCEPT = ".jpg,.jpeg,.png,.webp,.bmp,.tif,.tiff,image/*"
# Power actions are intentionally guarded: POST only, LAN/local client only, typed Hebrew confirmation, and cancel support.
POWER_ACTIONS_ENABLED = os.environ.get("TRIPSERVER_ENABLE_POWER_ACTIONS", "1").strip().lower() not in {"0", "false", "no", "off"}
POWER_MIN_DELAY_SECONDS = 30
POWER_MAX_DELAY_SECONDS = 3600
POWER_DEFAULT_DELAY_SECONDS = 60
POWER_CONFIRM_TEXT = {
"shutdown": "כיבוי",
"reboot": "הפעלה מחדש",
}
POWER_LABELS = {
"shutdown": "כיבוי מחשב",
"reboot": "ריסטארט למחשב",
}
ADMIN_UPDATE_FAILSAFE_RESTART_SECONDS = int(os.environ.get("TRIPSERVER_ADMIN_UPDATE_FAILSAFE_RESTART_SECONDS", "180"))
ADMIN_UPDATE_FAILSAFE_ENABLED = os.environ.get("TRIPSERVER_ADMIN_UPDATE_FAILSAFE", "1").strip().lower() not in {"0", "false", "no", "off"}
def _json_safe_write(path, payload):
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
tmp = path + ".tmp"
with open(tmp, "w", encoding="utf-8") as fh:
json.dump(payload, fh, ensure_ascii=False, indent=2)
os.replace(tmp, path)
return True
except Exception:
return False
def read_admin_failsafe_status():
try:
if os.path.exists(ADMIN_FAILSAFE_STATUS_PATH):
with open(ADMIN_FAILSAFE_STATUS_PATH, "r", encoding="utf-8") as fh:
data = json.load(fh)
if isinstance(data, dict):
return data
except Exception:
pass
return {"state": "unknown", "message": "עדיין אין נתוני Fail Safe שמורים."}
def write_admin_failsafe_status(state, message, **extra):
payload = {
"state": state,
"message": message,
"updated_at": time.strftime("%Y-%m-%d %H:%M:%S"),
"admin_version": ADMIN_PANEL_VERSION,
}
payload.update(extra)
_json_safe_write(ADMIN_FAILSAFE_STATUS_PATH, payload)
return payload
def is_no_pending_shutdown_message(output):
txt = (output or "").lower()
return ("1116" in txt) or ("no shutdown was in progress" in txt) or ("אין" in txt and "כיבוי" in txt)
def render_failsafe_status_card():
data = read_admin_failsafe_status()
state = str(data.get("state") or "unknown")
message = str(data.get("message") or "")
updated = str(data.get("updated_at") or "")
if state in {"armed"}:
icon = "🟠"
title = "Fail Safe חמוש"
elif state in {"cancelled", "auto_cancelled", "none"}:
icon = "🟢"
title = "Fail Safe לא פעיל"
elif state in {"arm_failed", "cancel_failed"}:
icon = "🔴"
title = "Fail Safe דורש בדיקה"
else:
icon = "⚪"
title = "Fail Safe ללא סטטוס"
safe_msg = html_lib.escape(message or "אין ריסטארט פעיל כרגע או שעדיין אין בדיקת סטטוס.")
safe_updated = html_lib.escape(updated)
return '<div class="status-card"><b>%s %s</b><span>%s<br><small>%s</small></span></div>' % (icon, title, safe_msg, safe_updated)
# Webcam / room camera control. The camera is never opened automatically on page load.
# It starts only when the user presses the camera button, and it is guarded to LAN/Tailscale/private IPs.
CAMERA_ENABLED = os.environ.get("TRIPSERVER_ENABLE_WEBCAM", "1").strip().lower() not in {"0", "false", "no", "off"}
CAMERA_INDEX = int(os.environ.get("TRIPSERVER_WEBCAM_INDEX", "0"))
CAMERA_WIDTH = int(os.environ.get("TRIPSERVER_WEBCAM_WIDTH", "1280"))
CAMERA_HEIGHT = int(os.environ.get("TRIPSERVER_WEBCAM_HEIGHT", "720"))
CAMERA_FPS = max(1, min(20, int(os.environ.get("TRIPSERVER_WEBCAM_FPS", "8"))))
CAMERA_JPEG_QUALITY = max(45, min(92, int(os.environ.get("TRIPSERVER_WEBCAM_JPEG_QUALITY", "78"))))
CAMERA_MAX_STREAM_SECONDS = int(os.environ.get("TRIPSERVER_WEBCAM_MAX_STREAM_SECONDS", "1800"))
CAMERA_FRAME_INTERVAL_SECONDS = 1.0 / float(CAMERA_FPS)
# Optional audio + recording layer. Audio and recording are manual-only and guarded by the same
# LAN/Tailscale/private-IP checks as the live camera. FFmpeg is required for audio and MP4 recording.
CAMERA_AUDIO_ENABLED = os.environ.get("TRIPSERVER_ENABLE_CAMERA_AUDIO", "1").strip().lower() not in {"0", "false", "no", "off"}
CAMERA_RECORDING_ENABLED = os.environ.get("TRIPSERVER_ENABLE_CAMERA_RECORDING", "1").strip().lower() not in {"0", "false", "no", "off"}
CAMERA_VIDEO_DEVICE = os.environ.get("TRIPSERVER_FFMPEG_VIDEO_DEVICE", "").strip()
CAMERA_AUDIO_DEVICE = os.environ.get("TRIPSERVER_FFMPEG_AUDIO_DEVICE", "").strip()
CAMERA_RECORDINGS_DIR = os.environ.get("TRIPSERVER_CAMERA_RECORDINGS_DIR", os.path.join(BASE_DIR, "camera_recordings"))
CAMERA_RECORD_FPS = max(1, min(30, int(os.environ.get("TRIPSERVER_CAMERA_RECORD_FPS", "15"))))
CAMERA_RECORD_MAX_SECONDS = max(30, min(8 * 3600, int(os.environ.get("TRIPSERVER_CAMERA_RECORD_MAX_SECONDS", "3600"))))
CAMERA_AUDIO_BITRATE = os.environ.get("TRIPSERVER_CAMERA_AUDIO_BITRATE", "128k")
# Camera component installer. First-run auto installer: if OpenCV is missing, the admin
# starts a background pip installation automatically when the Flask process starts.
# It uses the same Python executable that runs this admin server and writes a log.
# The camera itself still never turns on automatically; only the dependency installer runs.
CAMERA_SETUP_LOG_PATH = os.path.join(BASE_DIR, "camera_setup.log")
CAMERA_AUDIO_LOG_PATH = os.path.join(BASE_DIR, "camera_audio_stream.log")
CAMERA_HLS_DIR = os.path.join(BASE_DIR, "camera_live_hls")
CAMERA_HLS_LOG_PATH = os.path.join(BASE_DIR, "camera_hls_stream.log")
CAMERA_SETUP_CONFIRM_TEXT = "התקן"
CAMERA_OPENCV_PACKAGE = os.environ.get("TRIPSERVER_OPENCV_PACKAGE", "opencv-python").strip() or "opencv-python"
CAMERA_OPENCV_INSTALL_TIMEOUT_SECONDS = int(os.environ.get("TRIPSERVER_OPENCV_INSTALL_TIMEOUT_SECONDS", "900"))
CAMERA_AUTO_INSTALL_OPENCV = os.environ.get("TRIPSERVER_CAMERA_AUTO_INSTALL_OPENCV", "1").strip().lower() not in {"0", "false", "no", "off"}
CAMERA_AUTO_INSTALL_DELAY_SECONDS = int(os.environ.get("TRIPSERVER_CAMERA_AUTO_INSTALL_DELAY_SECONDS", "4"))
CAMERA_SETUP_MARKER_PATH = os.path.join(BASE_DIR, "camera_opencv_installed.ok")
CAMERA_SETUP_STATE = {"running": False, "started_at": "", "finished_at": "", "ok": False, "last_message": "", "mode": "idle", "auto_requested": False}
CAMERA_SETUP_LOCK = threading.RLock()
LOG_FILE_REGISTRY["camera_setup"] = {"label": "לוג התקנת רכיבי מצלמה", "path": CAMERA_SETUP_LOG_PATH, "note": "התקנת OpenCV דרך pip ובדיקת רכיבי מצלמה."}
LOG_FILE_REGISTRY["camera_audio"] = {"label": "לוג אודיו מצלמה", "path": CAMERA_AUDIO_LOG_PATH, "note": "FFmpeg DirectShow, בחירת מיקרופון וניסיונות הפעלת אודיו."}
LOG_FILE_REGISTRY["camera_hls"] = {"label": "לוג שידור מצלמה מאוחד HLS", "path": CAMERA_HLS_LOG_PATH, "note": "FFmpeg DirectShow וידאו+אודיו בתהליך אחד, כדי למנוע נעילת התקני USB."}
def safe_zip_relpath(raw_name):
raw = (raw_name or "").replace("\\", "/").strip("/")
if not raw or raw.startswith("__MACOSX/"):
return None
parts = [p for p in raw.split("/") if p and p not in (".", "..")]
if not parts or len(parts) != len(raw.split("/")):
return None
return "/".join(parts)
def file_ext(filename):
return os.path.splitext(filename or "")[1].lower()
def is_likely_jpeg_file(path):
try:
with open(path, "rb") as fh:
return fh.read(3) == b"\xff\xd8\xff"
except Exception:
return False
def is_likely_mp3_file(path):
"""Fast MP3 sniffing: ID3 tag or MPEG audio frame sync near the beginning."""
try:
with open(path, "rb") as fh:
data = fh.read(4096)
if data.startswith(b"ID3"):
return True
for idx in range(0, max(0, len(data) - 1)):
if data[idx] == 0xFF and (data[idx + 1] & 0xE0) == 0xE0:
return True
return False
except Exception:
return False
def find_ffmpeg_executable():
candidates = [
os.environ.get("FFMPEG_PATH"),
"ffmpeg",
r"C:\ffmpeg\bin\ffmpeg.exe",
r"C:\Program Files\ffmpeg\bin\ffmpeg.exe",
r"C:\Program Files (x86)\ffmpeg\bin\ffmpeg.exe",
]
for candidate in candidates:
if not candidate:
continue
found = shutil.which(candidate) if os.path.basename(candidate) == candidate else (candidate if os.path.exists(candidate) else None)
if found:
return found
return None
def find_ffprobe_executable():
candidates = [
os.environ.get("FFPROBE_PATH"),
"ffprobe",
r"C:\ffmpeg\bin\ffprobe.exe",
r"C:\Program Files\ffmpeg\bin\ffprobe.exe",
r"C:\Program Files (x86)\ffmpeg\bin\ffprobe.exe",
]
for candidate in candidates:
if not candidate:
continue
found = shutil.which(candidate) if os.path.basename(candidate) == candidate else (candidate if os.path.exists(candidate) else None)
if found:
return found
return None
def run_hidden_subprocess(cmd, timeout=20):
kwargs = dict(capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=timeout, shell=False)
if os.name == "nt":
kwargs["creationflags"] = getattr(subprocess, "CREATE_NO_WINDOW", 0)
return subprocess.run(cmd, **kwargs)
def format_seconds(value):
try:
total = int(round(float(value)))
minutes, seconds = divmod(total, 60)
hours, minutes = divmod(minutes, 60)
if hours:
return f"{hours}:{minutes:02d}:{seconds:02d}"
return f"{minutes}:{seconds:02d}"
except Exception:
return str(value)
def probe_audio_duration_seconds(path):
ffprobe = find_ffprobe_executable()
if not ffprobe:
return {"ok": False, "error": r"ffprobe לא נמצא. ודא שקיים C:\ffmpeg\bin\ffprobe.exe", "ffprobe": ""}
cmd = [
ffprobe, "-v", "error",
"-show_entries", "format=duration",
"-of", "default=noprint_wrappers=1:nokey=1",
path,
]
try:
cp = run_hidden_subprocess(cmd, timeout=20)
raw = ((cp.stdout or "") + "\n" + (cp.stderr or "")).strip()
if cp.returncode != 0:
return {"ok": False, "error": raw or f"ffprobe failed rc={cp.returncode}", "return_code": cp.returncode, "ffprobe": ffprobe}
value = None
for line in raw.splitlines():
line = line.strip()
if not line:
continue
try:
value = float(line)
break
except Exception:
pass
if value is None or value <= 0:
return {"ok": False, "error": "ffprobe לא החזיר משך שיר תקין", "raw": raw, "ffprobe": ffprobe}
return {"ok": True, "duration_seconds": value, "duration_display": format_seconds(value), "raw": raw, "ffprobe": ffprobe}
except subprocess.TimeoutExpired:
return {"ok": False, "error": "ffprobe חרג מזמן הבדיקה", "ffprobe": ffprobe}
except Exception as exc:
return {"ok": False, "error": repr(exc), "ffprobe": ffprobe}
def music_key_to_rule_key(trip, music_key):
trip = (trip or "").upper()
if trip == "JAPAN":
return music_key if music_key in PRESENTATION_MUSIC_SYNC_RULES else None
if trip == "USA":
if music_key == "usa_music":
return "usa_music_part1"
if music_key == "usa_music_part2":
return "usa_music_part2"
return None
def read_presentation_music_upload_status():
try:
if os.path.exists(PRESENTATION_MUSIC_UPLOAD_STATUS_PATH):
with open(PRESENTATION_MUSIC_UPLOAD_STATUS_PATH, "r", encoding="utf-8") as f:
data = json.load(f)
return data if isinstance(data, dict) else {}
except Exception:
return {}
return {}
def write_presentation_music_upload_status(rule_key, status):
data = read_presentation_music_upload_status()
data[rule_key or "unknown"] = status
history = data.get("_history")
if not isinstance(history, list):
history = []
history.insert(0, status)
data["_history"] = history[:30]
ensure_parent_dir(PRESENTATION_MUSIC_UPLOAD_STATUS_PATH)
tmp = PRESENTATION_MUSIC_UPLOAD_STATUS_PATH + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
os.replace(tmp, PRESENTATION_MUSIC_UPLOAD_STATUS_PATH)
def primary_saved_audio_path(save_result):
if not isinstance(save_result, dict):
return None
if save_result.get("path"):
return save_result.get("path")
targets = save_result.get("targets")
if isinstance(targets, list) and targets:
first = targets[0]
if isinstance(first, dict):
return first.get("path")
return None
def all_saved_audio_paths(save_result):
if not isinstance(save_result, dict):
return []
if save_result.get("path"):
return [save_result.get("path")]
targets = save_result.get("targets")
if isinstance(targets, list):
return [item.get("path") for item in targets if isinstance(item, dict) and item.get("path")]
return []
def read_text_file_utf8(path):
with open(path, "r", encoding="utf-8", errors="ignore") as f:
return f.read()
def write_text_file_utf8_atomic_with_backup(path, new_text, backup_group):
if not os.path.exists(path):
raise FileNotFoundError(path)
backup_path = backup_existing_file(path, backup_group)
tmp_path = path + ".tmp"
with open(tmp_path, "w", encoding="utf-8", newline="") as f:
f.write(new_text)
os.replace(tmp_path, path)
return backup_path
def calculate_scaled_slide_durations_ms(old_durations, target_total_ms):
if not old_durations:
raise ValueError("לא נמצאו זמני שקפים לחישוב.")
target_total_ms = int(round(float(target_total_ms)))
if target_total_ms <= 0:
raise ValueError("משך יעד לא תקין לסנכרון שקפים.")
current_total = sum(int(x) for x in old_durations)
if current_total <= 0:
raise ValueError("סך זמני השקפים הנוכחי אינו תקין.")
count = len(old_durations)
minimum = 1200 if target_total_ms >= count * 1200 else max(250, target_total_ms // max(1, count))
scale = target_total_ms / float(current_total)
new_durations = [max(minimum, int(round(int(x) * scale))) for x in old_durations]
diff = target_total_ms - sum(new_durations)
if diff > 0:
new_durations[-1] += diff
elif diff < 0:
remaining = -diff
for i in range(len(new_durations) - 1, -1, -1):
room = max(0, new_durations[i] - minimum)
if room <= 0:
continue
take = min(room, remaining)
new_durations[i] -= take
remaining -= take
if remaining <= 0:
break
if remaining > 0:
# Last-resort exact correction when the track is extremely short.
new_durations[-1] = max(1, new_durations[-1] - remaining)
if sum(new_durations) != target_total_ms:
new_durations[-1] += target_total_ms - sum(new_durations)
if any(int(x) <= 0 for x in new_durations):
raise ValueError("חישוב זמני השקפים יצר ערך לא תקין.")
return new_durations
def sync_japan_experience_presentation_to_music_duration(duration_seconds):
"""Synchronize japan-experience.html only. No FFmpeg/ffprobe is executed here; duration must already be measured."""
path = os.path.join(FOLDERS["JAPAN"], JAPAN_PRESENTATION_FILES["japan_experience"])
if not os.path.exists(path):
raise FileNotFoundError(path)
html = read_text_file_utf8(path)
match = re.search(r"const\s+SLIDES\s*=\s*(\[.*?\]);", html, flags=re.S)
if not match:
raise ValueError("לא נמצא מערך SLIDES במצגת יפן. אסור לנחש זמני שקפים.")
block = match.group(1)
duration_matches = list(re.finditer(r'"duration"\s*:\s*(\d+)', block))
if not duration_matches:
raise ValueError("לא נמצאו ערכי duration במערך SLIDES של מצגת יפן.")
old_durations = [int(m.group(1)) for m in duration_matches]
target_total_ms = int(round(float(duration_seconds) * 1000))
new_durations = calculate_scaled_slide_durations_ms(old_durations, target_total_ms)
index = {"value": 0}
def replace_duration(m):
i = index["value"]
index["value"] += 1
return '"duration":' + str(new_durations[i])
new_block = re.sub(r'"duration"\s*:\s*\d+', replace_duration, block)
stamp = (
f"// TripServer music sync v34.14: japan-experience.html סונכרן לפי שיר באורך "
f"{float(duration_seconds):.6f} שניות; שקפים: {len(new_durations)}; סך חדש: {sum(new_durations)/1000:.3f} שניות."
)
new_html = html[:match.start(1)] + new_block + html[match.end(1):]
if "// TripServer music sync v34.14: japan-experience.html" in new_html:
new_html = re.sub(r"// TripServer music sync v34\.14: japan-experience\.html[^\n]*\n", stamp + "\n", new_html, count=1)
elif "// TripServer music sync:" in new_html:
new_html = re.sub(r"// TripServer music sync:[^\n]*\n", stamp + "\n", new_html, count=1)
else:
new_html = new_html[:match.start()] + stamp + "\n" + new_html[match.start():]
if new_html == html:
raise ValueError("לא בוצע שינוי בפועל במצגת יפן.")
backup_path = write_text_file_utf8_atomic_with_backup(path, new_html, "JAPAN_presentation_music_sync")
return {
"ok": True,
"path": path,
"backup": backup_path,
"sha256": file_sha256(path),
"slides": len(new_durations),
"old_total_seconds": sum(old_durations) / 1000.0,
"new_total_seconds": sum(new_durations) / 1000.0,
"duration_seconds": float(duration_seconds),
"message": f"מצגת יפן סונכרנה לפי שיר באורך {format_seconds(duration_seconds)} ({float(duration_seconds):.3f} שניות).",
}
def sync_usa_presentation_part_to_music_duration(music_key, duration_seconds):
"""Synchronize USA presentation timing for one song part only.
The USA presentation uses two constants, not per-slide duration fields:
- PART_1_SECONDS controls slides 1-23.
- PART_2_SECONDS controls slides 24-44.
Updating one constant is the safe and intended synchronization for that song.
Both regular and TV presentation variants are updated when present.
No FFmpeg/ffprobe is executed here; duration must already be measured.
"""
key = music_key_to_rule_key("USA", music_key) or music_key
if key == "usa_music_part1":
const_name = "PART_1_SECONDS"
label = "ארה״ב חלק 1"
scope = "שקפים 1–23"
elif key == "usa_music_part2":
const_name = "PART_2_SECONDS"
label = "ארה״ב חלק 2"
scope = "שקפים 24–44"
else:
raise ValueError("סוג שיר ארה״ב לא מוכר לסנכרון.")
seconds = float(duration_seconds)
if seconds <= 0:
raise ValueError("משך שיר לא תקין לסנכרון ארה״ב.")
seconds_text = f"{seconds:.6f}"
results = []
for presentation_key in ("usa_presentation", "usa_presentation_tv"):
filename = USA_PRESENTATION_FILES.get(presentation_key)
if not filename:
continue
path = os.path.join(FOLDERS["USA"], filename)
if not os.path.exists(path):
continue
html = read_text_file_utf8(path)
pattern = re.compile(r"(const\s+" + re.escape(const_name) + r"\s*=\s*)([0-9]+(?:\.[0-9]+)?)(\s*;)")
match = pattern.search(html)
if not match:
raise ValueError(f"לא נמצא {const_name} בתוך {filename}. אסור לנחש זמני מצגת.")
old_value = float(match.group(2))
new_html = pattern.sub(r"\g<1>" + seconds_text + r"\g<3>", html, count=1)
# Keep the nearby Hebrew comment aligned when it exists. This is cosmetic but prevents future confusion.
if const_name == "PART_1_SECONDS":
new_html = re.sub(r"//\s*שקפים\s*1-23:[^\n]*", f"// שקפים 1-23: usa-roadtrip-theme, {seconds_text} שניות.", new_html, count=1)
else:
new_html = re.sub(r"//\s*שקפים\s*24-44:[^\n]*", f"// שקפים 24-44: usa-part2-theme, {seconds_text} שניות.", new_html, count=1)
stamp = f"// TripServer music sync v34.14: {label} ({scope}) סונכרן לפי שיר באורך {seconds_text} שניות."
if "// TripServer music sync v34.14:" in new_html:
# Do not remove previous part's stamp; only replace same part if it exists.
part_stamp_re = re.compile(r"// TripServer music sync v34\.14: " + re.escape(label) + r"[^\n]*\n")
if part_stamp_re.search(new_html):
new_html = part_stamp_re.sub(stamp + "\n", new_html, count=1)
else:
new_html = new_html[:match.start()] + stamp + "\n" + new_html[match.start():]
elif "// TripServer music sync:" in new_html:
# Legacy stamp from older experiments: add new clear stamp instead of deleting history.
new_html = new_html[:match.start()] + stamp + "\n" + new_html[match.start():]
else:
new_html = new_html[:match.start()] + stamp + "\n" + new_html[match.start():]
if new_html == html:
raise ValueError(f"לא בוצע שינוי בפועל ב־{filename}.")
backup_path = write_text_file_utf8_atomic_with_backup(path, new_html, "USA_presentation_music_sync")
results.append({
"ok": True,
"path": path,
"backup": backup_path,
"sha256": file_sha256(path),
"presentation_key": presentation_key,
"filename": filename,
"part": key,
"const_name": const_name,
"scope": scope,
"old_seconds": old_value,
"new_seconds": seconds,
"duration_seconds": seconds,
"message": f"{filename}: {const_name} עודכן מ־{old_value:.6f} ל־{seconds_text} שניות.",
})
if not results:
raise FileNotFoundError("לא נמצאה מצגת ארה״ב לסנכרון: usa2027-presentation.html או usa2027-presentation-tv.html")
return {
"ok": True,
"part": key,
"label": label,
"scope": scope,
"duration_seconds": seconds,
"results": results,
"message": f"{label} סונכרן לפי שיר באורך {format_seconds(seconds)} ({seconds:.3f} שניות).",
}
def register_existing_music_upload_duration(trip, music_key, save_result):
rule_key = music_key_to_rule_key(trip, music_key)
rule = PRESENTATION_MUSIC_SYNC_RULES.get(rule_key or "", {})
path = primary_saved_audio_path(save_result)
paths = all_saved_audio_paths(save_result)
status = {
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"trip": (trip or "").upper(),
"music_key": music_key,
"rule_key": rule_key or "",
"label": rule.get("label", ""),
"scope": rule.get("scope", ""),
"presentation": rule.get("presentation", ""),
"paths": paths,
"primary_path": path or "",
"duration_ok": False,
"duration_seconds": None,
"duration_display": "",
"sync_performed": False,
"sync_note": "v34.14: העלאת שיר קיימת מודדת את האורך ומסנכרנת את המצגת/המקטע הרלוונטיים כאשר כלל הסנכרון פעיל.",
}
if not path or not os.path.exists(path):
status["error"] = "קובץ השיר נשמר אך לא נמצא נתיב קובץ למדידת אורך."
if rule_key:
write_presentation_music_upload_status(rule_key, status)
return status
probe = probe_audio_duration_seconds(path)
status["ffprobe"] = probe.get("ffprobe", "")
if probe.get("ok"):
status["duration_ok"] = True
status["duration_seconds"] = probe.get("duration_seconds")
status["duration_display"] = probe.get("duration_display")
try:
sync_result = None
if (trip or "").upper() == "JAPAN" and music_key == "japan_experience_music":
sync_result = sync_japan_experience_presentation_to_music_duration(probe.get("duration_seconds"))
status["sync_note"] = "בוצע סנכרון בפועל: כל שקפי מצגת יפן סונכרנו לפי אורך השיר החדש."
elif (trip or "").upper() == "USA" and rule_key in {"usa_music_part1", "usa_music_part2"}:
sync_result = sync_usa_presentation_part_to_music_duration(rule_key, probe.get("duration_seconds"))
status["sync_note"] = "בוצע סנכרון בפועל: רק מקטע השיר הרלוונטי במצגת ארה״ב עודכן."
elif (trip or "").upper() == "JAPAN" and music_key == "japan_flight_music":
status["sync_note"] = "מוזיקת פתיח הטיסה נשמרה ונמדדה. לא שונה japan-experience.html כי פתיח הטיסה הוא מקטע נפרד."
if sync_result:
status["sync_performed"] = True
if isinstance(sync_result, dict) and sync_result.get("results"):
status["sync_results"] = sync_result.get("results")
status["sync_summary"] = sync_result.get("message", "")
else:
status["sync_results"] = [sync_result]
status["sync_summary"] = sync_result.get("message", "") if isinstance(sync_result, dict) else ""
except Exception as sync_exc:
status["sync_performed"] = False
status["sync_error"] = repr(sync_exc)
status["sync_note"] = "השיר נשמר ונמדד, אך סנכרון המצגת נכשל. אין אישור שהמצגת מסונכרנת."
else:
status["error"] = probe.get("error") or probe.get("raw") or "ffprobe לא הצליח לזהות משך שיר."
if rule_key:
write_presentation_music_upload_status(rule_key, status)
return status
def music_upload_duration_details(status):
if not isinstance(status, dict):
return ""
lines = ["סטטוס אורך שיר:"]
if status.get("label"):
lines.append(f"שיר/מקטע: {status.get('label')}")
if status.get("scope"):
lines.append(f"תחום סנכרון עתידי: {status.get('scope')}")
if status.get("duration_ok"):
lines.append(f"משך שזוהה: {status.get('duration_display')} ({float(status.get('duration_seconds') or 0):.3f} שניות)")
if status.get("sync_performed"):
lines.append("סנכרון שקפים: בוצע בפועל למצגת יפן.")
for item in status.get("sync_results") or []:
if isinstance(item, dict):
lines.append(f"מצגת: {item.get('path','')}")
lines.append(f"שקפים: {item.get('slides','')} | סך חדש: {float(item.get('new_total_seconds') or 0):.3f} שניות")
if item.get("backup"):
lines.append(f"גיבוי מצגת קודם: {item.get('backup')}")
elif status.get("sync_error"):
lines.append("סנכרון שקפים: נכשל.")
lines.append(f"שגיאת סנכרון: {status.get('sync_error')}")
else:
lines.append("סנכרון שקפים: לא בוצע בשלב הזה.")
else:
lines.append("אזהרה: השיר נשמר, אבל לא זוהה משך תקין ולכן אין אישור לסנכרון.")
if status.get("error"):
lines.append(f"שגיאה: {status.get('error')}")
if status.get("ffprobe"):
lines.append(f"ffprobe: {status.get('ffprobe')}")
if status.get("primary_path"):
lines.append(f"נתיב שנבדק: {status.get('primary_path')}")
lines.append("קובץ סטטוס: " + PRESENTATION_MUSIC_UPLOAD_STATUS_PATH)
return "\n".join(lines)
def render_music_upload_status_rows():
data = read_presentation_music_upload_status()
rows = []
for key, rule in PRESENTATION_MUSIC_SYNC_RULES.items():
st = data.get(key) if isinstance(data, dict) else None
if isinstance(st, dict) and st.get("timestamp"):
if st.get("duration_ok"):
if st.get("sync_performed"):
badge = f"<span class='badge safe'>סונכרן: {html_lib.escape(str(st.get('duration_display') or ''))}</span>"
detail = "משך זוהה ומצגת יפן סונכרנה בפועל."
elif st.get("sync_error"):
badge = f"<span class='badge warn'>נמדד, סנכרון נכשל</span>"
detail = html_lib.escape(str(st.get("sync_error") or "שגיאת סנכרון"))
else:
badge = f"<span class='badge safe'>נמדד: {html_lib.escape(str(st.get('duration_display') or ''))}</span>"
detail = "משך זוהה. עדיין לא בוצע סנכרון שקפים."
else:
badge = "<span class='badge warn'>שגיאת מדידה</span>"
detail = html_lib.escape(str(st.get("error") or "לא זוהה משך"))
timestamp = html_lib.escape(str(st.get("timestamp") or ""))
else:
badge = "<span class='badge warn'>אין העלאה אחרונה</span>"
detail = "טרם נרשם סטטוס העלאה לשיר הזה."
timestamp = "—"
rows.append(f"<tr><td><b>{html_lib.escape(rule.get('label',''))}</b></td><td>{badge}</td><td>{timestamp}</td><td>{detail}</td></tr>")
return "".join(rows)
def media_engine_status():
ffmpeg_path = find_ffmpeg_executable() or ""
ffprobe_path = find_ffprobe_executable() or ""
status = {"pillow": False, "ffmpeg": bool(ffmpeg_path), "ffmpeg_path": ffmpeg_path, "ffprobe": bool(ffprobe_path), "ffprobe_path": ffprobe_path}
try:
import PIL # noqa: F401
status["pillow"] = True
except Exception:
status["pillow"] = False
return status
def format_bytes(num):
try:
num = int(num)
except Exception:
return str(num)
units = ["B", "KB", "MB", "GB"]
value = float(num)
for unit in units:
if value < 1024 or unit == units[-1]:
return f"{value:.1f} {unit}" if unit != "B" else f"{int(value)} B"
value /= 1024
def convert_or_copy_image_to_jpg(source_path, target_path, original_name=""):
"""Create a real optimized JPG file; never writes a fake .jpg.
Smart behavior:
- Uses Pillow when installed.
- Applies EXIF orientation.
- Converts alpha/transparency to a white background.
- Downscales only when the long edge is larger than IMAGE_OPTIMIZER_MAX_LONG_EDGE.
- Uses progressive/optimized JPEG and a quality loop to reduce weight without aggressive damage.
- If Pillow is missing, accepts only true JPEG bytes and copies them safely.
"""
ensure_parent_dir(target_path)
original_size = os.path.getsize(source_path) if os.path.exists(source_path) else 0
try:
from PIL import Image, ImageOps
with Image.open(source_path) as img:
img.verify()
with Image.open(source_path) as img:
img = ImageOps.exif_transpose(img)
original_dimensions = tuple(img.size)
if img.mode in ("RGBA", "LA") or (img.mode == "P" and "transparency" in img.info):
rgba = img.convert("RGBA")
background = Image.new("RGBA", rgba.size, (255, 255, 255, 255))
background.alpha_composite(rgba)
img = background.convert("RGB")
elif img.mode != "RGB":
img = img.convert("RGB")
resized = False
max_edge = max(img.size) if img.size else 0
if IMAGE_OPTIMIZER_MAX_LONG_EDGE and max_edge > IMAGE_OPTIMIZER_MAX_LONG_EDGE:
scale = IMAGE_OPTIMIZER_MAX_LONG_EDGE / float(max_edge)
new_size = (max(1, int(round(img.size[0] * scale))), max(1, int(round(img.size[1] * scale))))
img = img.resize(new_size, Image.Resampling.LANCZOS)
resized = True
tmp_candidates = []
qualities = IMAGE_OPTIMIZER_QUALITIES or (88, 84, 80)
for quality in qualities:
tmp_candidate = f"{target_path}.q{quality}.tmp"
img.save(tmp_candidate, "JPEG", quality=int(quality), optimize=True, progressive=True)
tmp_candidates.append((quality, tmp_candidate, os.path.getsize(tmp_candidate)))
if os.path.getsize(tmp_candidate) <= IMAGE_OPTIMIZER_TARGET_BYTES:
break
# Keep the highest-quality candidate that reaches target size; otherwise keep the smallest candidate.
under_target = [item for item in tmp_candidates if item[2] <= IMAGE_OPTIMIZER_TARGET_BYTES]
chosen = under_target[0] if under_target else min(tmp_candidates, key=lambda item: item[2])
for quality, path, _size in tmp_candidates:
if path != chosen[1]:
try:
os.remove(path)
except Exception:
pass
os.replace(chosen[1], target_path)
final_size = os.path.getsize(target_path)
return {
"mode": "optimized_jpg",
"quality": chosen[0],
"original_size": original_size,
"final_size": final_size,
"original_dimensions": original_dimensions,
"final_dimensions": tuple(img.size),
"resized": resized,
"saved_bytes": max(0, original_size - final_size),
}
except ImportError:
if is_likely_jpeg_file(source_path):
tmp_target = target_path + ".tmp"
shutil.copy2(source_path, tmp_target)
os.replace(tmp_target, target_path)
final_size = os.path.getsize(target_path)
return {
"mode": "copied_real_jpeg_without_pillow",
"quality": "source",
"original_size": original_size,
"final_size": final_size,
"original_dimensions": "unknown",
"final_dimensions": "unknown",
"resized": False,
"saved_bytes": max(0, original_size - final_size),
}
raise ValueError("Pillow is not installed, so non-JPG images cannot be converted. Install Pillow with: py -m pip install pillow")
except ValueError:
raise
except Exception as exc:
raise ValueError("Image conversion/compression failed: " + repr(exc))
def convert_or_copy_audio_to_mp3(source_path, target_path, original_name=""):
"""Store uploaded audio as a real .mp3 file.
If FFmpeg exists, transcode supported audio/video containers to MP3.
If FFmpeg is missing, accept only files that are already real MP3 bytes, even if their extension is wrong.
"""
ensure_parent_dir(target_path)
original_size = os.path.getsize(source_path) if os.path.exists(source_path) else 0
if original_size <= 0:
raise ValueError("קובץ האודיו ריק.")
if original_size > AUDIO_UPLOAD_MAX_BYTES:
raise ValueError(f"קובץ האודיו גדול מדי: {format_bytes(original_size)}. מגבלה: {format_bytes(AUDIO_UPLOAD_MAX_BYTES)}")
source_is_mp3 = is_likely_mp3_file(source_path)
ffmpeg = find_ffmpeg_executable()
if ffmpeg:
tmp_mp3 = target_path + ".ffmpeg.tmp.mp3"
cmd = [
ffmpeg,
"-y",
"-hide_banner",
"-loglevel", "error",
"-i", source_path,
"-vn",
"-codec:a", "libmp3lame",
"-b:a", AUDIO_MP3_BITRATE,
"-ar", AUDIO_MP3_SAMPLE_RATE,
tmp_mp3,
]
run = run_hidden_subprocess(cmd, timeout=180)
if run.returncode != 0 or not os.path.exists(tmp_mp3) or os.path.getsize(tmp_mp3) <= 0:
try:
if os.path.exists(tmp_mp3):
os.remove(tmp_mp3)
except Exception:
pass
raise ValueError("FFmpeg failed to convert audio to MP3: " + ((run.stderr or run.stdout or "unknown error")[-1200:]))
converted_size = os.path.getsize(tmp_mp3)
if source_is_mp3 and converted_size > int(original_size * 1.05):
# Avoid bloating an already efficient MP3. Still normalize the target filename to .mp3.
try:
os.remove(tmp_mp3)
except Exception:
pass
tmp_target = target_path + ".copy.tmp"
shutil.copy2(source_path, tmp_target)
os.replace(tmp_target, target_path)
final_size = os.path.getsize(target_path)
return {"mode": "copied_existing_mp3_smaller_than_reencode", "original_size": original_size, "final_size": final_size, "saved_bytes": max(0, original_size - final_size), "ffmpeg": ffmpeg}
os.replace(tmp_mp3, target_path)
final_size = os.path.getsize(target_path)
return {"mode": "converted_to_mp3_ffmpeg", "original_size": original_size, "final_size": final_size, "saved_bytes": max(0, original_size - final_size), "ffmpeg": ffmpeg, "bitrate": AUDIO_MP3_BITRATE}
if source_is_mp3:
tmp_target = target_path + ".copy.tmp"
shutil.copy2(source_path, tmp_target)
os.replace(tmp_target, target_path)
final_size = os.path.getsize(target_path)
return {"mode": "copied_real_mp3_without_ffmpeg", "original_size": original_size, "final_size": final_size, "saved_bytes": max(0, original_size - final_size), "ffmpeg": "not_found"}
raise ValueError("FFmpeg is not installed, so this audio file cannot be converted to MP3. Install FFmpeg or upload a real MP3 file.")
def save_audio_upload_to_target(uploaded_file, target_path, backup_group="audio"):
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
safe_name = secure_filename(uploaded_file.filename or "audio_upload") or "audio_upload"
tmp_path = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"audio_{stamp}_{os.getpid()}_{safe_name}")
uploaded_file.save(tmp_path)
try:
backup_path = backup_existing_file(target_path, backup_group)
media = convert_or_copy_audio_to_mp3(tmp_path, target_path, uploaded_file.filename or "")
return {"path": target_path, "size": os.path.getsize(target_path), "sha256": file_sha256(target_path), "backup": backup_path, "media": media}
finally:
try:
if os.path.exists(tmp_path):
os.remove(tmp_path)
except Exception:
pass
def save_audio_upload_to_multiple_targets(uploaded_file, base_folder, rel_paths, backup_group):
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
safe_name = secure_filename(uploaded_file.filename or "audio_upload") or "audio_upload"
source_tmp = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"audio_multi_{stamp}_{os.getpid()}_{safe_name}")
converted_tmp = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"audio_multi_{stamp}_{os.getpid()}_converted.mp3")
uploaded_file.save(source_tmp)
try:
media = convert_or_copy_audio_to_mp3(source_tmp, converted_tmp, uploaded_file.filename or "")
sha = file_sha256(converted_tmp)
results = []
for rel_path in rel_paths:
target_path = os.path.join(base_folder, rel_path)
ensure_parent_dir(target_path)
backup_path = backup_existing_file(target_path, backup_group)
tmp_target = target_path + ".tmp"
shutil.copy2(converted_tmp, tmp_target)
os.replace(tmp_target, target_path)
results.append({"rel": rel_path, "path": target_path, "size": os.path.getsize(target_path), "sha256": file_sha256(target_path), "backup": backup_path})
return {"size": os.path.getsize(converted_tmp), "sha256": sha, "targets": results, "media": media}
finally:
for p in (source_tmp, converted_tmp):
try:
if os.path.exists(p):
os.remove(p)
except Exception:
pass
def media_details_text(media):
if not media:
return ""
lines = []
if media.get("mode"):
lines.append(f"מצב עיבוד: {media.get('mode')}")
if media.get("original_size") is not None and media.get("final_size") is not None:
lines.append(f"גודל לפני: {format_bytes(media.get('original_size'))}")
lines.append(f"גודל אחרי: {format_bytes(media.get('final_size'))}")
saved = int(media.get("saved_bytes") or 0)
if saved > 0:
lines.append(f"חיסכון: {format_bytes(saved)}")
if media.get("original_dimensions"):
lines.append(f"מידות מקור: {media.get('original_dimensions')}")
if media.get("final_dimensions"):
lines.append(f"מידות סופיות: {media.get('final_dimensions')}")
if media.get("quality"):
lines.append(f"איכות JPG: {media.get('quality')}")
if media.get("bitrate"):
lines.append(f"ביטרייט MP3: {media.get('bitrate')}")
if media.get("ffmpeg"):
lines.append(f"FFmpeg: {media.get('ffmpeg')}")
return "\n".join(lines)
def save_usa_presentation_image(image_key, uploaded_file):
if image_key not in USA_IMAGE_KEYS:
return None
original_name = uploaded_file.filename or ""
if file_ext(original_name) not in USA_IMAGE_EXTENSIONS:
raise ValueError("סיומת תמונה לא נתמכת. העלה JPG/JPEG/PNG/WebP/BMP/TIFF.")
image_dir = os.path.join(FOLDERS["USA"], "media", "usa-presentation", "images")
os.makedirs(image_dir, exist_ok=True)
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
tmp_path = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"usa_image_{stamp}_{os.getpid()}_{secure_filename(image_key)}")
uploaded_file.save(tmp_path)
try:
if os.path.getsize(tmp_path) <= 0:
raise ValueError("קובץ התמונה ריק.")
if os.path.getsize(tmp_path) > USA_IMAGES_ZIP_MAX_SINGLE_FILE_BYTES:
raise ValueError("קובץ התמונה גדול מדי.")
target_path = os.path.join(image_dir, image_key + ".jpg")
backup_path = backup_existing_file(target_path, "USA_images_single")
media = convert_or_copy_image_to_jpg(tmp_path, target_path, original_name)
for old_ext in USA_IMAGE_EXTENSIONS - {".jpg"}:
old_path = os.path.join(image_dir, image_key + old_ext)
if os.path.exists(old_path):
try:
backup_existing_file(old_path, "USA_images_single_old_ext")
os.remove(old_path)
except Exception:
pass
return {"rel": os.path.join("media", "usa-presentation", "images", image_key + ".jpg"), "path": target_path, "mode": media.get("mode", "optimized"), "media": media, "backup": backup_path, "sha256": file_sha256(target_path), "size": os.path.getsize(target_path)}
finally:
try:
if os.path.exists(tmp_path):
os.remove(tmp_path)
except Exception:
pass
def save_usa_presentation_images_zip(uploaded_file):
image_dir = os.path.join(FOLDERS["USA"], "media", "usa-presentation", "images")
os.makedirs(image_dir, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
tmp_zip = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"usa_images_{stamp}.zip")
tmp_extract = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"usa_images_extract_{stamp}")
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
os.makedirs(tmp_extract, exist_ok=True)
uploaded_file.save(tmp_zip)
zip_size = os.path.getsize(tmp_zip)
if zip_size > USA_IMAGES_ZIP_MAX_BYTES:
try:
os.remove(tmp_zip)
except Exception:
pass
raise ValueError(f"zip_size_exceeded:{zip_size}")
imported = []
skipped = []
seen = set()
try:
with zipfile.ZipFile(tmp_zip, "r") as zf:
infos = [info for info in zf.infolist() if not info.is_dir()]
if len(infos) > USA_IMAGES_ZIP_MAX_FILES:
raise ValueError(f"too_many_files:{len(infos)}")
total_uncompressed = sum(max(0, info.file_size) for info in infos)
if total_uncompressed > USA_IMAGES_ZIP_MAX_UNCOMPRESSED_BYTES:
raise ValueError(f"zip_uncompressed_size_exceeded:{total_uncompressed}")
for info in infos:
raw_name = (info.filename or "").replace("\\", "/")
base = os.path.basename(raw_name)
if not base or base.startswith(".") or raw_name.startswith("__MACOSX/"):
continue
if info.file_size > USA_IMAGES_ZIP_MAX_SINGLE_FILE_BYTES:
skipped.append(base + " — גדול מדי")
continue
stem, ext = os.path.splitext(base)
stem = stem.strip()
ext = ext.lower()
if ext not in USA_IMAGE_EXTENSIONS:
skipped.append(base + " — סיומת לא נתמכת")
continue
if stem not in USA_IMAGE_KEYS:
skipped.append(base + " — שם לא שייך למצגת")
continue
if stem in seen:
skipped.append(base + " — כפילות ב־ZIP")
continue
seen.add(stem)
source_tmp = os.path.join(tmp_extract, stem + ext)
with zf.open(info, "r") as source, open(source_tmp, "wb") as dest:
shutil.copyfileobj(source, dest)
target = os.path.join(image_dir, stem + ".jpg")
try:
backup_existing_file(target, "USA_images_zip")
media = convert_or_copy_image_to_jpg(source_tmp, target, base)
for old_ext in USA_IMAGE_EXTENSIONS - {".jpg"}:
old_path = os.path.join(image_dir, stem + old_ext)
if os.path.exists(old_path):
try:
backup_existing_file(old_path, "USA_images_zip_old_ext")
os.remove(old_path)
except Exception:
pass
imported.append(stem + ".jpg" + " — " + format_bytes(media.get("original_size", 0)) + " → " + format_bytes(media.get("final_size", 0)))
except Exception as exc:
skipped.append(base + " — לא הומר/נדחס ל־JPG: " + str(exc))
finally:
try:
os.remove(tmp_zip)
except Exception:
pass
try:
shutil.rmtree(tmp_extract, ignore_errors=True)
except Exception:
pass
imported.sort()
skipped.sort()
# Missing means absent on disk after the import, not merely absent from this specific ZIP.
missing = sorted(key + ".jpg" for key in USA_IMAGE_KEYS if not os.path.exists(os.path.join(image_dir, key + ".jpg")))
return imported, skipped, missing
for folder_path in FOLDERS.values():
os.makedirs(folder_path, exist_ok=True)
os.makedirs(os.path.join(FOLDERS["USA"], "media", "usa-presentation", "music"), exist_ok=True)
os.makedirs(os.path.join(FOLDERS["USA"], "media", "usa-presentation", "images"), exist_ok=True)
os.makedirs(os.path.join(FOLDERS["JAPAN"], "media", "images"), exist_ok=True)
os.makedirs(os.path.join(FOLDERS["JAPAN"], "media", "japan-experience", "music"), exist_ok=True)
os.makedirs(os.path.join(FOLDERS["JAPAN"], "media", "japan-flight", "music"), exist_ok=True)
os.makedirs(os.path.join(FOLDERS["JAPAN"], "media", "music"), exist_ok=True)
os.makedirs(ADMIN_BACKUP_DIR, exist_ok=True)
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
def no_cache_response(response):
response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0, private"
response.headers["Pragma"] = "no-cache"
response.headers["Expires"] = "0"
# Prevent old HTML/images from being reused after upload when the browser sends If-None-Match/If-Modified-Since.
response.headers.pop("ETag", None)
response.headers.pop("Last-Modified", None)
return response
@app.after_request
def add_global_no_cache_headers(response):
return no_cache_response(response)
def serve_file_no_cache(directory, file_name):
try:
response = make_response(send_from_directory(directory, file_name, conditional=False, max_age=0))
except TypeError:
# Compatibility fallback for older Flask/Werkzeug versions.
response = make_response(send_from_directory(directory, file_name))
return no_cache_response(response)
def file_sha256(path):
import hashlib
h = hashlib.sha256()
with open(path, "rb") as fh:
for chunk in iter(lambda: fh.read(1024 * 1024), b""):
h.update(chunk)
return h.hexdigest()
def ensure_parent_dir(path):
parent = os.path.dirname(path)
if parent:
os.makedirs(parent, exist_ok=True)
def backup_existing_file(target_path, backup_group="uploads"):
if not os.path.exists(target_path) or not os.path.isfile(target_path):
return None
stamp = time.strftime("%Y%m%d_%H%M%S")
safe_group = secure_filename(str(backup_group)) or "uploads"
safe_name = secure_filename(os.path.basename(target_path)) or "file"
backup_dir = os.path.join(UPLOAD_BACKUP_DIR, safe_group)
os.makedirs(backup_dir, exist_ok=True)
backup_path = os.path.join(backup_dir, f"{stamp}_{safe_name}")
shutil.copy2(target_path, backup_path)
return backup_path
def atomic_save_upload(uploaded_file, target_path, backup_group="uploads"):
"""Save uploaded file atomically: tmp -> validate size -> backup old -> os.replace."""
ensure_parent_dir(target_path)
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
safe_target = secure_filename(os.path.basename(target_path)) or "upload.bin"
tmp_path = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"tmp_{stamp}_{os.getpid()}_{safe_target}")
try:
uploaded_file.save(tmp_path)
size = os.path.getsize(tmp_path)
if size <= 0:
raise ValueError("uploaded file is empty")
backup_path = backup_existing_file(target_path, backup_group)
os.replace(tmp_path, target_path)
final_size = os.path.getsize(target_path)
if final_size != size:
raise IOError(f"size mismatch after save: tmp={size}, final={final_size}")
return {"path": target_path, "size": final_size, "backup": backup_path, "sha256": file_sha256(target_path)}
finally:
try:
if os.path.exists(tmp_path):
os.remove(tmp_path)
except Exception:
pass
def find_html_file(folder_path):
index_path = os.path.join(folder_path, "index.html")
if os.path.exists(index_path):
return "index.html"
html_files = [f for f in os.listdir(folder_path) if f.lower().endswith((".html", ".htm"))]
if html_files:
return html_files[0]
return None
def clean_wrong_main_files(folder_path):
wrong_names = {"html", "htm", "index", "index.htm", "download", "unknown"}
for name in os.listdir(folder_path):
full_path = os.path.join(folder_path, name)
if not os.path.isfile(full_path):
continue
if name.lower().strip() in wrong_names:
try:
os.remove(full_path)
except Exception:
pass
def save_uploaded_file(folder, uploaded_file):
folder_path = FOLDERS[folder]
os.makedirs(folder_path, exist_ok=True)
original_filename = uploaded_file.filename or ""
safe_name = secure_filename(original_filename)
if folder in APP_FOLDERS:
clean_wrong_main_files(folder_path)
filename = "index.html"
else:
filename = safe_name or f"upload_{int(time.time())}"
target_path = os.path.join(folder_path, filename)
result = atomic_save_upload(uploaded_file, target_path, backup_group=f"{folder}_main")
return filename, result
def save_japan_presentation(presentation_key, uploaded_file):
if presentation_key not in JAPAN_PRESENTATION_FILES:
return None
japan_folder = FOLDERS["JAPAN"]
os.makedirs(japan_folder, exist_ok=True)
filename = JAPAN_PRESENTATION_FILES[presentation_key]
target_path = os.path.join(japan_folder, filename)
result = atomic_save_upload(uploaded_file, target_path, backup_group="JAPAN_presentations")
return filename, result
def save_usa_presentation(presentation_key, uploaded_file):
if presentation_key not in USA_PRESENTATION_FILES:
return None
usa_folder = FOLDERS["USA"]
os.makedirs(usa_folder, exist_ok=True)
filename = USA_PRESENTATION_FILES[presentation_key]
target_path = os.path.join(usa_folder, filename)
result = atomic_save_upload(uploaded_file, target_path, backup_group="USA_presentations")
return filename, result
def save_usa_music(music_key, uploaded_file):
if music_key not in USA_MUSIC_FILES:
return None
usa_folder = FOLDERS["USA"]
rel_path = USA_MUSIC_FILES[music_key]
full_path = os.path.join(usa_folder, rel_path)
result = save_audio_upload_to_target(uploaded_file, full_path, backup_group="USA_music")
return rel_path, result
def save_usa_extra_html(extra_key, uploaded_file):
if extra_key not in USA_EXTRA_HTML_FILES:
return None
filename = USA_EXTRA_HTML_FILES[extra_key]
target_path = os.path.join(FOLDERS["USA"], filename)
result = atomic_save_upload(uploaded_file, target_path, backup_group="USA_extra_html")
return filename, result
def save_file_to_multiple_targets(uploaded_file, base_folder, rel_paths, backup_group):
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
tmp_path = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"multi_upload_{stamp}_{os.getpid()}")
uploaded_file.save(tmp_path)
try:
size = os.path.getsize(tmp_path)
if size <= 0:
raise ValueError("uploaded file is empty")
sha = file_sha256(tmp_path)
results = []
for rel_path in rel_paths:
target_path = os.path.join(base_folder, rel_path)
ensure_parent_dir(target_path)
backup_path = backup_existing_file(target_path, backup_group)
tmp_target = target_path + ".tmp"
shutil.copy2(tmp_path, tmp_target)
os.replace(tmp_target, target_path)
results.append({"rel": rel_path, "path": target_path, "size": os.path.getsize(target_path), "sha256": file_sha256(target_path), "backup": backup_path})
return {"size": size, "sha256": sha, "targets": results}
finally:
try:
if os.path.exists(tmp_path):
os.remove(tmp_path)
except Exception:
pass
def save_japan_music(music_key, uploaded_file):
if music_key not in JAPAN_MUSIC_TARGETS:
return None
return save_audio_upload_to_multiple_targets(uploaded_file, FOLDERS["JAPAN"], JAPAN_MUSIC_TARGETS[music_key], "JAPAN_music")
def normalize_japan_image_rel(raw_name):
safe = safe_zip_relpath(raw_name)
if not safe:
return None
lower = safe.lower()
marker = "/media/images/"
if marker in lower:
idx = lower.index(marker) + len(marker)
return safe[idx:]
if lower.startswith("media/images/"):
return safe[len("media/images/"):]
if lower.startswith("images/"):
return safe[len("images/"):]
marker2 = "/images/"
if marker2 in lower:
idx = lower.index(marker2) + len(marker2)
return safe[idx:]
return None
def save_japan_images_zip(uploaded_file):
image_root = os.path.join(FOLDERS["JAPAN"], "media", "images")
os.makedirs(image_root, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
tmp_zip = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"japan_images_{stamp}.zip")
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
uploaded_file.save(tmp_zip)
imported, skipped, overwritten = [], [], []
try:
if os.path.getsize(tmp_zip) > JAPAN_IMAGES_ZIP_MAX_BYTES:
raise ValueError(f"zip_size_exceeded:{os.path.getsize(tmp_zip)}")
with zipfile.ZipFile(tmp_zip, "r") as zf:
infos = [info for info in zf.infolist() if not info.is_dir()]
if len(infos) > JAPAN_IMAGES_ZIP_MAX_FILES:
raise ValueError(f"too_many_files:{len(infos)}")
total_uncompressed = sum(max(0, info.file_size) for info in infos)
if total_uncompressed > JAPAN_IMAGES_ZIP_MAX_UNCOMPRESSED_BYTES:
raise ValueError(f"zip_uncompressed_size_exceeded:{total_uncompressed}")
for info in infos:
rel = normalize_japan_image_rel(info.filename)
base = os.path.basename((info.filename or "").replace("\\", "/"))
if not rel:
skipped.append(base + " — לא בתיקיית images/media/images")
continue
if info.file_size > JAPAN_IMAGES_ZIP_MAX_SINGLE_FILE_BYTES:
skipped.append(rel + " — גדול מדי")
continue
if file_ext(rel) not in JAPAN_IMAGE_EXTENSIONS:
skipped.append(rel + " — סיומת לא נתמכת")
continue
# Prevent traversal after normalization.
if not safe_zip_relpath(rel):
skipped.append(base + " — נתיב לא בטוח")
continue
target = os.path.join(image_root, *rel.split("/"))
ensure_parent_dir(target)
if os.path.exists(target):
overwritten.append(rel)
backup_existing_file(target, "JAPAN_images_zip")
tmp_target = target + ".tmp"
with zf.open(info, "r") as source, open(tmp_target, "wb") as dest:
shutil.copyfileobj(source, dest)
os.replace(tmp_target, target)
imported.append(rel)
finally:
try:
os.remove(tmp_zip)
except Exception:
pass
return sorted(imported), sorted(skipped), sorted(overwritten)
def normalize_trip_package_rel(trip, raw_name):
safe = safe_zip_relpath(raw_name)
if not safe:
return None
lower = safe.lower()
if trip == "USA":
if lower.startswith("usa/"):
return safe[4:]
# Also allow a package that contains USA files at root.
# Do not accept root index.html from Netlify ZIPs, because it is usually only a root redirect/landing file and would overwrite USA/index.html.
if lower in {"usa2027-presentation.html", "usa2027-presentation-tv.html", "usa2027-tv.html"} or lower.startswith("media/usa-presentation/"):
return safe
return None
if trip == "JAPAN":
if lower.startswith("japan/"):
return safe[6:]
if lower in {"index.html", "japan-experience.html", "japan-flight-intro.html", "japan-theme.mp3"} or lower.startswith("media/"):
return safe
return None
return None
def save_trip_package_zip(trip, uploaded_file):
if trip not in {"USA", "JAPAN"}:
return None
base_folder = FOLDERS[trip]
os.makedirs(base_folder, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
tmp_zip = os.path.join(ADMIN_UPLOAD_TMP_DIR, f"{trip.lower()}_package_{stamp}.zip")
os.makedirs(ADMIN_UPLOAD_TMP_DIR, exist_ok=True)
uploaded_file.save(tmp_zip)
imported, skipped, overwritten = [], [], []
try:
if os.path.getsize(tmp_zip) > TRIP_PACKAGE_ZIP_MAX_BYTES:
raise ValueError(f"zip_size_exceeded:{os.path.getsize(tmp_zip)}")
with zipfile.ZipFile(tmp_zip, "r") as zf:
infos = [info for info in zf.infolist() if not info.is_dir()]
if len(infos) > TRIP_PACKAGE_ZIP_MAX_FILES:
raise ValueError(f"too_many_files:{len(infos)}")
total_uncompressed = sum(max(0, info.file_size) for info in infos)
if total_uncompressed > TRIP_PACKAGE_ZIP_MAX_UNCOMPRESSED_BYTES:
raise ValueError(f"zip_uncompressed_size_exceeded:{total_uncompressed}")
for info in infos:
rel = normalize_trip_package_rel(trip, info.filename)
base = os.path.basename((info.filename or "").replace("\\", "/"))
if not rel:
skipped.append(base + " — לא שייך לחבילת " + trip)
continue
if info.file_size > TRIP_PACKAGE_ZIP_MAX_SINGLE_FILE_BYTES:
skipped.append(rel + " — גדול מדי")
continue
ext = file_ext(rel)
if ext not in TRIP_PACKAGE_EXTENSIONS:
skipped.append(rel + " — סיומת לא נתמכת")
continue
if not safe_zip_relpath(rel):
skipped.append(base + " — נתיב לא בטוח")
continue
target = os.path.join(base_folder, *rel.split("/"))
ensure_parent_dir(target)
if os.path.exists(target):
overwritten.append(rel)
backup_existing_file(target, f"{trip}_package_zip")
tmp_target = target + ".tmp"
with zf.open(info, "r") as source, open(tmp_target, "wb") as dest:
shutil.copyfileobj(source, dest)
os.replace(tmp_target, target)
imported.append(rel)
finally:
try:
os.remove(tmp_zip)
except Exception:
pass
return sorted(imported), sorted(skipped), sorted(overwritten)
def describe_path(path):
exists = os.path.exists(path)
item = {"path": path, "exists": exists}
if exists and os.path.isfile(path):
try:
item["size"] = os.path.getsize(path)
item["mtime"] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(path)))
item["sha256"] = file_sha256(path)[:16]
except Exception as exc:
item["error"] = repr(exc)
return item
def count_files(root, extensions=None):
total = 0
if not os.path.isdir(root):
return 0
for dirpath, _, filenames in os.walk(root):
for name in filenames:
if extensions is None or file_ext(name) in extensions:
total += 1
return total
def list_relative_files(root, extensions=None):
files = []
if not os.path.isdir(root):
return files
for dirpath, _, filenames in os.walk(root):
for name in filenames:
if extensions is None or file_ext(name) in extensions:
full = os.path.join(dirpath, name)
rel = os.path.relpath(full, root).replace("\\", "/")
files.append(rel)
return sorted(files, key=lambda x: x.lower())
def normalize_web_path(path):
value = str(path or "").strip().replace("\\", "/")
value = value.split("?", 1)[0].split("#", 1)[0]
while value.startswith("./"):
value = value[2:]
return value
def analyze_japan_presentation_images(presentation_path=None):
"""Count images that Japan experience actually uses, not just files in media/images."""
presentation_path = presentation_path or os.path.join(FOLDERS["JAPAN"], "japan-experience.html")
image_root = os.path.join(FOLDERS["JAPAN"], "media", "images")
folder_files = list_relative_files(image_root, JAPAN_IMAGE_EXTENSIONS)
result = {
"presentation_path": presentation_path,
"presentation_exists": os.path.isfile(presentation_path),
"folder_images": len(folder_files),
"image_slides": 0,
"external_image_uses": 0,
"embedded_image_uses": 0,
"remote_image_uses": 0,
"other_src_uses": 0,
"unique_external_images": 0,
"missing_external_images": 0,
"unused_folder_images": 0,
"chapter_slides": 0,
"notes": [],
"missing_external_list": [],
"unused_folder_list": [],
}
if not result["presentation_exists"]:
result["notes"].append("קובץ המצגת japan-experience.html לא נמצא, לכן נספרו רק קבצים בתיקיית התמונות.")
return result
try:
with open(presentation_path, "r", encoding="utf-8", errors="ignore") as fh:
html_text = fh.read()
except Exception as exc:
result["notes"].append("לא ניתן לקרוא את קובץ מצגת יפן: " + repr(exc))
return result
refs = [m.group(2).strip() for m in re.finditer(r'["\']src["\']\s*:\s*(["\'])(.*?)\1', html_text, re.S) if m.group(2).strip()]
if not refs:
refs = [m.group(2).strip() for m in re.finditer(r'\bsrc\s*:\s*(["\'])(.*?)\1', html_text, re.S) if m.group(2).strip()]
external = []
embedded = []
remote = []
other = []
for ref in refs:
clean = normalize_web_path(ref)
if clean.startswith("media/images/"):
external.append(clean[len("media/images/"):])
elif clean.startswith("data:image"):
embedded.append(ref)
elif clean.startswith("http://") or clean.startswith("https://"):
remote.append(ref)
else:
other.append(ref)
unique_external = sorted(set(external), key=lambda x: x.lower())
folder_lower = {f.lower(): f for f in folder_files}
used_lower = {f.lower() for f in unique_external}
missing = [rel for rel in unique_external if rel.lower() not in folder_lower]
unused = [rel for rel in folder_files if rel.lower() not in used_lower]
result.update({
"image_slides": len(refs),
"external_image_uses": len(external),
"embedded_image_uses": len(embedded),
"remote_image_uses": len(remote),
"other_src_uses": len(other),
"unique_external_images": len(unique_external),
"missing_external_images": len(missing),
"unused_folder_images": len(unused),
"chapter_slides": len(re.findall(r'["\']chapter["\']\s*:\s*true', html_text)),
"missing_external_list": missing[:40],
"unused_folder_list": unused[:40],
})
if result["image_slides"] == 0:
result["notes"].append("לא נמצאו src בשקפי מצגת יפן. ייתכן שמבנה המצגת השתנה.")
if result["missing_external_images"]:
result["notes"].append("יש תמונות שהמצגת מבקשת אבל אינן קיימות בתיקיית media/images.")
if result["unused_folder_images"]:
result["notes"].append("יש תמונות בתיקייה שאינן בשימוש במצגת הפעילה.")
return result
def compact_japan_usage_for_health():
usage = analyze_japan_presentation_images()
return {
"image_slides_total": usage.get("image_slides", 0),
"external_files_used": usage.get("external_image_uses", 0),
"embedded_images": usage.get("embedded_image_uses", 0),
"folder_images": usage.get("folder_images", 0),
"missing_external_images": usage.get("missing_external_images", 0),
"unused_folder_images": usage.get("unused_folder_images", 0),
"chapter_slides": usage.get("chapter_slides", 0),
"presentation_exists": usage.get("presentation_exists", False),
}
def render_admin_status():
rows = []
def add(label, path):
info = describe_path(path)
status = "✅ קיים" if info.get("exists") else "❌ חסר"
size = f"{info.get('size', 0):,}" if info.get("exists") and "size" in info else "—"
mtime = info.get("mtime", "—")
sha = info.get("sha256", "—")
rows.append(f"<tr><td>{html_lib.escape(label)}</td><td>{status}</td><td>{size}</td><td>{html_lib.escape(mtime)}</td><td>{html_lib.escape(sha)}</td><td><code>{html_lib.escape(path)}</code></td></tr>")
add("פאנל פעיל", ADMIN_SERVER_PATH)
add("טיול יפן ראשי", os.path.join(FOLDERS["JAPAN"], "index.html"))
add("פתיח טיסה יפן", os.path.join(FOLDERS["JAPAN"], "japan-flight-intro.html"))
add("מצגת יפן", os.path.join(FOLDERS["JAPAN"], "japan-experience.html"))
add("מוזיקת מצגת יפן", os.path.join(FOLDERS["JAPAN"], "media", "japan-experience", "music", "japan-theme.mp3"))
add("מוזיקת פתיח יפן", os.path.join(FOLDERS["JAPAN"], "media", "japan-flight", "music", "flight-intro.mp3"))
add("טיול ארה״ב ראשי", os.path.join(FOLDERS["USA"], "index.html"))
add("טיול ארה״ב TV", os.path.join(FOLDERS["USA"], "usa2027-tv.html"))
add("מצגת ארה״ב", os.path.join(FOLDERS["USA"], "usa2027-presentation.html"))
add("מצגת ארה״ב TV", os.path.join(FOLDERS["USA"], "usa2027-presentation-tv.html"))
add("שיר ארה״ב חלק 1", os.path.join(FOLDERS["USA"], "media", "usa-presentation", "music", "usa-roadtrip-theme.mp3"))
add("שיר ארה״ב חלק 2", os.path.join(FOLDERS["USA"], "media", "usa-presentation", "music", "usa-part2-theme.mp3"))
add("תקציב טיולים", os.path.join(FOLDERS["Moneyapp"], "index.html"))
add("תקציב משפחתי", os.path.join(FOLDERS["MoneyHome"], "index.html"))
add("טופס פתיחת טיול", os.path.join(FOLDERS["TripForm"], "index.html"))
usa_img_count = count_files(os.path.join(FOLDERS["USA"], "media", "usa-presentation", "images"), USA_IMAGE_EXTENSIONS)
japan_usage = analyze_japan_presentation_images()
japan_img_count = japan_usage["folder_images"]
japan_usage_ok = japan_usage["presentation_exists"] and japan_usage["missing_external_images"] == 0
japan_usage_badge = "✅ תקין" if japan_usage_ok else "⚠️ לבדיקה"
japan_usage_details = (
f"{japan_usage['external_image_uses']} קבצים חיצוניים + "
f"{japan_usage['embedded_image_uses']} מוטמעות · "
f"בתיקייה: {japan_img_count} · "
f"לא בשימוש: {japan_usage['unused_folder_images']} · "
f"חסרות: {japan_usage['missing_external_images']}"
)
watchdog_info = watchdog_status(include_log=False)
watchdog_label = "ON" if watchdog_info.get("task_installed") else "OFF"
missing_html = ""
if japan_usage["missing_external_images"]:
missing_items = "".join(f"<li><code>{html_lib.escape(x)}</code></li>" for x in japan_usage["missing_external_list"][:20])
missing_html = f"<div class='warn'><b>תמונות חסרות למצגת יפן</b><ul>{missing_items}</ul></div>"
unused_html = ""
if japan_usage["unused_folder_images"]:
unused_items = "".join(f"<li><code>{html_lib.escape(x)}</code></li>" for x in japan_usage["unused_folder_list"][:20])
more = ""
if japan_usage["unused_folder_images"] > 20:
more = f"<p>ועוד {japan_usage['unused_folder_images'] - 20} קבצים שלא מוצגים כאן.</p>"
unused_html = f"<details class='details'><summary>תמונות בתיקיית יפן שלא בשימוש במצגת ({japan_usage['unused_folder_images']})</summary><ul>{unused_items}</ul>{more}</details>"
return f"""
<!DOCTYPE html>
<html lang="he" dir="rtl">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>סטטוס TripServer</title>
<style>
body{{margin:0;background:#0f172a;color:white;font-family:Arial;padding:24px}}.wrap{{max-width:1180px;margin:auto}}a{{color:#93c5fd}}table{{width:100%;border-collapse:collapse;background:#1e293b;border-radius:18px;overflow:hidden}}th,td{{border-bottom:1px solid #334155;padding:10px;text-align:right;vertical-align:top}}th{{background:#111827;color:#bfdbfe}}code{{direction:ltr;display:block;white-space:normal;color:#fde68a}}.cards{{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:14px;margin:18px 0}}.card{{background:#1e293b;border:1px solid #334155;border-radius:18px;padding:16px}}.num{{font-size:30px;font-weight:900;color:#fde68a}}
.small{{color:#cbd5e1;font-size:13px;line-height:1.5;margin-top:6px}}.warn{{background:rgba(127,29,29,.38);border:1px solid rgba(248,113,113,.35);color:#fecaca;border-radius:16px;padding:14px;margin:14px 0}}.details{{background:#111827;border:1px solid #334155;border-radius:16px;padding:14px;margin:14px 0;color:#cbd5e1}}summary{{cursor:pointer;font-weight:900;color:#fde68a}}ul{{margin:10px 0 0}}
</style></head><body><div class="wrap"><a href="/admin">⬅ חזרה לפאנל</a><h1>סטטוס קבצים פעילים</h1><p>כאן רואים בפועל מה מותקן כרגע ב־C:\\TripServer, כולל גודל, תאריך שינוי ו־SHA קצר. זה נועד למנוע מצב של “העליתי אבל אני רואה גרסה ישנה”.</p><div class="cards"><div class="card"><div>תמונות מצגת ארה״ב</div><div class="num">{usa_img_count}</div><div class="small">כרטיסיות העלאה מסונכרנות לשקפי ארה״ב.</div></div><div class="card"><div>מצגת יפן — תמונות בשימוש בפועל</div><div class="num">{japan_usage['image_slides']}</div><div class="small">{japan_usage_badge} · {japan_usage_details}</div></div><div class="card"><div>קבצים בתיקיית תמונות יפן</div><div class="num">{japan_img_count}</div><div class="small">זה מספר קבצים בתיקייה, לא מספר התמונות שהמצגת משתמשת בהן.</div></div><div class="card"><div>גרסת פאנל</div><div>{html_lib.escape(ADMIN_PANEL_VERSION)}</div></div></div>{missing_html}{unused_html}<table><thead><tr><th>רכיב</th><th>מצב</th><th>גודל bytes</th><th>עודכן</th><th>SHA16</th><th>נתיב</th></tr></thead><tbody>{''.join(rows)}</tbody></table></div></body></html>
"""
def validate_admin_server_upload(tmp_path):
size = os.path.getsize(tmp_path)
if size <= 0:
return False, "הקובץ ריק."
if size > ADMIN_MAX_UPLOAD_BYTES:
return False, f"הקובץ גדול מדי: {size:,} bytes. המגבלה היא {ADMIN_MAX_UPLOAD_BYTES:,} bytes."
try:
py_compile.compile(tmp_path, doraise=True)
except py_compile.PyCompileError as exc:
return False, "בדיקת קומפילציה נכשלה:\n" + str(exc)
except Exception as exc:
return False, "שגיאה בבדיקת הקובץ:\n" + repr(exc)
try:
with open(tmp_path, "r", encoding="utf-8", errors="ignore") as fh:
content = fh.read(400000)
except Exception as exc:
return False, "לא ניתן לקרוא את הקובץ:\n" + repr(exc)
required_tokens = ["Flask", "@app.route", "app.run", "BASE_DIR", "ADMIN_PORT", "restart_process_later", "upload_admin_server", "run_self_test_suite", "build_watchdog_code", "watchdog_status"]
missing = [token for token in required_tokens if token not in content]
if missing:
return False, "הקובץ עבר קומפילציה, אבל לא נראה כמו admin_server.py של TripServer. חסרים סימנים: " + ", ".join(missing)
return True, "OK"
def extract_admin_server_from_zip_upload(zip_path, work_dir):
"""Validate a controlled admin update ZIP and extract only the admin Python file.
Supported package format:
- root admin_server.py, or
- exactly one root-level .py file that passes TripServer admin validation.
For safety, this route does not execute arbitrary scripts from the ZIP.
"""
size = os.path.getsize(zip_path)
if size <= 0:
return False, "קובץ ה־ZIP ריק.", None
if size > ADMIN_MAX_PACKAGE_UPLOAD_BYTES:
return False, f"קובץ ה־ZIP גדול מדי: {size:,} bytes. המגבלה היא {ADMIN_MAX_PACKAGE_UPLOAD_BYTES:,} bytes.", None
try:
with zipfile.ZipFile(zip_path, "r") as zf:
infos = zf.infolist()
if not infos:
return False, "קובץ ה־ZIP ריק.", None
unsafe = []
py_candidates = []
for info in infos:
name = (info.filename or "").replace("\\", "/")
clean = name.strip("/")
if not clean or info.is_dir():
continue
if clean.startswith("/") or ".." in clean.split("/"):
unsafe.append(name)
continue
# Keep package execution intentionally disabled; only extract admin Python.
if clean.lower() == "admin_server.py" or ("/" not in clean and clean.lower().endswith(".py")):
py_candidates.append(clean)
if unsafe:
return False, "קובץ ה־ZIP כולל נתיבים לא בטוחים: " + ", ".join(unsafe[:6]), None
if "admin_server.py" in py_candidates:
selected = "admin_server.py"
elif len(py_candidates) == 1:
selected = py_candidates[0]
else:
return False, "ZIP עדכון אדמין חייב לכלול admin_server.py בשורש, או קובץ Python יחיד בשורש החבילה.", None
os.makedirs(work_dir, exist_ok=True)
extracted_path = os.path.join(work_dir, "admin_server_from_zip.py")
with zf.open(selected, "r") as src, open(extracted_path, "wb") as dst:
shutil.copyfileobj(src, dst)
valid, details = validate_admin_server_upload(extracted_path)
if not valid:
try: os.remove(extracted_path)
except Exception: pass
return False, "admin_server.py שבתוך ה־ZIP נדחה:\n" + details, None
return True, f"ZIP תקין. נבחר קובץ: {selected}", extracted_path
except zipfile.BadZipFile:
return False, "קובץ ZIP לא תקין או פגום.", None
except Exception as exc:
return False, "שגיאה בבדיקת ZIP:\n" + repr(exc), None
def admin_status_page(title, message, ok=True, details=""):
color = "#16a34a" if ok else "#dc2626"
bg = "#052e1b" if ok else "#3f1d1d"
safe_title = html_lib.escape(title)
safe_message = html_lib.escape(message).replace("\n", "<br>")
safe_details = html_lib.escape(details).replace("\n", "<br>")
details_html = f"<pre>{safe_details}</pre>" if details else ""
return f"""
<!DOCTYPE html>
<html lang="he" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{safe_title}</title>
<style>
body{{margin:0;font-family:Arial;background:#0f172a;color:white;min-height:100vh;display:flex;align-items:center;justify-content:center;padding:24px}}
.card{{width:min(780px,94vw);background:#1e293b;border-radius:24px;padding:26px;box-shadow:0 18px 70px rgba(0,0,0,.35)}}
.badge{{display:inline-block;background:{bg};border:1px solid {color};color:white;border-radius:999px;padding:8px 14px;font-weight:bold;margin-bottom:12px}}
h1{{margin:0 0 12px;font-size:30px}}
p{{line-height:1.7;color:#e2e8f0}}
pre{{white-space:pre-wrap;direction:ltr;text-align:left;background:#020617;color:#fde68a;border:1px solid #334155;border-radius:14px;padding:14px;overflow:auto;max-height:360px}}
a,button{{display:inline-block;margin:8px 0 0 8px;background:#2563eb;color:white;border:0;border-radius:12px;padding:12px 16px;text-decoration:none;font-weight:bold;cursor:pointer}}
button.restart{{background:#ea580c}}
.advanced-note{{margin-top:10px;color:#cbd5e1;font-size:13px;line-height:1.55}}
form{{display:inline}}
</style>
</head>
<body>
<div class="card">
<div class="badge">TripServer Admin</div>
<h1>{safe_title}</h1>
<p>{safe_message}</p>
{details_html}
<a href="/admin">חזרה לפאנל</a>
<form action="/restart_admin_server" method="post" onsubmit="return confirm('זו בדיקת אתחול מתקדמת. להפעיל מחדש את הפאנל עכשיו?')"><button class="restart">בדיקת אתחול פאנל — מתקדם</button></form>
<div class="advanced-note">מיועד לבדיקה יזומה בלבד אחרי עדכון/שחזור קובץ אדמין.</div>
</div>
</body>
</html>
"""
def backup_current_admin_server(prefix="admin_server_backup"):
os.makedirs(ADMIN_BACKUP_DIR, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
backup_path = os.path.join(ADMIN_BACKUP_DIR, f"{prefix}_{stamp}.py")
shutil.copy2(ADMIN_SERVER_PATH, backup_path)
return backup_path
def latest_admin_backup():
os.makedirs(ADMIN_BACKUP_DIR, exist_ok=True)
backups = glob.glob(os.path.join(ADMIN_BACKUP_DIR, "admin_server_backup_*.py"))
if not backups:
return None
return max(backups, key=os.path.getmtime)
def log_control_bootstrap(message):
try:
os.makedirs(BASE_DIR, exist_ok=True)
with open(CONTROL_BOOTSTRAP_LOG_PATH, "a", encoding="utf-8") as fh:
fh.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | control-bootstrap | " + str(message) + "\n")
except Exception:
pass
def quiet_pythonw_path():
exe = sys.executable or "python"
base = os.path.basename(exe).lower()
if base == "python.exe":
candidate = os.path.join(os.path.dirname(exe), "pythonw.exe")
if os.path.exists(candidate):
return candidate
return exe
def build_control_server_code():
return '# -*- coding: utf-8 -*-\nimport os, sys, time, json, shutil, subprocess, socket, threading, traceback\nfrom http.server import BaseHTTPRequestHandler, ThreadingHTTPServer\nfrom urllib.parse import urlparse\nBASE_DIR = r"C:\\TripServer"\nADMIN_PORT = 8000\nCONTROL_PORT = 8010\nADMIN_SCRIPT = os.path.join(BASE_DIR, "admin_server.py")\nCONTROL_DIR = os.path.join(BASE_DIR, "control_server")\nPENDING_ADMIN = os.path.join(CONTROL_DIR, "pending_admin_server.py")\nREQUEST_JSON = os.path.join(CONTROL_DIR, "request.json")\nSTATUS_JSON = os.path.join(CONTROL_DIR, "status.json")\nFAILSAFE_STATUS_JSON = os.path.join(BASE_DIR, "admin_failsafe_status.json")\nLOG_PATH = os.path.join(CONTROL_DIR, "tripserver_control_server.log")\nBACKUP_DIR = os.path.join(BASE_DIR, "backups", "admin_server")\nPYTHON_EXE = sys.executable\nif os.path.basename(PYTHON_EXE).lower() == "pythonw.exe":\n c = os.path.join(os.path.dirname(PYTHON_EXE), "python.exe")\n if os.path.exists(c): PYTHON_EXE = c\nos.makedirs(CONTROL_DIR, exist_ok=True); os.makedirs(BACKUP_DIR, exist_ok=True)\n\ndef log(msg):\n try:\n with open(LOG_PATH, "a", encoding="utf-8") as f:\n f.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | control-v34 | " + str(msg) + "\\n")\n except Exception: pass\n\ndef write_status(**kw):\n data={"updated_at":time.strftime("%Y-%m-%d %H:%M:%S"),"control_port":CONTROL_PORT,"admin_port":ADMIN_PORT,"python":PYTHON_EXE}\n data.update(kw)\n try:\n with open(STATUS_JSON,"w",encoding="utf-8") as f: json.dump(data,f,ensure_ascii=False,indent=2)\n except Exception as e: log("status write failed "+repr(e))\n return data\n\ndef port_open(port):\n s=socket.socket(socket.AF_INET,socket.SOCK_STREAM); s.settimeout(0.7)\n try: return s.connect_ex(("127.0.0.1",int(port)))==0\n finally:\n try: s.close()\n except Exception: pass\n\ndef admin_ping_ok():\n try:\n import urllib.request\n with urllib.request.urlopen("http://127.0.0.1:%s/ping.json"%ADMIN_PORT,timeout=1.5) as r: return r.status==200\n except Exception: return False\n\ndef pids_on_port(port):\n pids=[]\n if os.name!="nt": return pids\n try:\n cp=subprocess.run(["netstat","-ano","-p","tcp"],capture_output=True,text=True,timeout=8)\n for line in (cp.stdout or "").splitlines():\n low=line.lower()\n if (":"+str(port)) in low and "listening" in low:\n parts=line.split(); pid=parts[-1] if parts else ""\n if pid.isdigit() and int(pid) not in pids: pids.append(int(pid))\n except Exception as e: log("pids_on_port failed "+repr(e))\n return pids\n\ndef kill_admin():\n pids=pids_on_port(ADMIN_PORT); log("kill_admin pids="+repr(pids))\n for pid in pids:\n if pid==os.getpid(): continue\n try:\n cp=subprocess.run(["taskkill","/PID",str(pid),"/F"],capture_output=True,text=True,timeout=12)\n log("taskkill pid=%s rc=%s out=%s err=%s"%(pid,cp.returncode,(cp.stdout or "").strip(),(cp.stderr or "").strip()))\n except Exception as e: log("taskkill failed pid=%s %r"%(pid,e))\n deadline=time.time()+8\n while time.time()<deadline:\n if not port_open(ADMIN_PORT): return True\n time.sleep(0.25)\n return not port_open(ADMIN_PORT)\n\ndef start_admin():\n log("starting admin "+ADMIN_SCRIPT+" python="+PYTHON_EXE)\n flags=0\n if os.name=="nt": flags|=getattr(subprocess,"CREATE_NEW_PROCESS_GROUP",0)|getattr(subprocess,"CREATE_NO_WINDOW",0)|0x00000008\n out=open(os.path.join(BASE_DIR,"admin_stdout.log"),"a",encoding="utf-8",errors="replace")\n proc=subprocess.Popen([PYTHON_EXE,ADMIN_SCRIPT],cwd=BASE_DIR,stdout=out,stderr=out,stdin=subprocess.DEVNULL,creationflags=flags)\n log("started admin pid="+str(proc.pid)); write_status(last_start_pid=proc.pid,last_action="start_admin"); return proc.pid\n\ndef compile_ok(path):\n try:\n src=open(path,"r",encoding="utf-8",errors="replace").read(); compile(src,path,"exec"); return True,"OK"\n except Exception as e: return False,repr(e)\n\ndef sha256(path):\n import hashlib\n h=hashlib.sha256()\n with open(path,"rb") as f:\n for chunk in iter(lambda:f.read(1024*1024),b""): h.update(chunk)\n return h.hexdigest()\n\ndef write_failsafe_status(state,msg,**extra):\n try:\n data={"state":state,"message":msg,"updated_at":time.strftime("%Y-%m-%d %H:%M:%S"),"source":"control_server"}\n data.update(extra)\n tmp=FAILSAFE_STATUS_JSON+".tmp"\n with open(tmp,"w",encoding="utf-8") as f: json.dump(data,f,ensure_ascii=False,indent=2)\n os.replace(tmp,FAILSAFE_STATUS_JSON)\n except Exception as e: log("failsafe status write failed "+repr(e))\n\ndef no_pending_shutdown_msg(txt):\n low=(txt or "").lower()\n return ("1116" in low) or ("no shutdown was in progress" in low)\n\ndef cancel_pending_shutdown(reason="auto_cancel"):\n if os.name!="nt": return False,"not windows"\n try:\n cp=subprocess.run(["shutdown","/a"],capture_output=True,text=True,timeout=8)\n msg=(cp.stdout or cp.stderr or "").strip()\n log("shutdown /a reason=%s rc=%s out=%s err=%s"%(reason,cp.returncode,(cp.stdout or "").strip(),(cp.stderr or "").strip()))\n if cp.returncode==0:\n write_failsafe_status("auto_cancelled","Fail Safe בוטל אוטומטית אחרי שהאדמין עלה תקין.",windows_output=msg)\n return True,msg\n if no_pending_shutdown_msg(msg):\n write_failsafe_status("none","אין ריסטארט Fail Safe פעיל. Windows דיווח שלא היה כיבוי/ריסטארט בהמתנה.",windows_output=msg)\n return True,msg\n write_failsafe_status("cancel_failed","ניסיון ביטול אוטומטי החזיר שגיאה.",windows_output=msg,return_code=cp.returncode)\n return False,msg\n except Exception as e:\n log("shutdown /a failed "+repr(e)); write_failsafe_status("cancel_failed","שגיאה בביטול אוטומטי: "+repr(e)); return False,repr(e)\n\ndef apply_update(reason="manual"):\n log("apply_update begin reason="+str(reason))\n if not os.path.exists(PENDING_ADMIN):\n msg="pending admin not found"; log(msg); write_status(last_action="apply_update",ok=False,error=msg); return False,msg\n ok,info=compile_ok(PENDING_ADMIN)\n if not ok:\n log("pending compile failed "+info); write_status(last_action="apply_update",ok=False,error=info); return False,info\n new_sha=sha256(PENDING_ADMIN); stamp=time.strftime("%Y%m%d_%H%M%S"); backup=os.path.join(BACKUP_DIR,"admin_server_backup_"+stamp+".py")\n try:\n if os.path.exists(ADMIN_SCRIPT): shutil.copy2(ADMIN_SCRIPT,backup)\n except Exception as e: log("backup failed "+repr(e))\n kill_admin()\n try:\n os.replace(PENDING_ADMIN,ADMIN_SCRIPT); log("admin replaced sha="+new_sha+" backup="+backup)\n except Exception as e:\n log("replace failed "+repr(e)); write_status(last_action="apply_update",ok=False,error=repr(e)); return False,repr(e)\n pid=start_admin(); time.sleep(2); ping=admin_ping_ok(); cancel_ok=False; cancel_msg=""\n if ping: cancel_ok,cancel_msg=cancel_pending_shutdown("admin_update_ping_ok")\n write_status(last_action="apply_update",ok=True,new_sha=new_sha,backup=backup,started_pid=pid,ping_ok=ping,failsafe_cancel_ok=cancel_ok,failsafe_cancel_msg=cancel_msg); return True,"updated pid=%s ping=%s failsafe_cancel=%s"%(pid,ping,cancel_ok)\n\ndef restart_admin(reason="manual"):\n kill_admin(); pid=start_admin(); time.sleep(2); ping=admin_ping_ok(); write_status(last_action="restart_admin",ok=True,started_pid=pid,ping_ok=ping); return True,"started pid=%s ping=%s"%(pid,ping)\n\ndef process_request_file():\n if not os.path.exists(REQUEST_JSON): return\n try:\n with open(REQUEST_JSON,"r",encoding="utf-8") as f: req=json.load(f)\n except Exception as e: log("bad request json "+repr(e)); return\n try: not_before=float(req.get("not_before",0) or 0)\n except Exception: not_before=0\n if time.time()<not_before: return\n try: os.remove(REQUEST_JSON)\n except Exception: pass\n action=req.get("action")\n if action=="apply_update": apply_update(req.get("reason","request"))\n elif action=="restart_admin": restart_admin(req.get("reason","request"))\n else: log("unknown action "+repr(action))\n\ndef poll_loop():\n log("poll loop started")\n while True:\n try:\n process_request_file()\n if not port_open(ADMIN_PORT): log("admin port closed; starting admin"); start_admin()\n except Exception as e: log("poll error "+repr(e)+"\\n"+traceback.format_exc())\n time.sleep(2)\n\nclass Handler(BaseHTTPRequestHandler):\n server_version="TripServerControl/34"\n def _send(self,code,body,ctype="text/html; charset=utf-8"):\n if isinstance(body,str): body=body.encode("utf-8")\n self.send_response(code); self.send_header("Content-Type",ctype); self.send_header("Content-Length",str(len(body))); self.end_headers(); self.wfile.write(body)\n def do_GET(self):\n path=urlparse(self.path).path\n if path=="/ping.json": self._send(200,json.dumps({"ok":True,"admin_ping":admin_ping_ok(),"admin_port_open":port_open(ADMIN_PORT)},ensure_ascii=False),"application/json; charset=utf-8"); return\n if path=="/status.json":\n data={}\n try:\n if os.path.exists(STATUS_JSON): data=json.load(open(STATUS_JSON,"r",encoding="utf-8"))\n except Exception as e: data={"error":repr(e)}\n data.update({"ok":True,"admin_ping":admin_ping_ok(),"admin_port_open":port_open(ADMIN_PORT),"pids":pids_on_port(ADMIN_PORT)})\n self._send(200,json.dumps(data,ensure_ascii=False,indent=2),"application/json; charset=utf-8"); return\n if path=="/log":\n try: txt=open(LOG_PATH,"r",encoding="utf-8",errors="replace").read()[-20000:]\n except Exception as e: txt=repr(e)\n self._send(200,"<pre style=\'white-space:pre-wrap;direction:ltr\'>"+txt.replace("<","<")+"</pre>"); return\n html="""<html><head><meta charset=\'utf-8\'><title>TripServer Control</title><style>body{font-family:Arial;background:#0b1220;color:#eef;direction:rtl;padding:30px}.card{max-width:760px;margin:auto;background:#111a2e;border:1px solid #334155;border-radius:22px;padding:24px}button{font-size:22px;padding:16px 22px;border-radius:16px;border:0;margin:8px;background:#2563eb;color:white}.danger{background:#dc2626}a{color:#93c5fd}</style></head><body><div class=\'card\'><h1>TripServer Control Server</h1><p>שרת בקרה חיצוני לפורט 8000. לא תלוי באדמין הראשי.</p><form method=\'post\' action=\'/restart\'><button>הפעל/אתחל אדמין ראשי</button></form><form method=\'post\' action=\'/apply-update\'><button class=\'danger\'>החל עדכון ממתין</button></form><p><a href=\'/status.json\'>Status JSON</a> · <a href=\'/log\'>Log</a></p></div></body></html>"""\n self._send(200,html)\n def do_POST(self):\n path=urlparse(self.path).path\n if path=="/restart": ok,msg=restart_admin("http"); self._send(200 if ok else 500,json.dumps({"ok":ok,"message":msg},ensure_ascii=False),"application/json; charset=utf-8"); return\n if path=="/apply-update": ok,msg=apply_update("http"); self._send(200 if ok else 500,json.dumps({"ok":ok,"message":msg},ensure_ascii=False),"application/json; charset=utf-8"); return\n self._send(404,"not found")\n def log_message(self,fmt,*args): log("http "+(fmt%args))\nif __name__=="__main__":\n log("starting control server python="+sys.executable)\n write_status(last_action="control_start",ok=True)\n threading.Thread(target=poll_loop,daemon=True).start()\n srv=ThreadingHTTPServer(("0.0.0.0",CONTROL_PORT),Handler)\n log("listening on port "+str(CONTROL_PORT))\n srv.serve_forever()\n'
def control_server_is_alive():
try:
with urllib.request.urlopen(f"http://127.0.0.1:{CONTROL_PORT}/ping.json", timeout=1.0) as resp:
return resp.status == 200
except Exception:
return False
def bootstrap_control_server(reason="startup"):
try:
os.makedirs(CONTROL_DIR, exist_ok=True)
code = build_control_server_code()
current = None
if os.path.exists(CONTROL_SCRIPT_PATH):
try:
current = open(CONTROL_SCRIPT_PATH, "r", encoding="utf-8", errors="replace").read()
except Exception:
current = None
if current != code:
compile(code, CONTROL_SCRIPT_PATH, "exec")
with open(CONTROL_SCRIPT_PATH, "w", encoding="utf-8") as fh:
fh.write(code)
log_control_bootstrap("control script written " + CONTROL_SCRIPT_PATH)
pyw = quiet_pythonw_path()
if os.name == "nt":
cp = subprocess.run(["schtasks", "/Create", "/TN", CONTROL_TASK_NAME, "/SC", "ONLOGON", "/TR", f'"{pyw}" "{CONTROL_SCRIPT_PATH}"', "/F"], capture_output=True, text=True, timeout=20)
log_control_bootstrap("schtasks create rc=%s out=%s err=%s" % (cp.returncode, (cp.stdout or "").strip(), (cp.stderr or "").strip()))
run = subprocess.run(["schtasks", "/Run", "/TN", CONTROL_TASK_NAME], capture_output=True, text=True, timeout=20)
log_control_bootstrap("schtasks run rc=%s out=%s err=%s" % (run.returncode, (run.stdout or '').strip(), (run.stderr or '').strip()))
if not control_server_is_alive():
flags = 0
if os.name == "nt":
flags |= getattr(subprocess, "CREATE_NO_WINDOW", 0) | getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) | 0x00000008
subprocess.Popen([pyw, CONTROL_SCRIPT_PATH], cwd=BASE_DIR, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=flags)
log_control_bootstrap("direct control start requested via " + pyw)
return True, "OK"
except Exception as exc:
log_control_bootstrap("bootstrap failed " + repr(exc))
return False, repr(exc)
def write_control_request(action, reason="admin", pending_path=None):
os.makedirs(CONTROL_DIR, exist_ok=True)
req = {
"action": action,
"reason": reason,
"requested_at": time.strftime("%Y-%m-%d %H:%M:%S"),
"not_before": time.time() + 2.5,
"admin_pid": os.getpid(),
"pending_admin_path": pending_path or CONTROL_PENDING_ADMIN_PATH,
}
with open(CONTROL_REQUEST_PATH, "w", encoding="utf-8") as fh:
json.dump(req, fh, ensure_ascii=False, indent=2)
return req
def log_admin_restart(message):
try:
os.makedirs(BASE_DIR, exist_ok=True)
with open(ADMIN_RESTART_LOG_PATH, "a", encoding="utf-8") as fh:
fh.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | " + str(message) + "\n")
except Exception:
pass
def build_restart_helper_code(script_path, python_exe, work_dir, current_pid=None, backup_path=None, expected_sha=None, reason=""):
"""Build a simple and conservative restart helper.
v29 change: stop doing rollback/health gating inside the helper. The logs from
the real Windows machine showed the new Flask server starts, but the helper's
own health request sometimes times out and then incorrectly rolls back or
leaves the port in a bad state. Since uploaded admin files already pass
compile + TripServer validation before replacement, the safest behavior is:
wait, release port 8000, start admin_server.py, write logs, exit. Watchdog and
the browser then verify availability through /ping.json.
"""
return "\n".join([
"# -*- coding: utf-8 -*-",
"import os",
"import sys",
"import subprocess",
"import time",
"import socket",
"import hashlib",
"",
f"script_path = {script_path!r}",
f"python_exe = {python_exe!r}",
f"work_dir = {work_dir!r}",
f"log_path = {ADMIN_RESTART_LOG_PATH!r}",
f"admin_port = {ADMIN_PORT!r}",
f"current_pid = {int(current_pid or 0)!r}",
f"backup_path = {backup_path!r}",
f"expected_sha = {expected_sha!r}",
f"reason = {reason!r}",
"",
"def log(message):",
" try:",
" os.makedirs(os.path.dirname(log_path), exist_ok=True)",
" with open(log_path, 'a', encoding='utf-8') as fh:",
" fh.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' | restart-v29 | ' + str(message) + '\\n')",
" except Exception:",
" pass",
"",
"def file_sha256(path):",
" h = hashlib.sha256()",
" with open(path, 'rb') as fh:",
" for chunk in iter(lambda: fh.read(1024 * 1024), b''):",
" h.update(chunk)",
" return h.hexdigest()",
"",
"def port_open():",
" try:",
" with socket.create_connection(('127.0.0.1', int(admin_port)), timeout=0.7):",
" return True",
" except Exception:",
" return False",
"",
"def wait_port_closed(timeout=12):",
" deadline = time.time() + timeout",
" while time.time() < deadline:",
" if not port_open():",
" return True",
" time.sleep(0.4)",
" return not port_open()",
"",
"def pids_on_port():",
" pids = []",
" if os.name != 'nt':",
" return pids",
" try:",
" cp = subprocess.run(['netstat', '-ano', '-p', 'tcp'], capture_output=True, text=True, shell=False, timeout=10)",
" out = (cp.stdout or '') + '\\n' + (cp.stderr or '')",
" marker = ':' + str(admin_port)",
" for line in out.splitlines():",
" if marker not in line or 'listen' not in line.lower():",
" continue",
" parts = line.split()",
" if parts:",
" try:",
" pid = int(parts[-1])",
" if pid and pid not in pids:",
" pids.append(pid)",
" except Exception:",
" pass",
" except Exception as exc:",
" log('netstat failed: ' + repr(exc))",
" return pids",
"",
"def kill_pid(pid):",
" try:",
" pid = int(pid)",
" except Exception:",
" return False",
" if pid <= 0 or pid == os.getpid():",
" log('skip kill pid=' + str(pid) + ' self=' + str(os.getpid()))",
" return False",
" try:",
" if os.name == 'nt':",
" # No /T. Kill only the process that owns port 8000, never a tree.",
" cp = subprocess.run(['taskkill', '/PID', str(pid), '/F'], capture_output=True, text=True, shell=False, timeout=12)",
" log('taskkill pid=' + str(pid) + ' rc=' + str(cp.returncode) + ' out=' + ((cp.stdout or cp.stderr or "").strip()[:500]))",
" return cp.returncode == 0",
" os.kill(pid, 9)",
" return True",
" except Exception as exc:",
" log('kill failed pid=' + str(pid) + ' err=' + repr(exc))",
" return False",
"",
"def release_port():",
" # Give the old request time to finish and Flask to exit if it exits naturally.",
" if wait_port_closed(10):",
" log('port closed naturally')",
" return",
" pids = pids_on_port()",
" log('port still open; killing owners without rollback; current_pid=' + str(current_pid) + ' pids=' + str(pids))",
" for pid in pids:",
" kill_pid(pid)",
" wait_port_closed(10)",
" log('port open after release=' + str(port_open()) + ' pids=' + str(pids_on_port()))",
"",
"def start_server():",
" flags = 0",
" if os.name == 'nt':",
" flags = getattr(subprocess, 'CREATE_NEW_PROCESS_GROUP', 0) | getattr(subprocess, 'DETACHED_PROCESS', 0) | getattr(subprocess, 'CREATE_BREAKAWAY_FROM_JOB', 0)",
" env = os.environ.copy()",
" env.setdefault('PYTHONIOENCODING', 'utf-8')",
" os.makedirs(os.path.dirname(log_path), exist_ok=True)",
" with open(log_path, 'a', encoding='utf-8') as log_file:",
" log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' | restart-v29 | starting admin: ' + script_path + '\\n')",
" log_file.flush()",
" proc = subprocess.Popen([python_exe, script_path], cwd=work_dir, stdin=subprocess.DEVNULL, stdout=log_file, stderr=log_file, close_fds=(False if os.name == 'nt' else True), creationflags=flags, start_new_session=(os.name != 'nt'), env=env)",
" log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' | restart-v29 | started admin pid=' + str(proc.pid) + '\\n')",
" log_file.flush()",
" return proc.pid",
"",
"def main():",
" log('simple restart started reason=' + str(reason) + ' script=' + script_path + ' python=' + python_exe + ' expected_sha=' + str(expected_sha) + ' backup=' + str(backup_path))",
" time.sleep(2.5)",
" if expected_sha:",
" try:",
" actual = file_sha256(script_path)",
" log('active sha=' + actual + ' expected=' + str(expected_sha))",
" except Exception as exc:",
" log('sha check failed: ' + repr(exc))",
" release_port()",
" start_server()",
" # Do not call /health or /ping here and do not roll back. Previous versions",
" # produced false timeout failures even though Flask was already running.",
" log('simple restart helper finished; browser/watchdog will verify /ping.json')",
" return 0",
"",
"if __name__ == '__main__':",
" raise SystemExit(main())",
"",
]) + "\n"
def validate_restart_helper_source(helper_code):
try:
compile(helper_code, "<tripserver_restart_helper>", "exec")
return True, "OK"
except Exception as exc:
return False, repr(exc)
def write_restart_helper(script_path, python_exe, work_dir, backup_path=None, expected_sha=None, reason=""):
helper_code = build_restart_helper_code(script_path, python_exe, work_dir, os.getpid(), backup_path, expected_sha, reason)
ok, info = validate_restart_helper_source(helper_code)
if not ok:
raise RuntimeError("Generated restart helper is invalid: " + info)
ensure_parent_dir(ADMIN_RESTART_HELPER_PATH)
with open(ADMIN_RESTART_HELPER_PATH, "w", encoding="utf-8") as fh:
fh.write(helper_code)
py_compile.compile(ADMIN_RESTART_HELPER_PATH, doraise=True)
return ADMIN_RESTART_HELPER_PATH
def restart_process_later(backup_path=None, expected_sha=None, reason="manual_restart"):
"""Hard self-restart for Windows/iPhone admin workflow.
v23: the helper can force-release a stuck port safely and roll back to the previous admin
backup if the newly uploaded admin does not pass /ping.json after boot.
"""
time.sleep(1.2)
script_path = os.path.abspath(ADMIN_SERVER_PATH)
python_exe = sys.executable or "python"
work_dir = os.path.dirname(script_path) or BASE_DIR
try:
log_admin_restart(f"hard restart requested | reason={reason} | version={ADMIN_PANEL_VERSION} | script={script_path} | python={python_exe} | pid={os.getpid()} | backup={backup_path} | expected_sha={expected_sha} | argv={sys.argv}")
helper_path = write_restart_helper(script_path, python_exe, work_dir, backup_path=backup_path, expected_sha=expected_sha, reason=reason)
flags = 0
if os.name == "nt":
flags = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) | getattr(subprocess, "DETACHED_PROCESS", 0) | getattr(subprocess, "CREATE_BREAKAWAY_FROM_JOB", 0)
subprocess.Popen(
[python_exe, helper_path],
cwd=work_dir,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
close_fds=(False if os.name == "nt" else True),
creationflags=flags,
start_new_session=(os.name != "nt"),
)
log_admin_restart("hard restart helper launched: " + helper_path)
os._exit(0)
except Exception as exc:
log_admin_restart("hard restart helper failed: " + repr(exc))
try:
subprocess.Popen([python_exe, script_path], cwd=work_dir, close_fds=True, start_new_session=(os.name != "nt"))
os._exit(0)
except Exception as exc2:
log_admin_restart("direct restart fallback failed: " + repr(exc2))
try:
os.execv(python_exe, [python_exe, script_path])
except Exception as exc3:
log_admin_restart("execv fallback failed: " + repr(exc3))
os._exit(1)
def launch_restart_helper_via_scheduler(helper_path, python_exe, work_dir):
"""Launch restart helper as an independent Windows Scheduled Task.
This is more reliable than subprocess.Popen from the Flask process because the
helper is not a child of the admin process it is about to kill. On Windows,
self-restart helpers launched as child processes can be terminated together
with the old server or stuck in the same job/process group.
"""
if os.name != "nt":
raise RuntimeError("Task Scheduler restart is supported only on Windows")
quiet_python = get_pythonw_executable(python_exe)
task_command = f'"{quiet_python}" "{helper_path}"'
# Create a normal on-demand task. The time is deliberately far in the future;
# we immediately run it with /Run, and then overwrite it on the next update.
create_cmd = [
"schtasks", "/Create",
"/TN", RESTART_TASK_NAME,
"/TR", task_command,
"/SC", "ONCE",
"/ST", "23:59",
"/F",
]
cp_create = subprocess.run(create_cmd, capture_output=True, text=True, shell=False, timeout=20)
create_out = (cp_create.stdout or cp_create.stderr or "").strip()
if cp_create.returncode != 0:
raise RuntimeError("Failed to create restart task: " + (create_out or str(cp_create.returncode)))
cp_run = subprocess.run(["schtasks", "/Run", "/TN", RESTART_TASK_NAME], capture_output=True, text=True, shell=False, timeout=20)
run_out = (cp_run.stdout or cp_run.stderr or "").strip()
if cp_run.returncode != 0:
raise RuntimeError("Failed to run restart task: " + (run_out or str(cp_run.returncode)))
log_admin_restart("restart helper launched via Task Scheduler | task=" + RESTART_TASK_NAME + " | python=" + quiet_python + " | create=" + create_out + " | run=" + run_out)
return {"task": RESTART_TASK_NAME, "python": quiet_python, "create": create_out, "run": run_out}
def launch_safe_external_restart(backup_path=None, expected_sha=None, reason="safe_external_restart"):
"""Launch a detached restart helper through Task Scheduler.
v27: this is the important architectural fix. The helper is no longer a
direct child of the Flask/admin process. It is started by Windows Task
Scheduler, then it can safely kill the old process on port 8000 and start the
new admin without dying with its parent.
"""
script_path = os.path.abspath(ADMIN_SERVER_PATH)
python_exe = sys.executable or "python"
work_dir = os.path.dirname(script_path) or BASE_DIR
log_admin_restart(
f"scheduler restart requested | reason={reason} | version={ADMIN_PANEL_VERSION} | script={script_path} | python={python_exe} | pid={os.getpid()} | backup={backup_path} | expected_sha={expected_sha}"
)
helper_path = write_restart_helper(script_path, python_exe, work_dir, backup_path=backup_path, expected_sha=expected_sha, reason=reason)
try:
task_info = launch_restart_helper_via_scheduler(helper_path, python_exe, work_dir)
return helper_path + " | task=" + str(task_info.get("task")) + " | python=" + str(task_info.get("python"))
except Exception as exc:
# Fallback only: use direct detached Popen if Task Scheduler is unavailable.
# This preserves some functionality, but the scheduled task path is the
# intended reliable path on the user's Windows server.
log_admin_restart("Task Scheduler restart launch failed, falling back to detached Popen: " + repr(exc))
flags = 0
if os.name == "nt":
flags = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) | getattr(subprocess, "DETACHED_PROCESS", 0) | getattr(subprocess, "CREATE_BREAKAWAY_FROM_JOB", 0)
quiet_python = get_pythonw_executable(python_exe)
subprocess.Popen(
[quiet_python, helper_path],
cwd=work_dir,
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
close_fds=(False if os.name == "nt" else True),
creationflags=flags,
start_new_session=(os.name != "nt"),
)
log_admin_restart("restart helper launched through fallback detached Popen: " + helper_path)
return helper_path + " | fallback=detached-popen"
def build_watchdog_code(script_path, python_exe, work_dir):
"""Build a one-shot watchdog script for Windows Task Scheduler.
v28 change: if the port is open but /ping.json fails repeatedly, the watchdog
treats it as a stuck admin process, kills the process that owns port 8000, and
starts admin_server.py again. This is the case that forced a physical reboot.
"""
return "\n".join([
"# -*- coding: utf-8 -*-",
"import os",
"import sys",
"import time",
"import socket",
"import subprocess",
"import urllib.request",
"import json",
"",
f"script_path = {script_path!r}",
f"python_exe = {python_exe!r}",
f"work_dir = {work_dir!r}",
f"admin_port = {ADMIN_PORT!r}",
f"health_url = {'http://127.0.0.1:%s/ping.json' % ADMIN_PORT!r}",
f"log_path = {WATCHDOG_LOG_PATH!r}",
f"state_path = {WATCHDOG_STATE_PATH!r}",
f"cooldown_seconds = {WATCHDOG_MIN_RESTART_INTERVAL_SECONDS!r}",
"",
"def log(message):",
" try:",
" os.makedirs(os.path.dirname(log_path), exist_ok=True)",
" with open(log_path, 'a', encoding='utf-8') as fh:",
" fh.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' | watchdog-v28 | ' + str(message) + '\\n')",
" except Exception:",
" pass",
"",
"def load_state():",
" try:",
" raw = open(state_path, 'r', encoding='utf-8').read().strip()",
" if not raw:",
" return {}",
" try:",
" data = json.loads(raw)",
" return data if isinstance(data, dict) else {}",
" except Exception:",
" return {'last_start': float(raw)}",
" except Exception:",
" return {}",
"",
"def save_state(state):",
" try:",
" os.makedirs(os.path.dirname(state_path), exist_ok=True)",
" with open(state_path, 'w', encoding='utf-8') as fh:",
" json.dump(state, fh)",
" except Exception:",
" pass",
"",
"def port_open():",
" try:",
" with socket.create_connection(('127.0.0.1', int(admin_port)), timeout=2.0):",
" return True",
" except Exception:",
" return False",
"",
"def pids_on_port():",
" pids = []",
" if os.name != 'nt':",
" return pids",
" try:",
" cp = subprocess.run(['netstat', '-ano', '-p', 'tcp'], capture_output=True, text=True, shell=False, timeout=10)",
" out = (cp.stdout or '') + '\\n' + (cp.stderr or '')",
" marker = ':' + str(admin_port)",
" for line in out.splitlines():",
" if marker not in line or 'listen' not in line.lower():",
" continue",
" parts = line.split()",
" if parts:",
" try:",
" pid = int(parts[-1])",
" if pid and pid not in pids:",
" pids.append(pid)",
" except Exception:",
" pass",
" except Exception as exc:",
" log('netstat failed: ' + repr(exc))",
" return pids",
"",
"def kill_port_owner(pid):",
" # v23: avoid /T even in watchdog. If run-once was launched from the admin",
" # panel, /T could kill the watchdog child together with the stuck admin.",
" try:",
" pid = int(pid)",
" except Exception:",
" return False",
" if pid <= 0 or pid == os.getpid():",
" log('skip killing pid=' + str(pid) + ' self=' + str(os.getpid()))",
" return False",
" try:",
" if os.name == 'nt':",
" cp = subprocess.run(['taskkill', '/PID', str(pid), '/F'], capture_output=True, text=True, shell=False, timeout=12)",
" log('taskkill-port-owner pid=' + str(pid) + ' rc=' + str(cp.returncode) + ' out=' + ((cp.stdout or cp.stderr or "").strip()[:500]))",
" return cp.returncode == 0",
" os.kill(pid, 9)",
" return True",
" except Exception as exc:",
" log('kill port owner failed pid=' + str(pid) + ' err=' + repr(exc))",
" return False",
"",
"def wait_port_closed(timeout=12):",
" deadline = time.time() + timeout",
" while time.time() < deadline:",
" if not port_open():",
" return True",
" time.sleep(0.6)",
" return not port_open()",
"",
"def health_ok():",
" try:",
" req = urllib.request.Request(health_url, headers={'Accept': 'application/json'})",
" with urllib.request.urlopen(req, timeout=4.0) as resp:",
" raw = resp.read(8192).decode('utf-8', errors='replace')",
" data = json.loads(raw)",
" return bool(data.get('ok')), 'OK' if data.get('ok') else raw[:500]",
" except Exception as exc:",
" return False, repr(exc)",
"",
"def recently_started(state):",
" try:",
" last = float(state.get('last_start') or 0)",
" return (time.time() - last) < float(cooldown_seconds)",
" except Exception:",
" return False",
"",
"def mark_started(state):",
" state['last_start'] = time.time()",
" state['failure_count'] = 0",
" save_state(state)",
"",
"def script_compiles():",
" try:",
" import py_compile",
" py_compile.compile(script_path, doraise=True)",
" return True, 'OK'",
" except Exception as exc:",
" return False, repr(exc)",
"",
"def launch_admin(state):",
" flags = 0",
" if os.name == 'nt':",
" flags = getattr(subprocess, 'CREATE_NEW_PROCESS_GROUP', 0) | getattr(subprocess, 'DETACHED_PROCESS', 0)",
" env = os.environ.copy()",
" env.setdefault('PYTHONIOENCODING', 'utf-8')",
" os.makedirs(os.path.dirname(log_path), exist_ok=True)",
" with open(log_path, 'a', encoding='utf-8') as log_file:",
" log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' | watchdog-v28 | starting admin: ' + script_path + '\\n')",
" log_file.flush()",
" proc = subprocess.Popen(",
" [python_exe, script_path],",
" cwd=work_dir,",
" stdin=subprocess.DEVNULL,",
" stdout=log_file,",
" stderr=log_file,",
" close_fds=(False if os.name == 'nt' else True),",
" creationflags=flags,",
" start_new_session=(os.name != 'nt'),",
" env=env,",
" )",
" log_file.write(time.strftime('%Y-%m-%d %H:%M:%S') + ' | watchdog-v28 | started pid=' + str(proc.pid) + '\\n')",
" mark_started(state)",
"",
"def main():",
" state = load_state()",
" ok, info = health_ok()",
" if ok:",
" if state.get('failure_count'):",
" state['failure_count'] = 0",
" save_state(state)",
" return 0",
" fail_count = int(state.get('failure_count') or 0) + 1",
" state['failure_count'] = fail_count",
" save_state(state)",
" is_port_open = port_open()",
" log('health failed count=' + str(fail_count) + ' port_open=' + str(is_port_open) + ' info=' + str(info))",
" if recently_started(state):",
" log('restart cooldown active; skip this minute')",
" return 3",
" if is_port_open:",
" pids = pids_on_port()",
" if fail_count < 2:",
" log('port is open but health failed; waiting for second failure before hard kill. pids=' + str(pids))",
" return 2",
" log('port open + repeated health failure; killing stuck pids=' + str(pids))",
" for pid in pids:",
" kill_port_owner(pid)",
" wait_port_closed(14)",
" if not os.path.exists(script_path):",
" log('admin script not found: ' + script_path)",
" return 4",
" ok_compile, compile_info = script_compiles()",
" if not ok_compile:",
" log('admin script does not compile; not starting. info=' + str(compile_info))",
" return 5",
" try:",
" launch_admin(state)",
" return 10",
" except Exception as exc:",
" log('failed to launch admin: ' + repr(exc))",
" return 6",
"",
"if __name__ == '__main__':",
" raise SystemExit(main())",
"",
]) + "\n"
def validate_watchdog_source(watchdog_code):
try:
compile(watchdog_code, "<tripserver_watchdog>", "exec")
return True, "OK"
except Exception as exc:
return False, repr(exc)
def write_watchdog_script():
script_path = os.path.abspath(ADMIN_SERVER_PATH)
python_exe = sys.executable or "python"
work_dir = os.path.dirname(script_path) or BASE_DIR
source = build_watchdog_code(script_path, python_exe, work_dir)
ok, info = validate_watchdog_source(source)
if not ok:
raise RuntimeError("Generated watchdog script is invalid: " + info)
ensure_parent_dir(WATCHDOG_SCRIPT_PATH)
with open(WATCHDOG_SCRIPT_PATH, "w", encoding="utf-8") as fh:
fh.write(source)
py_compile.compile(WATCHDOG_SCRIPT_PATH, doraise=True)
return WATCHDOG_SCRIPT_PATH
def read_log_tail(path, max_lines=18):
try:
with open(path, "r", encoding="utf-8", errors="replace") as fh:
lines = fh.readlines()
return "".join(lines[-max_lines:]).strip()
except Exception:
return ""
def safe_int(value, default, min_value=None, max_value=None):
try:
parsed = int(value)
except Exception:
parsed = default
if min_value is not None:
parsed = max(min_value, parsed)
if max_value is not None:
parsed = min(max_value, parsed)
return parsed
def log_file_summary(log_key, max_lines=LOG_TAIL_DEFAULT_LINES):
meta = LOG_FILE_REGISTRY.get(log_key)
if not meta:
return None
path = meta["path"]
exists = os.path.exists(path)
size = os.path.getsize(path) if exists else 0
updated = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(os.path.getmtime(path))) if exists else "לא קיים עדיין"
tail = read_log_tail(path, max_lines=max_lines) if exists else "אין עדיין לוג. הוא יווצר אחרי פעולה ראשונה."
return {
"key": log_key,
"label": meta["label"],
"note": meta.get("note", ""),
"path": path,
"exists": exists,
"size": size,
"size_label": format_bytes(size),
"updated": updated,
"tail": tail,
}
def build_logs_payload(max_lines=LOG_TAIL_DEFAULT_LINES):
max_lines = safe_int(max_lines, LOG_TAIL_DEFAULT_LINES, 20, LOG_TAIL_MAX_LINES)
items = []
for key in LOG_FILE_REGISTRY:
info = log_file_summary(key, max_lines=max_lines)
if info:
items.append(info)
return {
"ok": True,
"version": ADMIN_PANEL_VERSION,
"checked_at": time.strftime("%Y-%m-%d %H:%M:%S"),
"lines": max_lines,
"logs": items,
}
def render_admin_logs():
payload = build_logs_payload(request.args.get("lines", LOG_TAIL_DEFAULT_LINES))
cards = []
for info in payload["logs"]:
badge_cls = "ok" if info["exists"] else "warn"
badge_text = "קיים" if info["exists"] else "לא נוצר עדיין"
tail = html_lib.escape(info["tail"] or "אין תוכן להצגה.").replace("\n", "<br>")
cards.append(f'''
<div class="log-card">
<div class="log-head">
<div><h2>{html_lib.escape(info["label"])}</h2><p>{html_lib.escape(info["note"])}</p></div>
<span class="badge {badge_cls}">{badge_text}</span>
</div>
<div class="meta"><b>נתיב</b><code>{html_lib.escape(info["path"])}</code></div>
<div class="stats"><span>גודל: <b>{html_lib.escape(info["size_label"])}</b></span><span>עודכן: <b>{html_lib.escape(info["updated"])}</b></span></div>
<pre>{tail}</pre>
</div>''')
return f'''
<!DOCTYPE html>
<html lang="he" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>לוגים TripServer</title>
<style>
body{{margin:0;background:#07111f;color:white;font-family:Arial;padding:22px}}.wrap{{max-width:1120px;margin:auto}}a{{color:#dbeafe;text-decoration:none}}.hero{{background:linear-gradient(135deg,#1e3a8a,#0f172a 62%,#111827);border:1px solid #1d4ed8;border-radius:28px;padding:22px;margin-bottom:16px;box-shadow:0 20px 70px rgba(0,0,0,.28)}}h1{{margin:0 0 8px;font-size:34px}}p{{color:#cbd5e1;line-height:1.6}}.actions{{display:flex;gap:10px;flex-wrap:wrap;margin-top:14px}}.actions a{{background:#2563eb;color:white;border-radius:999px;padding:10px 14px;font-weight:900}}.actions a.secondary{{background:#1e293b;border:1px solid #334155}}.log-card{{background:#101c31;border:1px solid #253650;border-radius:24px;padding:18px;margin:14px 0;box-shadow:0 18px 55px rgba(0,0,0,.22)}}.log-head{{display:flex;align-items:flex-start;justify-content:space-between;gap:14px}}.log-head h2{{margin:0 0 6px;font-size:24px}}.badge{{white-space:nowrap;border-radius:999px;padding:7px 11px;font-weight:900}}.badge.ok{{background:#14532d;color:#bbf7d0;border:1px solid #22c55e}}.badge.warn{{background:#451a03;color:#fed7aa;border:1px solid #f97316}}.meta{{margin-top:10px;color:#cbd5e1}}code{{direction:ltr;display:block;background:#0b1220;border:1px solid #334155;border-radius:12px;padding:10px;color:#fde68a;margin-top:6px;white-space:normal}}.stats{{display:flex;gap:12px;flex-wrap:wrap;color:#cbd5e1;margin:12px 0}}.stats span{{background:#0b1220;border:1px solid #334155;border-radius:999px;padding:8px 10px}}pre{{direction:ltr;text-align:left;white-space:pre-wrap;background:#020617;color:#fde68a;border:1px solid #334155;border-radius:16px;padding:14px;overflow:auto;max-height:420px;font-size:13px;line-height:1.55}}@media(max-width:700px){{body{{padding:14px}}.hero{{padding:18px;border-radius:22px}}h1{{font-size:28px}}.log-head{{display:block}}}}
</style></head><body><div class="wrap"><div class="hero"><a href="/admin">⬅ חזרה לפאנל</a><h1>📜 מרכז לוגים</h1><p>צפייה נוחה בלוגי האדמין, עדכונים ופעולות מערכת בלי לפתוח קבצים ידנית במחשב. המסך לקריאה בלבד ולא מוחק או משנה שום דבר.</p><div class="actions"><a href="/admin/logs">רענן</a><a class="secondary" href="/admin/logs?lines=250">הצג 250 שורות</a><a class="secondary" href="/admin/logs?lines=500">הצג 500 שורות</a><a class="secondary" href="/admin/logs.json" target="_blank">JSON טכני</a><a class="secondary" href="/admin/self-test">בדיקה עצמית</a></div></div>{''.join(cards)}</div></body></html>
'''
def query_watchdog_task():
if os.name != "nt":
return {"supported": False, "installed": False, "ok": False, "details": "Task Scheduler זמין רק על Windows."}
try:
completed = subprocess.run(["schtasks", "/Query", "/TN", WATCHDOG_TASK_NAME, "/FO", "LIST"], capture_output=True, text=True, shell=False, timeout=12)
output = (completed.stdout or completed.stderr or "").strip()
return {"supported": True, "installed": completed.returncode == 0, "ok": completed.returncode == 0, "details": output}
except Exception as exc:
return {"supported": True, "installed": False, "ok": False, "details": repr(exc)}
def watchdog_status(include_log=True, include_task=True):
generated = build_watchdog_code(os.path.abspath(ADMIN_SERVER_PATH), sys.executable or "python", os.path.dirname(os.path.abspath(ADMIN_SERVER_PATH)) or BASE_DIR)
source_ok, source_info = validate_watchdog_source(generated)
file_exists = os.path.exists(WATCHDOG_SCRIPT_PATH)
file_compiles = False
file_info = "לא נכתב עדיין"
if file_exists:
try:
py_compile.compile(WATCHDOG_SCRIPT_PATH, doraise=True)
file_compiles = True
file_info = "OK"
except Exception as exc:
file_info = repr(exc)
task = query_watchdog_task() if include_task else {"supported": None, "installed": None, "ok": None, "details": "not queried"}
return {
"source_ok": source_ok,
"source_status": source_info,
"script_path": WATCHDOG_SCRIPT_PATH,
"script_exists": file_exists,
"script_compiles": file_compiles,
"script_status": file_info,
"task_name": WATCHDOG_TASK_NAME,
"task_supported": task.get("supported"),
"task_installed": task.get("installed"),
"task_status": task.get("details", ""),
"interval_minutes": WATCHDOG_INTERVAL_MINUTES,
"cooldown_seconds": WATCHDOG_MIN_RESTART_INTERVAL_SECONDS,
"quiet_python": get_pythonw_executable(sys.executable or "python"),
"log_path": WATCHDOG_LOG_PATH,
"log_tail": read_log_tail(WATCHDOG_LOG_PATH) if include_log else "",
}
def install_watchdog_task():
script_path = write_watchdog_script()
if os.name != "nt":
return False, "קובץ ה־Watchdog נכתב, אבל התקנת Task Scheduler נתמכת רק על Windows.", f"script={script_path}"
# Use pythonw.exe for the scheduled watchdog so Windows will not flash a black
# console window and steal keyboard focus every run.
quiet_python = get_pythonw_executable(sys.executable or "python")
task_command = f'"{quiet_python}" "{script_path}"'
cmd = ["schtasks", "/Create", "/TN", WATCHDOG_TASK_NAME, "/TR", task_command, "/SC", "MINUTE", "/MO", str(max(1, WATCHDOG_INTERVAL_MINUTES)), "/F"]
completed = subprocess.run(cmd, capture_output=True, text=True, shell=False, timeout=20)
output = (completed.stdout or completed.stderr or "").strip()
if completed.returncode != 0:
return False, "קובץ ה־Watchdog נכתב, אבל התקנת המשימה נכשלה.", output or f"return code: {completed.returncode}"
return True, f"Watchdog שקט הותקן כמשימה מתוזמנת שרצה כל {max(1, WATCHDOG_INTERVAL_MINUTES)} דקות עם pythonw.exe, בלי חלון שחור קופץ.", output
def uninstall_watchdog_task():
if os.name != "nt":
return False, "הסרת Task Scheduler נתמכת רק על Windows.", ""
completed = subprocess.run(["schtasks", "/Delete", "/TN", WATCHDOG_TASK_NAME, "/F"], capture_output=True, text=True, shell=False, timeout=20)
output = (completed.stdout or completed.stderr or "").strip()
if completed.returncode != 0:
return False, "לא הצלחתי להסיר את משימת ה־Watchdog, או שהיא לא קיימת.", output or f"return code: {completed.returncode}"
return True, "משימת ה־Watchdog הוסרה.", output
def run_watchdog_once_now():
script_path = write_watchdog_script()
completed = subprocess.run([sys.executable or "python", script_path], capture_output=True, text=True, shell=False, timeout=25)
output = (completed.stdout or completed.stderr or "").strip()
ok = completed.returncode in {0, 2, 3, 10}
return ok, f"Watchdog רץ פעם אחת והחזיר קוד {completed.returncode}.", output or read_log_tail(WATCHDOG_LOG_PATH)
def render_admin_watchdog():
status = watchdog_status(include_log=True)
installed = bool(status.get("task_installed"))
script_ok = bool(status.get("script_exists")) and bool(status.get("script_compiles"))
source_ok = bool(status.get("source_ok"))
state_badge = "מותקן ופעיל" if installed else "לא מותקן עדיין"
state_cls = "ok" if installed and source_ok else "warn"
task_text = html_lib.escape(status.get("task_status") or "")
log_tail = html_lib.escape(status.get("log_tail") or "אין עדיין לוג.").replace("\n", "<br>")
script_label = "✅ קיים ותקין" if script_ok else "⚠️ עדיין לא נכתב/לא תקין"
task_label = "✅ מותקנת" if installed else "⚠️ לא מותקנת"
return f"""
<!DOCTYPE html>
<html lang="he" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>TripServer Watchdog</title>
<style>
body{{margin:0;background:#0f172a;color:white;font-family:Arial;padding:22px}}.wrap{{max-width:1050px;margin:auto}}a{{color:#bfdbfe;text-decoration:none}}.hero{{background:linear-gradient(135deg,#14532d,#0f172a 60%,#111827);border:1px solid #166534;border-radius:28px;padding:22px;margin-bottom:16px;box-shadow:0 20px 70px rgba(0,0,0,.28)}}h1{{margin:0 0 8px;font-size:34px}}p{{color:#cbd5e1;line-height:1.7}}.badge{{display:inline-block;border-radius:999px;padding:8px 14px;font-weight:900;margin:8px 0}}.badge.ok{{background:#064e3b;color:#bbf7d0;border:1px solid #22c55e}}.badge.warn{{background:#713f12;color:#fde68a;border:1px solid #f59e0b}}.grid{{display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:14px}}.card{{background:#111827;border:1px solid #334155;border-radius:20px;padding:16px;margin-top:14px}}.card b{{display:block;font-size:18px;margin-bottom:8px}}code,pre{{direction:ltr;text-align:left;white-space:pre-wrap;word-break:break-word}}pre{{background:#020617;border:1px solid #334155;color:#fde68a;border-radius:14px;padding:13px;max-height:340px;overflow:auto}}button{{border:0;border-radius:14px;padding:13px 16px;font-weight:950;color:white;background:#2563eb;margin:8px 8px 0 0;cursor:pointer}}button.orange{{background:#ea580c}}button.danger{{background:#dc2626}}.actions{{display:flex;gap:10px;flex-wrap:wrap;margin-top:12px}}.actions a{{display:inline-flex;background:#172033;border:1px solid #334155;border-radius:999px;padding:10px 14px;font-weight:900}}.note{{background:#0b1220;border:1px solid #334155;border-radius:18px;padding:14px;margin:14px 0;color:#cbd5e1;line-height:1.7}}@media(max-width:700px){{body{{padding:14px}}h1{{font-size:28px}}}}
</style></head><body><div class="wrap"><div class="hero"><a href="/admin">⬅ חזרה לפאנל</a><h1>🛡️ Watchdog לשמירת פאנל האדמין</h1><p>שכבת ביטחון חיצונית: Windows מריץ סקריפט שקט כל כמה דקות. אם הפאנל לא עונה ב־localhost:{ADMIN_PORT}, הוא מפעיל מחדש את admin_server.py לבד.</p><span class="badge {state_cls}">{state_badge}</span><div class="actions"><a href="/admin/self-test">בדיקה עצמית</a><a href="/admin/health">Health</a><a href="/admin/status">סטטוס קבצים</a></div></div>
<div class="grid"><div class="card"><b>סקריפט Watchdog</b><p>{script_label}</p><code>{html_lib.escape(status.get('script_path',''))}</code></div><div class="card"><b>משימה מתוזמנת</b><p>{task_label}</p><p>שם: <code>{html_lib.escape(status.get('task_name',''))}</code></p><p>תדירות: כל {status.get('interval_minutes')} דקות</p></div><div class="card"><b>הרצה שקטה</b><p>מותקן דרך <code>pythonw.exe</code> כדי לא לפתוח חלון CMD שחור ולא לגנוב פוקוס בזמן הקלדה.</p><code>{html_lib.escape(status.get('quiet_python',''))}</code></div><div class="card"><b>Cooldown</b><p>{status.get('cooldown_seconds')} שניות בין ניסיונות הפעלה כדי למנוע לופ.</p></div></div>
<div class="note"><b>מה זה עושה בפועל?</b><br>ה־Watchdog לא מחליף קבצים, לא מכבה מחשב ולא עושה restart למחשב. הוא רק בודק אם פאנל האדמין חי, ואם לא — מפעיל את קובץ האדמין הפעיל מחדש. זה בדיוק הכיסוי למקרה שבו העלאת admin או restart נפלו ואתה לא רוצה ללכת פיזית למחשב.</div>
<div class="card"><b>פעולות</b><form action="/watchdog/install" method="post" onsubmit="return confirm('להתקין Watchdog מתוזמן שרץ שקט כל כמה דקות?')"><button>התקן / עדכן Watchdog</button></form><form action="/watchdog/run-once" method="post" onsubmit="return confirm('להריץ בדיקת Watchdog חד־פעמית עכשיו?')"><button class="orange">הרץ פעם אחת עכשיו</button></form><form action="/watchdog/uninstall" method="post" onsubmit="return confirm('להסיר את משימת ה־Watchdog?')"><button class="danger">הסר Watchdog</button></form></div>
<div class="card"><b>סטטוס Task Scheduler</b><pre>{task_text or 'אין מידע.'}</pre></div><div class="card"><b>לוג אחרון</b><pre>{log_tail}</pre></div></div></body></html>
"""
def get_client_ip():
# Use the direct remote address rather than trusting arbitrary X-Forwarded-For headers.
return request.remote_addr or ""
def is_private_or_loopback_ip(ip_text):
try:
ip = ipaddress.ip_address((ip_text or "").split("%", 1)[0])
tailscale_ipv4 = ip.version == 4 and ip in ipaddress.ip_network("100.64.0.0/10")
tailscale_ipv6 = ip.version == 6 and ip in ipaddress.ip_network("fd7a:115c:a1e0::/48")
return ip.is_loopback or ip.is_private or ip.is_link_local or tailscale_ipv4 or tailscale_ipv6
except Exception:
return False
def power_action_allowed():
if not POWER_ACTIONS_ENABLED:
return False, "פעולות כיבוי/ריסטארט מחשב כבויות בהגדרת TRIPSERVER_ENABLE_POWER_ACTIONS."
ip = get_client_ip()
if not is_private_or_loopback_ip(ip):
return False, f"פעולת מחשב חסומה מכתובת שאינה מקומית/רשת ביתית: {ip}"
if os.name != "nt":
return False, "פעולות מחשב נתמכות כאן רק על Windows."
return True, "OK"
def normalize_power_delay(raw_value):
try:
delay = int(raw_value)
except Exception:
delay = POWER_DEFAULT_DELAY_SECONDS
return max(POWER_MIN_DELAY_SECONDS, min(POWER_MAX_DELAY_SECONDS, delay))
def run_power_command(action, delay_seconds):
if action == "shutdown":
mode = "/s"
label = "כיבוי מחשב"
elif action == "reboot":
mode = "/r"
label = "ריסטארט למחשב"
else:
raise ValueError("unknown power action")
delay = normalize_power_delay(delay_seconds)
comment = f"TripServer Admin scheduled {label}"
cmd = ["shutdown", mode, "/t", str(delay), "/c", comment]
completed = subprocess.run(cmd, capture_output=True, text=True, shell=False)
if completed.returncode != 0:
raise RuntimeError((completed.stderr or completed.stdout or "shutdown command failed").strip())
log_admin_restart(f"POWER {action} scheduled in {delay}s by {get_client_ip()}")
return label, delay, completed.stdout.strip() or completed.stderr.strip()
def cancel_power_command():
completed = subprocess.run(["shutdown", "/a"], capture_output=True, text=True, shell=False)
# Windows returns non-zero when there is no pending shutdown. Treat it as useful feedback, not a server crash.
log_admin_restart(f"POWER cancel requested by {get_client_ip()} | rc={completed.returncode} | out={completed.stdout.strip()} | err={completed.stderr.strip()}")
return completed.returncode, (completed.stdout or completed.stderr or "").strip()
def arm_admin_update_failsafe_restart():
"""Schedule a Windows reboot before applying an admin update."""
if not ADMIN_UPDATE_FAILSAFE_ENABLED:
msg = "Fail Safe כבוי בהגדרת TRIPSERVER_ADMIN_UPDATE_FAILSAFE."
write_admin_failsafe_status("arm_failed", msg)
return False, msg
allowed, reason = power_action_allowed()
if not allowed:
write_admin_failsafe_status("arm_failed", reason)
return False, reason
delay = normalize_power_delay(ADMIN_UPDATE_FAILSAFE_RESTART_SECONDS)
try:
subprocess.run(["shutdown", "/a"], capture_output=True, text=True, shell=False)
except Exception:
pass
try:
label, final_delay, output = run_power_command("reboot", delay)
msg = f"{label} Fail Safe נקבע לעוד {final_delay} שניות."
write_admin_failsafe_status("armed", msg, delay_seconds=final_delay, windows_output=output or "OK")
return True, f"{msg} פלט Windows: {output or 'OK'}"
except Exception as exc:
msg = "Fail Safe לא הצליח לקבוע ריסטארט Windows: " + repr(exc)
write_admin_failsafe_status("arm_failed", msg)
return False, msg
def camera_access_allowed():
if not CAMERA_ENABLED:
return False, "מצלמת החדר כבויה בהגדרת TRIPSERVER_ENABLE_WEBCAM."
ip = get_client_ip()
if not is_private_or_loopback_ip(ip):
return False, f"גישה למצלמה חסומה מכתובת שאינה מקומית/רשת ביתית/Tailscale: {ip}"
return True, "OK"
def camera_engine_status():
status = {"enabled": CAMERA_ENABLED, "opencv": False, "opencv_version": "", "camera_index": CAMERA_INDEX}
try:
import cv2 # noqa: F401
status["opencv"] = True
status["opencv_version"] = getattr(cv2, "__version__", "")
except Exception as exc:
status["opencv"] = False
status["error"] = str(exc)
return status
class WebcamManager:
def __init__(self):
self.lock = threading.RLock()
self.cap = None
self.opened_at = 0.0
self.last_frame_at = 0.0
self.frames = 0
self.last_error = ""
self.stop_requested = False
def _import_cv2(self):
import cv2
return cv2
def _open_locked(self):
cv2 = self._import_cv2()
if self.cap is not None and self.cap.isOpened():
return cv2
self.stop_requested = False
backend = getattr(cv2, "CAP_DSHOW", 0) if os.name == "nt" else 0
cap = cv2.VideoCapture(CAMERA_INDEX, backend)
try:
cap.set(getattr(cv2, "CAP_PROP_FRAME_WIDTH", 3), CAMERA_WIDTH)
cap.set(getattr(cv2, "CAP_PROP_FRAME_HEIGHT", 4), CAMERA_HEIGHT)
cap.set(getattr(cv2, "CAP_PROP_FPS", 5), CAMERA_FPS)
except Exception:
pass
if not cap or not cap.isOpened():
try:
cap.release()
except Exception:
pass
self.last_error = f"לא הצלחתי לפתוח מצלמה index={CAMERA_INDEX}. בדוק הרשאות Windows או מצלמה תפוסה באפליקציה אחרת."
raise RuntimeError(self.last_error)
self.cap = cap
self.opened_at = time.time()
self.frames = 0
self.last_error = ""
return cv2
def read_jpeg(self):
with self.lock:
# v21 fix: stop_requested is only for the CURRENT stream. In older builds,
# once /camera/stop or a browser disconnect set it to True, future starts
# were blocked forever until the whole admin process rebooted. If the
# capture was already released, allow a new stream to open cleanly.
if self.cap is None or not getattr(self.cap, "isOpened", lambda: False)():
self.stop_requested = False
elif self.stop_requested:
raise RuntimeError("המצלמה כובתה ידנית")
cv2 = self._open_locked()
ok, frame = self.cap.read()
if not ok or frame is None:
time.sleep(0.15)
ok, frame = self.cap.read()
if not ok or frame is None:
self.last_error = "לא התקבלה תמונה מהמצלמה."
raise RuntimeError(self.last_error)
ok, encoded = cv2.imencode(".jpg", frame, [int(getattr(cv2, "IMWRITE_JPEG_QUALITY", 1)), CAMERA_JPEG_QUALITY])
if not ok:
self.last_error = "קידוד תמונת המצלמה ל־JPG נכשל."
raise RuntimeError(self.last_error)
self.frames += 1
self.last_frame_at = time.time()
return encoded.tobytes()
def stop(self):
with self.lock:
self.stop_requested = True
if self.cap is not None:
try:
self.cap.release()
except Exception:
pass
self.cap = None
self.opened_at = 0.0
def prepare_new_stream(self):
"""Release stale webcam handles and allow the next stream to start.
This is intentionally lighter than a full camera reset: it does not stop
audio or recording. It is used before assigning a new MJPEG src in the UI.
"""
with self.lock:
if self.cap is not None:
try:
self.cap.release()
except Exception:
pass
self.cap = None
self.opened_at = 0.0
self.stop_requested = False
self.last_error = ""
return True
def force_reset(self):
"""Stop all current webcam capture state and clear sticky errors."""
self.stop()
with self.lock:
self.stop_requested = False
self.last_error = ""
self.frames = 0
self.last_frame_at = 0.0
return True
def status(self):
with self.lock:
active = bool(self.cap is not None and self.cap.isOpened())
return {
"enabled": CAMERA_ENABLED,
"active": active,
"camera_index": CAMERA_INDEX,
"width": CAMERA_WIDTH,
"height": CAMERA_HEIGHT,
"fps": CAMERA_FPS,
"jpeg_quality": CAMERA_JPEG_QUALITY,
"opened_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.opened_at)) if self.opened_at else "",
"last_frame_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.last_frame_at)) if self.last_frame_at else "",
"frames": self.frames,
"last_error": self.last_error,
}
CAMERA_MANAGER = WebcamManager()
def log_camera_event(message):
try:
os.makedirs(BASE_DIR, exist_ok=True)
with open(os.path.join(BASE_DIR, "camera_security.log"), "a", encoding="utf-8") as fh:
fh.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | " + str(message) + "\n")
except Exception:
pass
def ffmpeg_dshow_devices():
"""Return DirectShow audio/video devices available to FFmpeg on Windows.
v22 fix: FFmpeg on Windows usually prints devices under section headers like
"DirectShow audio devices" and then plain quoted names. Older builds only
looked for a non-standard `"name" (audio)` suffix, so microphones could be
missed even when they existed. This parser supports both formats and also
stores Alternative names, which are often more reliable than friendly names.
"""
devices = {"video": [], "audio": [], "video_alt": [], "audio_alt": [], "alternatives": {"video": {}, "audio": {}}, "error": "", "raw": ""}
ffmpeg = find_ffmpeg_executable()
if not ffmpeg:
devices["error"] = "FFmpeg לא נמצא."
return devices
if os.name != "nt":
devices["error"] = "זיהוי התקני מצלמה/מיקרופון דרך DirectShow נתמך כאן בעיקר ב־Windows."
return devices
try:
completed = subprocess.run(
[ffmpeg, "-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "dummy"],
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
timeout=12,
)
raw = (completed.stdout or "") + "\n" + (completed.stderr or "")
devices["raw"] = raw[-9000:]
section = None
current_name = None
current_kind = None
def add_device(kind, name):
if not kind or not name:
return
bucket = "video" if kind == "video" else "audio"
if name not in devices[bucket]:
devices[bucket].append(name)
def add_alt(kind, name, alt):
if not kind or not name or not alt:
return
alt_bucket = "video_alt" if kind == "video" else "audio_alt"
devices["alternatives"].setdefault(kind, {})[name] = alt
if alt not in devices[alt_bucket]:
devices[alt_bucket].append(alt)
for line in raw.splitlines():
low = line.lower()
if "directshow video devices" in low:
section = "video"
current_name = None
current_kind = None
continue
if "directshow audio devices" in low:
section = "audio"
current_name = None
current_kind = None
continue
alt_match = re.search(r'Alternative name\s+"([^"]+)"', line, flags=re.I)
if alt_match and current_name and current_kind:
add_alt(current_kind, current_name, alt_match.group(1))
continue
name_match = re.search(r'\]\s+"([^"]+)"', line)
if name_match and section in {"video", "audio"} and "alternative name" not in low:
name = name_match.group(1).strip()
add_device(section, name)
current_name = name
current_kind = section
continue
for name, kind in re.findall(r'"([^"]+)"\s+\((video|audio)\)', line, flags=re.I):
kind = "video" if kind.lower() == "video" else "audio"
add_device(kind, name.strip())
current_name = name.strip()
current_kind = kind
except Exception as exc:
devices["error"] = str(exc)
return devices
def _device_tokens(name):
text = (name or "").lower()
tokens = re.findall(r"[a-z0-9א-ת]+", text)
stop = {"microphone", "mic", "audio", "device", "usb", "video", "camera", "webcam", "hd", "pro", "stream", "capture", "array", "מערך", "מיקרופון", "מצלמה"}
return {t for t in tokens if len(t) >= 3 and t not in stop}
def preferred_audio_candidates(devices=None):
devices = devices or ffmpeg_dshow_devices()
if CAMERA_AUDIO_DEVICE:
return [{"name": CAMERA_AUDIO_DEVICE, "source": "env", "score": 999, "alt": False}]
video = CAMERA_VIDEO_DEVICE or (devices.get("video") or [""])[0]
video_tokens = _device_tokens(video)
rows = []
alternatives = (devices.get("alternatives") or {}).get("audio", {})
for i, name in enumerate(devices.get("audio") or []):
tokens = _device_tokens(name)
score = 100 - i
if video_tokens and tokens:
score += 20 * len(video_tokens & tokens)
low = name.lower()
if any(w in low for w in ["webcam", "camera", "logitech", "c920", "c922", "brio", "usb"]):
score += 15
rows.append({"name": name, "source": "friendly", "score": score, "alt": False})
alt = alternatives.get(name)
if alt:
rows.append({"name": alt, "source": f"alternative for {name}", "score": score - 1, "alt": True})
rows.sort(key=lambda r: r.get("score", 0), reverse=True)
result = []
seen = set()
for row in rows:
if row["name"] and row["name"] not in seen:
result.append(row)
seen.add(row["name"])
return result
def preferred_video_candidates(devices=None):
devices = devices or ffmpeg_dshow_devices()
if CAMERA_VIDEO_DEVICE:
return [{"name": CAMERA_VIDEO_DEVICE, "source": "env", "score": 999, "alt": False}]
alternatives = (devices.get("alternatives") or {}).get("video", {})
rows = []
for i, name in enumerate(devices.get("video") or []):
score = 100 - i
rows.append({"name": name, "source": "friendly", "score": score, "alt": False})
alt = alternatives.get(name)
if alt:
rows.append({"name": alt, "source": f"alternative for {name}", "score": score - 1, "alt": True})
result = []
seen = set()
for row in rows:
if row["name"] and row["name"] not in seen:
result.append(row)
seen.add(row["name"])
return result
def selected_ffmpeg_devices():
devices = ffmpeg_dshow_devices()
video_candidates = preferred_video_candidates(devices)
audio_candidates = preferred_audio_candidates(devices)
devices["video_candidates"] = video_candidates
devices["audio_candidates"] = audio_candidates
video = video_candidates[0]["name"] if video_candidates else ""
audio = audio_candidates[0]["name"] if audio_candidates else ""
return video, audio, devices
def dshow_input_name(video_device="", audio_device=""):
parts = []
if video_device:
parts.append("video=" + video_device)
if audio_device:
parts.append("audio=" + audio_device)
return ":".join(parts)
def tail_text_file(path, max_chars=5000):
try:
if not path or not os.path.exists(path):
return ""
with open(path, "rb") as fh:
fh.seek(0, os.SEEK_END)
size = fh.tell()
fh.seek(max(0, size - max_chars))
data = fh.read().decode("utf-8", errors="replace")
return data[-max_chars:]
except Exception:
return ""
class CameraAudioStreamManager:
def __init__(self):
self.lock = threading.RLock()
self.proc = None
self.started_at = 0.0
self.device = ""
self.last_error = ""
self.log_path = CAMERA_AUDIO_LOG_PATH
self.log_handle = None
self.candidates_tried = []
def _close_log_locked(self):
try:
if self.log_handle:
self.log_handle.close()
except Exception:
pass
self.log_handle = None
def _append_audio_log_locked(self, message):
try:
os.makedirs(BASE_DIR, exist_ok=True)
with open(CAMERA_AUDIO_LOG_PATH, "a", encoding="utf-8") as fh:
fh.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | " + str(message) + "\n")
except Exception:
pass
def _start_candidate_locked(self, ffmpeg, audio_name):
os.makedirs(BASE_DIR, exist_ok=True)
self._append_audio_log_locked("trying audio device: " + str(audio_name))
logfh = open(CAMERA_AUDIO_LOG_PATH, "ab")
header = ("\n" + "=" * 70 + "\n" + time.strftime("%Y-%m-%d %H:%M:%S") + " | ffmpeg audio start | device=" + str(audio_name) + "\n").encode("utf-8", errors="replace")
try:
logfh.write(header)
logfh.flush()
except Exception:
pass
cmd = [
ffmpeg, "-hide_banner", "-nostdin", "-loglevel", "warning",
"-f", "dshow", "-rtbufsize", "64M", "-i", "audio=" + audio_name,
"-vn", "-ac", "1", "-ar", "44100", "-codec:a", "libmp3lame", "-b:a", "96k", "-f", "mp3", "pipe:1",
]
creationflags = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) if os.name == "nt" else 0
proc = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=logfh, creationflags=creationflags)
time.sleep(0.75)
if proc.poll() is not None:
try:
logfh.flush()
logfh.close()
except Exception:
pass
err = tail_text_file(CAMERA_AUDIO_LOG_PATH, 2500)
self._append_audio_log_locked("device failed immediately: " + str(audio_name))
self.candidates_tried.append({"device": audio_name, "ok": False})
self.last_error = "FFmpeg לא הצליח לפתוח את המיקרופון: " + str(audio_name) + "\n" + err[-1200:]
return None, None
self.candidates_tried.append({"device": audio_name, "ok": True})
return proc, logfh
def start(self, restart=False):
with self.lock:
if restart:
self.stop_locked()
return self.start_locked()
def start_locked(self):
if self.proc is not None and self.proc.poll() is None:
return self.proc
if not CAMERA_AUDIO_ENABLED:
raise RuntimeError("אודיו מצלמה כבוי בהגדרת TRIPSERVER_ENABLE_CAMERA_AUDIO.")
ffmpeg = find_ffmpeg_executable()
if not ffmpeg:
raise RuntimeError("FFmpeg לא נמצא. נדרש FFmpeg כדי להזרים אודיו חי.")
_, audio, devices = selected_ffmpeg_devices()
candidates = preferred_audio_candidates(devices)
self.candidates_tried = []
if not candidates:
raw_tail = (devices.get("raw") or "")[-2500:]
self.last_error = "לא נמצא התקן מיקרופון ב־FFmpeg. בדוק הרשאות Windows או הגדר TRIPSERVER_FFMPEG_AUDIO_DEVICE.\n" + raw_tail
raise RuntimeError(self.last_error)
last_error = ""
for row in candidates:
name = row.get("name") or ""
if not name:
continue
proc, logfh = self._start_candidate_locked(ffmpeg, name)
if proc is not None:
self.proc = proc
self.log_handle = logfh
self.started_at = time.time()
self.device = name
self.last_error = ""
log_camera_event(f"audio stream started | device={name}")
return self.proc
last_error = self.last_error
self.proc = None
self._close_log_locked()
if not last_error:
last_error = "כל ניסיונות פתיחת האודיו נכשלו. בדוק לוג אודיו מצלמה."
raise RuntimeError(last_error)
def stream_chunks(self):
try:
with self.lock:
proc = self.start_locked()
while proc is not None and proc.poll() is None:
chunk = proc.stdout.read(4096) if proc.stdout else b""
if not chunk:
if proc.poll() is not None:
self.last_error = "שידור האודיו נסגר על ידי FFmpeg. " + tail_text_file(CAMERA_AUDIO_LOG_PATH, 1600)
break
yield chunk
except GeneratorExit:
pass
except Exception as exc:
self.last_error = str(exc)
self._append_audio_log_locked("stream exception: " + repr(exc))
yield b""
finally:
self.stop()
def stop_locked(self):
proc = self.proc
self.proc = None
if proc is not None and proc.poll() is None:
try:
proc.terminate()
proc.wait(timeout=3)
except Exception:
try:
proc.kill()
except Exception:
pass
self._close_log_locked()
self.started_at = 0.0
def stop(self):
with self.lock:
self.stop_locked()
log_camera_event("audio stream stopped")
def status(self):
with self.lock:
active = bool(self.proc is not None and self.proc.poll() is None)
return {
"audio_enabled": CAMERA_AUDIO_ENABLED,
"active": active,
"device": self.device,
"started_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.started_at)) if self.started_at else "",
"last_error": self.last_error,
"log_path": CAMERA_AUDIO_LOG_PATH,
"candidates_tried": self.candidates_tried,
}
class CameraRecordingManager:
def __init__(self):
self.lock = threading.RLock()
self.proc = None
self.started_at = 0.0
self.file_path = ""
self.log_path = ""
self.last_error = ""
self.with_audio = False
self.video_device = ""
self.audio_device = ""
self.log_handle = None
def _refresh_locked(self):
if self.proc is not None and self.proc.poll() is not None:
try:
if self.log_handle:
self.log_handle.close()
except Exception:
pass
self.log_handle = None
self.proc = None
self.started_at = 0.0
def start(self, with_audio=True):
with self.lock:
self._refresh_locked()
if self.proc is not None and self.proc.poll() is None:
raise RuntimeError("יש כבר הקלטה פעילה. עצור אותה לפני התחלה חדשה.")
if not CAMERA_RECORDING_ENABLED:
raise RuntimeError("הקלטת מצלמה כבויה בהגדרת TRIPSERVER_ENABLE_CAMERA_RECORDING.")
ffmpeg = find_ffmpeg_executable()
if not ffmpeg:
raise RuntimeError("FFmpeg לא נמצא. נדרש FFmpeg כדי להקליט וידאו/אודיו.")
video, audio, devices = selected_ffmpeg_devices()
if not video:
raise RuntimeError("לא נמצא התקן מצלמה ב־FFmpeg. בדוק הרשאות Windows או הגדר TRIPSERVER_FFMPEG_VIDEO_DEVICE.")
if with_audio and not audio:
raise RuntimeError("ביקשת הקלטה עם אודיו, אבל לא נמצא התקן מיקרופון ב־FFmpeg.")
os.makedirs(CAMERA_RECORDINGS_DIR, exist_ok=True)
stamp = time.strftime("%Y%m%d_%H%M%S")
target = os.path.join(CAMERA_RECORDINGS_DIR, f"room_camera_{stamp}.mp4")
log_path = os.path.join(CAMERA_RECORDINGS_DIR, f"room_camera_{stamp}.ffmpeg.log")
input_name = dshow_input_name(video, audio if with_audio else "")
cmd = [
ffmpeg, "-hide_banner", "-y", "-f", "dshow", "-rtbufsize", "512M",
"-framerate", str(CAMERA_RECORD_FPS), "-video_size", f"{CAMERA_WIDTH}x{CAMERA_HEIGHT}",
"-i", input_name, "-t", str(CAMERA_RECORD_MAX_SECONDS),
"-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-pix_fmt", "yuv420p",
]
if with_audio:
cmd += ["-c:a", "aac", "-b:a", CAMERA_AUDIO_BITRATE]
else:
cmd += ["-an"]
cmd += ["-movflags", "+faststart", target]
logfh = open(log_path, "ab")
creationflags = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) if os.name == "nt" else 0
self.proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=logfh, creationflags=creationflags)
self.log_handle = logfh
self.started_at = time.time()
self.file_path = target
self.log_path = log_path
self.last_error = ""
self.with_audio = bool(with_audio)
self.video_device = video
self.audio_device = audio if with_audio else ""
log_camera_event(f"recording started by {get_client_ip()} | file={target} | video={video} | audio={self.audio_device or 'no-audio'}")
return target
def stop(self):
with self.lock:
self._refresh_locked()
proc = self.proc
if proc is None:
return self.file_path or ""
try:
if proc.stdin:
proc.stdin.write(b"q")
proc.stdin.flush()
except Exception:
try:
proc.terminate()
except Exception:
pass
try:
proc.wait(timeout=10)
except Exception:
try:
proc.kill()
except Exception:
pass
try:
if self.log_handle:
self.log_handle.close()
except Exception:
pass
self.log_handle = None
self.proc = None
self.started_at = 0.0
log_camera_event(f"recording stopped by {get_client_ip()} | file={self.file_path}")
return self.file_path
def status(self):
with self.lock:
self._refresh_locked()
active = bool(self.proc is not None and self.proc.poll() is None)
size = os.path.getsize(self.file_path) if self.file_path and os.path.exists(self.file_path) else 0
return {
"recording_enabled": CAMERA_RECORDING_ENABLED,
"active": active,
"file_path": self.file_path,
"file_name": os.path.basename(self.file_path) if self.file_path else "",
"file_size": size,
"started_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.started_at)) if self.started_at else "",
"elapsed_seconds": int(time.time() - self.started_at) if active and self.started_at else 0,
"with_audio": self.with_audio,
"video_device": self.video_device,
"audio_device": self.audio_device,
"log_path": self.log_path,
"last_error": self.last_error,
}
class CameraHlsStreamManager:
"""Unified video+audio live stream using one FFmpeg DirectShow process.
Some USB webcams expose camera and microphone as one physical device. Opening
video through OpenCV and audio through a separate FFmpeg process can fail or
lock the device. HLS captures `video=...:audio=...` in one process, closer to
how Zoom/Teams uses the camera+mic together.
"""
def __init__(self):
self.lock = threading.RLock()
self.proc = None
self.started_at = 0.0
self.video_device = ""
self.audio_device = ""
self.with_audio = True
self.last_error = ""
self.mode = "idle"
def _log(self, message):
try:
os.makedirs(BASE_DIR, exist_ok=True)
with open(CAMERA_HLS_LOG_PATH, "a", encoding="utf-8") as fh:
fh.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | hls | " + str(message) + "\n")
except Exception:
pass
def _clean_dir_locked(self):
try:
os.makedirs(CAMERA_HLS_DIR, exist_ok=True)
for name in os.listdir(CAMERA_HLS_DIR):
if name.lower().endswith((".m3u8", ".ts", ".tmp")):
try:
os.remove(os.path.join(CAMERA_HLS_DIR, name))
except Exception:
pass
except Exception as exc:
self._log("clean hls dir failed: " + repr(exc))
def _hls_input_option_candidates(self):
"""Return DirectShow option fallbacks for webcams.
Some USB webcams, including EMEET models, work in OpenCV but reject a
forced FFmpeg DirectShow combination such as -video_size 1280x720
together with -framerate 15. FFmpeg then fails with: Could not set video
options. v25 tries progressively safer combinations and finally lets the
driver auto-negotiate the video mode.
"""
preferred_size = f"{CAMERA_WIDTH}x{CAMERA_HEIGHT}"
common_sizes = []
for item in (preferred_size, "1280x720", "640x480", "1920x1080"):
if item not in common_sizes:
common_sizes.append(item)
fps_values = []
for item in (CAMERA_RECORD_FPS, 30, 15, 10):
try:
item = int(item)
except Exception:
continue
if item > 0 and item not in fps_values:
fps_values.append(item)
candidates = []
# Try common explicit modes first, then fall back to auto negotiation.
for fps in fps_values[:3]:
candidates.append((f"size={preferred_size},fps={fps}", ["-video_size", preferred_size, "-framerate", str(fps)]))
for size in common_sizes:
candidates.append((f"size={size},auto-fps", ["-video_size", size]))
for fps in fps_values:
candidates.append((f"auto-size,fps={fps}", ["-framerate", str(fps)]))
candidates.append(("driver-auto", []))
seen = set()
unique = []
for label, opts in candidates:
key = tuple(opts)
if key not in seen:
unique.append((label, opts))
seen.add(key)
return unique
def _start_ffmpeg_locked(self, video_name, audio_name=""):
ffmpeg = find_ffmpeg_executable()
if not ffmpeg:
raise RuntimeError("FFmpeg לא נמצא. נדרש FFmpeg לשידור וידאו+אודיו מאוחד.")
self._clean_dir_locked()
playlist = os.path.join(CAMERA_HLS_DIR, "stream.m3u8")
segment_pattern = os.path.join(CAMERA_HLS_DIR, "seg_%05d.ts")
input_name = dshow_input_name(video_name, audio_name)
os.makedirs(os.path.dirname(CAMERA_HLS_LOG_PATH), exist_ok=True)
last_err = ""
for mode_label, input_options in self._hls_input_option_candidates():
self._clean_dir_locked()
cmd = [
ffmpeg, "-hide_banner", "-y", "-nostdin", "-loglevel", "warning",
"-f", "dshow", "-rtbufsize", "512M",
] + list(input_options) + [
"-i", input_name,
"-c:v", "libx264", "-preset", "ultrafast", "-tune", "zerolatency",
"-pix_fmt", "yuv420p", "-r", str(CAMERA_RECORD_FPS),
]
if audio_name:
cmd += ["-c:a", "aac", "-b:a", CAMERA_AUDIO_BITRATE, "-ar", "44100", "-ac", "1"]
else:
cmd += ["-an"]
cmd += [
"-f", "hls", "-hls_time", "1", "-hls_list_size", "5",
"-hls_flags", "delete_segments+omit_endlist+program_date_time",
"-hls_segment_filename", segment_pattern,
playlist,
]
logfh = open(CAMERA_HLS_LOG_PATH, "ab")
try:
header = ("\n" + "=" * 70 + "\n" + time.strftime("%Y-%m-%d %H:%M:%S") + " | unified HLS start | mode=" + mode_label + " | input=" + input_name + "\ncmd=" + " ".join(cmd) + "\n").encode("utf-8", errors="replace")
logfh.write(header)
logfh.flush()
except Exception:
pass
flags = getattr(subprocess, "CREATE_NEW_PROCESS_GROUP", 0) if os.name == "nt" else 0
proc = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=logfh, creationflags=flags)
deadline = time.time() + 8.0
while time.time() < deadline:
if proc.poll() is not None:
break
if os.path.exists(playlist) and os.path.getsize(playlist) > 20:
self.proc = proc
self.started_at = time.time()
self.video_device = video_name
self.audio_device = audio_name
self.with_audio = bool(audio_name)
self.last_error = ""
self.mode = ("hls_av:" if audio_name else "hls_video_only:") + mode_label
self._log("HLS started ok | mode=" + mode_label + " | video=" + str(video_name) + " | audio=" + str(audio_name or "no-audio"))
return True
time.sleep(0.35)
try:
if proc.poll() is None:
proc.terminate()
proc.wait(timeout=3)
except Exception:
try:
proc.kill()
except Exception:
pass
try:
logfh.close()
except Exception:
pass
last_err = tail_text_file(CAMERA_HLS_LOG_PATH, 4200)
if "Could not set video options" in last_err or "Error opening input" in last_err:
self._log("HLS candidate failed; trying next | mode=" + mode_label + " | input=" + input_name)
continue
# Keep trying all candidates even for other errors, because DirectShow is inconsistent.
self._log("HLS candidate failed; trying next | mode=" + mode_label + " | input=" + input_name)
self.last_error = "FFmpeg לא הצליח לפתוח שידור מאוחד אחרי כל מצבי DirectShow: " + input_name + "\n" + (last_err[-1800:] if last_err else "")
self._log("HLS failed all candidates | input=" + input_name)
return False
def start(self, with_audio=True, restart=True):
with self.lock:
if restart:
self.stop_locked()
if self.proc is not None and self.proc.poll() is None:
return True
if not CAMERA_ENABLED:
raise RuntimeError("מצלמה כבויה בהגדרת TRIPSERVER_ENABLE_WEBCAM.")
video, audio, devices = selected_ffmpeg_devices()
if not video:
raise RuntimeError("לא נמצא התקן וידאו ב־FFmpeg. בדוק הרשאות מצלמה או הגדר TRIPSERVER_FFMPEG_VIDEO_DEVICE.")
try:
CAMERA_MANAGER.stop()
except Exception:
pass
try:
CAMERA_AUDIO_STREAM.stop()
except Exception:
pass
audio_rows = preferred_audio_candidates(devices) if with_audio and CAMERA_AUDIO_ENABLED else []
if with_audio and audio_rows:
last_audio_error = ""
for row in audio_rows[:8]:
audio_name = row.get("name") or ""
if not audio_name:
continue
if self._start_ffmpeg_locked(video, audio_name):
return True
last_audio_error = self.last_error
# Keep the camera available even when audio fails, but preserve the audio error.
if self._start_ffmpeg_locked(video, ""):
self.last_error = "האודיו נכשל ולכן הופעל וידאו בלבד. שגיאת אודיו אחרונה:\n" + last_audio_error
return True
raise RuntimeError(last_audio_error or self.last_error or "שידור מאוחד נכשל.")
if self._start_ffmpeg_locked(video, ""):
return True
raise RuntimeError(self.last_error or "שידור מאוחד נכשל.")
def stop_locked(self):
proc = self.proc
self.proc = None
if proc is not None and proc.poll() is None:
try:
proc.terminate()
proc.wait(timeout=4)
except Exception:
try:
proc.kill()
except Exception:
pass
self.started_at = 0.0
self.mode = "idle"
def stop(self):
with self.lock:
self.stop_locked()
self._log("HLS stopped")
def status(self):
with self.lock:
active = bool(self.proc is not None and self.proc.poll() is None)
playlist = os.path.join(CAMERA_HLS_DIR, "stream.m3u8")
return {
"active": active,
"mode": self.mode,
"with_audio": self.with_audio,
"video_device": self.video_device,
"audio_device": self.audio_device,
"started_at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(self.started_at)) if self.started_at else "",
"playlist_exists": os.path.exists(playlist),
"playlist_size": os.path.getsize(playlist) if os.path.exists(playlist) else 0,
"dir": CAMERA_HLS_DIR,
"log_path": CAMERA_HLS_LOG_PATH,
"last_error": self.last_error,
}
CAMERA_HLS_STREAM = CameraHlsStreamManager()
CAMERA_AUDIO_STREAM = CameraAudioStreamManager()
CAMERA_RECORDER = CameraRecordingManager()
def list_camera_recordings(limit=30):
try:
os.makedirs(CAMERA_RECORDINGS_DIR, exist_ok=True)
items = []
for path in glob.glob(os.path.join(CAMERA_RECORDINGS_DIR, "*.mp4")):
try:
items.append({
"name": os.path.basename(path),
"path": path,
"size": os.path.getsize(path),
"mtime": os.path.getmtime(path),
})
except Exception:
pass
items.sort(key=lambda x: x.get("mtime", 0), reverse=True)
return items[:limit]
except Exception:
return []
def append_camera_setup_log(message):
os.makedirs(BASE_DIR, exist_ok=True)
line = f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] {message}\n"
try:
with open(CAMERA_SETUP_LOG_PATH, "a", encoding="utf-8") as fh:
fh.write(line)
except Exception:
pass
return line
def read_camera_setup_log_tail(lines=80):
try:
lines = max(20, min(300, int(lines)))
except Exception:
lines = 80
if not os.path.exists(CAMERA_SETUP_LOG_PATH):
return ""
try:
with open(CAMERA_SETUP_LOG_PATH, "r", encoding="utf-8", errors="replace") as fh:
data = fh.readlines()
return "".join(data[-lines:])
except Exception as exc:
return f"שגיאה בקריאת לוג התקנה: {exc}"
def opencv_import_status():
try:
import cv2 # noqa: F401
return True, getattr(cv2, "__version__", "")
except Exception as exc:
return False, str(exc)
def camera_setup_status():
ok, info = opencv_import_status()
ffmpeg_path = find_ffmpeg_executable() or ""
with CAMERA_SETUP_LOCK:
state = dict(CAMERA_SETUP_STATE)
return {
"opencv": ok,
"opencv_info": info,
"ffmpeg": bool(ffmpeg_path),
"ffmpeg_path": ffmpeg_path,
"python_executable": sys.executable,
"pip_command": f'"{sys.executable}" -m pip install --upgrade {CAMERA_OPENCV_PACKAGE}',
"opencv_package": CAMERA_OPENCV_PACKAGE,
"setup_log_path": CAMERA_SETUP_LOG_PATH,
"marker_path": CAMERA_SETUP_MARKER_PATH,
"auto_install_enabled": CAMERA_AUTO_INSTALL_OPENCV,
"auto_install_delay_seconds": CAMERA_AUTO_INSTALL_DELAY_SECONDS,
"installer": state,
}
def ensure_pip_available():
"""Make pip available in the same Python interpreter when possible."""
check = subprocess.run([sys.executable, "-m", "pip", "--version"], capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=60, shell=False)
if check.returncode == 0:
append_camera_setup_log("pip available: " + (check.stdout or check.stderr or "").strip())
return True
append_camera_setup_log("pip not available, trying ensurepip --upgrade")
try:
ep = subprocess.run([sys.executable, "-m", "ensurepip", "--upgrade"], capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=180, shell=False)
append_camera_setup_log("ensurepip return code: " + str(ep.returncode))
if ep.stdout:
append_camera_setup_log("ensurepip stdout:\n" + ep.stdout[-6000:])
if ep.stderr:
append_camera_setup_log("ensurepip stderr:\n" + ep.stderr[-6000:])
return ep.returncode == 0
except Exception as exc:
append_camera_setup_log("ensurepip exception: " + repr(exc))
return False
def write_camera_setup_marker(info):
try:
os.makedirs(BASE_DIR, exist_ok=True)
with open(CAMERA_SETUP_MARKER_PATH, "w", encoding="utf-8") as fh:
fh.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | " + str(info) + "\n")
except Exception:
pass
def run_opencv_install_worker(mode="manual"):
with CAMERA_SETUP_LOCK:
CAMERA_SETUP_STATE.update({"running": True, "started_at": time.strftime("%Y-%m-%d %H:%M:%S"), "finished_at": "", "ok": False, "last_message": "מתקין OpenCV...", "mode": mode})
append_camera_setup_log("OpenCV install requested | mode=" + str(mode))
append_camera_setup_log("Python executable: " + sys.executable)
if not ensure_pip_available():
msg = "pip לא זמין וגם ensurepip לא הצליח. אי אפשר להתקין OpenCV אוטומטית."
append_camera_setup_log("ERROR: " + msg)
with CAMERA_SETUP_LOCK:
CAMERA_SETUP_STATE.update({"running": False, "finished_at": time.strftime("%Y-%m-%d %H:%M:%S"), "ok": False, "last_message": msg, "mode": mode})
return
cmd = [sys.executable, "-m", "pip", "install", "--upgrade", CAMERA_OPENCV_PACKAGE]
append_camera_setup_log("Command: " + " ".join(cmd))
ok = False
msg = ""
try:
completed = subprocess.run(
cmd,
capture_output=True,
text=True,
encoding="utf-8",
errors="replace",
timeout=CAMERA_OPENCV_INSTALL_TIMEOUT_SECONDS,
shell=False,
)
append_camera_setup_log("pip return code: " + str(completed.returncode))
if completed.stdout:
append_camera_setup_log("pip stdout:\n" + completed.stdout[-12000:])
if completed.stderr:
append_camera_setup_log("pip stderr:\n" + completed.stderr[-12000:])
ok, info = opencv_import_status()
if completed.returncode == 0 and ok:
msg = "OpenCV הותקן וזוהה: " + info
append_camera_setup_log("SUCCESS: " + msg)
write_camera_setup_marker(msg)
elif completed.returncode == 0 and not ok:
msg = "pip הסתיים בהצלחה אבל import cv2 עדיין נכשל: " + info
append_camera_setup_log("WARNING: " + msg)
else:
msg = "pip נכשל. בדוק את הלוג במסך ההתקנה."
append_camera_setup_log("ERROR: " + msg)
ok = False
except subprocess.TimeoutExpired:
msg = f"התקנת OpenCV עברה את מגבלת הזמן ({CAMERA_OPENCV_INSTALL_TIMEOUT_SECONDS} שניות)."
append_camera_setup_log("TIMEOUT: " + msg)
except Exception as exc:
msg = "שגיאה בהרצת pip: " + repr(exc)
append_camera_setup_log("EXCEPTION: " + msg)
with CAMERA_SETUP_LOCK:
CAMERA_SETUP_STATE.update({"running": False, "finished_at": time.strftime("%Y-%m-%d %H:%M:%S"), "ok": bool(ok), "last_message": msg, "mode": mode})
def start_opencv_install_background(mode="manual"):
with CAMERA_SETUP_LOCK:
if CAMERA_SETUP_STATE.get("running"):
return False, "התקנת OpenCV כבר רצה כרגע."
CAMERA_SETUP_STATE.update({"running": True, "started_at": time.strftime("%Y-%m-%d %H:%M:%S"), "finished_at": "", "ok": False, "last_message": "מתחיל התקנה...", "mode": mode})
t = threading.Thread(target=run_opencv_install_worker, kwargs={"mode": mode}, name="tripserver-opencv-installer", daemon=True)
t.start()
return True, "התקנת OpenCV התחילה ברקע. אפשר לרענן את מסך המצלמה/התקנה ולבדוק לוג."
def maybe_start_camera_auto_install(reason="startup"):
if not CAMERA_AUTO_INSTALL_OPENCV:
append_camera_setup_log("Auto install disabled by TRIPSERVER_CAMERA_AUTO_INSTALL_OPENCV")
return False, "התקנה אוטומטית כבויה."
ok, info = opencv_import_status()
if ok:
with CAMERA_SETUP_LOCK:
CAMERA_SETUP_STATE.update({"ok": True, "last_message": "OpenCV כבר זמין: " + str(info), "mode": "ready"})
write_camera_setup_marker("already available: " + str(info))
return False, "OpenCV כבר מותקן."
with CAMERA_SETUP_LOCK:
CAMERA_SETUP_STATE["auto_requested"] = True
def delayed_auto():
try:
if CAMERA_AUTO_INSTALL_DELAY_SECONDS > 0:
time.sleep(CAMERA_AUTO_INSTALL_DELAY_SECONDS)
ok2, info2 = opencv_import_status()
if ok2:
write_camera_setup_marker("available before auto install: " + str(info2))
with CAMERA_SETUP_LOCK:
CAMERA_SETUP_STATE.update({"ok": True, "last_message": "OpenCV כבר זמין: " + str(info2), "mode": "ready"})
return
append_camera_setup_log("Auto install triggered | reason=" + str(reason) + " | cv2 import failed: " + str(info2))
start_opencv_install_background(mode="auto:" + str(reason))
except Exception as exc:
append_camera_setup_log("Auto install scheduler exception: " + repr(exc))
t = threading.Thread(target=delayed_auto, name="tripserver-opencv-auto-installer", daemon=True)
t.start()
return True, "התקנת OpenCV אוטומטית תתחיל ברקע."
def render_camera_setup_page():
allowed, reason = camera_access_allowed()
status = camera_setup_status()
inst = status.get("installer", {})
running = bool(inst.get("running"))
opencv_badge = "ok" if status.get("opencv") else "warn"
ffmpeg_badge = "ok" if status.get("ffmpeg") else "warn"
run_badge = "warn" if running else ("ok" if inst.get("ok") else "idle")
install_disabled = "disabled" if (not allowed or running or status.get("opencv")) else ""
allowed_block = "" if allowed else f"<div class='warn'>גישה חסומה: {html_lib.escape(reason)}</div>"
log_tail = html_lib.escape(read_camera_setup_log_tail(120)) or "אין עדיין לוג התקנה."
return f"""
<!DOCTYPE html><html lang="he" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>התקנת רכיבי מצלמה</title>
<style>
body{{margin:0;background:#07111f;color:white;font-family:Arial,'Segoe UI',sans-serif;padding:18px}}.wrap{{max-width:1050px;margin:auto}}a{{color:#bfdbfe;text-decoration:none}}.hero{{background:linear-gradient(135deg,#0f766e,#0f172a 62%,#111827);border:1px solid #14b8a6;border-radius:28px;padding:22px;margin-bottom:16px;box-shadow:0 20px 70px rgba(0,0,0,.28)}}h1{{margin:0 0 8px;font-size:34px}}p{{color:#cbd5e1;line-height:1.7}}.grid{{display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:14px}}.card{{background:#111827;border:1px solid #334155;border-radius:20px;padding:16px;margin-top:14px}}.badge{{display:inline-block;border-radius:999px;padding:8px 14px;font-weight:900;margin:8px 6px 0 0}}.badge.ok{{background:#064e3b;color:#bbf7d0;border:1px solid #22c55e}}.badge.warn{{background:#7f1d1d;color:#fecaca;border:1px solid #f87171}}.badge.idle{{background:#334155;color:#e2e8f0;border:1px solid #64748b}}button,.btn{{border:0;border-radius:14px;padding:13px 16px;font-weight:950;color:white;background:#2563eb;margin:8px 8px 0 0;cursor:pointer;display:inline-block}}button:disabled{{background:#475569;cursor:not-allowed;opacity:.55}}.warn{{background:#7f1d1d;border:1px solid #f87171;color:#fecaca;border-radius:16px;padding:13px;line-height:1.65;margin:12px 0}}.good{{background:#052e2b;border:1px solid #14b8a6;border-radius:16px;padding:13px;color:#ccfbf1;line-height:1.65;margin:12px 0}}.note{{background:#0b1220;border:1px solid #334155;border-radius:16px;padding:13px;color:#cbd5e1;line-height:1.65;margin:12px 0}}code,pre{{direction:ltr;text-align:left;white-space:pre-wrap;word-break:break-word}}pre{{background:#020617;border:1px solid #334155;color:#fde68a;border-radius:12px;padding:12px;overflow:auto;max-height:440px}}@media(max-width:700px){{body{{padding:13px}}h1{{font-size:28px}}}}
</style></head><body><div class="wrap"><div class="hero"><a href="/admin/camera">⬅ חזרה למצלמה</a><h1>🧩 התקנת רכיבי מצלמה</h1><p>OpenCV מותקן אוטומטית חד־פעמית ברקע כאשר פאנל האדמין עולה. FFmpeg לא מותקן כאן; הוא רק מזוהה מהנתיב שכבר קיים אצלך.</p><span class="badge {opencv_badge}">OpenCV: {'מותקן' if status.get('opencv') else 'חסר'}</span><span class="badge {ffmpeg_badge}">FFmpeg: {'זמין' if status.get('ffmpeg') else 'לא נמצא'}</span><span class="badge {run_badge}">Installer: {'רץ עכשיו' if running else ('הצלחה אחרונה' if inst.get('ok') else 'ממתין')}</span></div>{allowed_block}
<div class="grid"><div class="card"><h2>התקנה אוטומטית חד־פעמית</h2><p>אם OpenCV חסר, הפאנל מריץ לבד את הפקודה:</p><pre>{html_lib.escape(status.get('pip_command',''))}</pre><div class="note">אין כפתור התקנה נוסף. ההתקנה מופעלת ברקע אחרי עליית הפאנל, כדי שתוכל לעשות את זה מרחוק בלי CMD ידני.</div>{'<div class="good">OpenCV כבר מותקן וזמין. אין צורך להתקין שוב.</div>' if status.get('opencv') else ''}</div>
<div class="card"><h2>סטטוס</h2><p><b>Python:</b></p><pre>{html_lib.escape(status.get('python_executable',''))}</pre><p><b>OpenCV:</b> {html_lib.escape(str(status.get('opencv_info','')))}</p><p><b>FFmpeg:</b></p><pre>{html_lib.escape(status.get('ffmpeg_path') or 'לא נמצא')}</pre><p><b>הודעה אחרונה:</b><br>{html_lib.escape(str(inst.get('last_message') or 'אין'))}</p><p><b>התחיל:</b> {html_lib.escape(str(inst.get('started_at') or ''))}<br><b>הסתיים:</b> {html_lib.escape(str(inst.get('finished_at') or ''))}</p><p><a class="btn" href="/camera/setup">רענן</a><a class="btn" href="/camera/setup/status.json" target="_blank">JSON טכני</a></p></div></div>
<div class="card"><h2>לוג התקנה</h2><p>נתיב לוג: <code>{html_lib.escape(CAMERA_SETUP_LOG_PATH)}</code></p><pre>{log_tail}</pre></div></div></body></html>
"""
def camera_av_status():
video, audio, devices = selected_ffmpeg_devices()
return {
"ffmpeg": bool(find_ffmpeg_executable()),
"ffmpeg_path": find_ffmpeg_executable() or "",
"video_device": video,
"audio_device": audio,
"devices": devices,
"audio_stream": CAMERA_AUDIO_STREAM.status(),
"recording": CAMERA_RECORDER.status(),
"recordings_dir": CAMERA_RECORDINGS_DIR,
"audio_log_path": CAMERA_AUDIO_LOG_PATH,
"hls_stream": CAMERA_HLS_STREAM.status(),
"hls_log_path": CAMERA_HLS_LOG_PATH,
}
def camera_stream_generator():
started = time.time()
try:
while True:
if CAMERA_MAX_STREAM_SECONDS > 0 and (time.time() - started) > CAMERA_MAX_STREAM_SECONDS:
break
frame = CAMERA_MANAGER.read_jpeg()
yield b"--frame\r\nContent-Type: image/jpeg\r\nCache-Control: no-cache\r\n\r\n" + frame + b"\r\n"
time.sleep(CAMERA_FRAME_INTERVAL_SECONDS)
except GeneratorExit:
pass
except Exception as exc:
CAMERA_MANAGER.last_error = str(exc)
finally:
# Release the webcam when the browser closes/stops the stream so the LED turns off.
CAMERA_MANAGER.stop()
def render_admin_camera():
allowed, reason = camera_access_allowed()
engine = camera_engine_status()
av = camera_av_status()
cam_status = CAMERA_MANAGER.status()
rec_status = av.get("recording", {})
hls_status = av.get("hls_stream", {})
devices = av.get("devices", {}) or {}
allowed_block = "" if allowed else f"<div class='warn'>גישה חסומה: {html_lib.escape(reason)}</div>"
engine_block = ""
if not engine.get("opencv"):
engine_block = "<div class='warn'><b>OpenCV לא מותקן.</b><br>הפאנל מנסה להתקין אותו אוטומטית. אפשר לראות את הלוג דרך: <a class='btn' href='/camera/setup'>🧩 סטטוס רכיבי מצלמה</a></div>"
if not av.get("ffmpeg"):
engine_block += "<div class='warn'><b>FFmpeg לא נמצא.</b><br>שידור מאוחד וידאו+אודיו והקלטות דורשים FFmpeg ב־PATH או ב־<code>C:\\ffmpeg\\bin\\ffmpeg.exe</code>.</div>"
hls_text = "פעיל" if hls_status.get("active") else "כבוי"
hls_cls = "ok" if hls_status.get("active") else "idle"
audio_text = "כלול" if hls_status.get("active") and hls_status.get("with_audio") else ("וידאו בלבד" if hls_status.get("active") else "כבוי")
audio_cls = "ok" if hls_status.get("active") and hls_status.get("with_audio") else "idle"
rec_text = "מקליט" if rec_status.get("active") else "לא מקליט"
rec_cls = "rec" if rec_status.get("active") else "idle"
disabled_hls = "disabled" if (not allowed or not av.get("ffmpeg")) else ""
disabled_video = "disabled" if (not allowed or not engine.get("opencv")) else ""
disabled_av = "disabled" if (not allowed or not av.get("ffmpeg")) else ""
recordings = list_camera_recordings(12)
rec_rows = "".join(
f"<div class='file-row'><div><b>{html_lib.escape(item['name'])}</b><br><span>{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(item['mtime']))} · {format_bytes(item['size'])}</span></div><a class='mini' href='/camera/recordings/{html_lib.escape(item['name'])}' target='_blank'>פתח</a></div>"
for item in recordings
) or "<div class='muted'>אין עדיין הקלטות בתיקייה.</div>"
video_devices = ", ".join(devices.get("video", [])) or "לא זוהו דרך FFmpeg"
audio_devices = ", ".join(devices.get("audio", [])) or "לא זוהו דרך FFmpeg"
return f"""
<!DOCTYPE html>
<html lang="he" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>מצלמת חדר</title>
<style>
body{{margin:0;background:#07111f;color:white;font-family:Arial,'Segoe UI',sans-serif;padding:18px}}.wrap{{max-width:1120px;margin:auto}}a{{color:#bfdbfe;text-decoration:none}}.hero{{background:linear-gradient(135deg,#0f766e,#0f172a 62%,#111827);border:1px solid #14b8a6;border-radius:28px;padding:22px;margin-bottom:16px;box-shadow:0 20px 70px rgba(0,0,0,.28)}}h1{{margin:0 0 8px;font-size:34px}}h2{{margin:0 0 10px}}p{{color:#cbd5e1;line-height:1.7}}.grid{{display:grid;grid-template-columns:1.45fr .9fr;gap:16px}}.card{{background:#111827;border:1px solid #334155;border-radius:20px;padding:16px;margin-top:14px}}.cam-box{{background:#020617;border:1px solid #334155;border-radius:22px;overflow:hidden;min-height:280px;display:flex;align-items:center;justify-content:center}}#hlsVideo,#cameraView{{width:100%;height:auto;background:#020617;display:none}}.placeholder{{color:#94a3b8;text-align:center;padding:28px;line-height:1.7}}button,.btn{{border:0;border-radius:14px;padding:13px 16px;font-weight:950;color:white;background:#2563eb;margin:8px 8px 0 0;cursor:pointer;display:inline-block}}button.stop{{background:#dc2626}}button.gray,.btn.gray{{background:#475569}}button.snap{{background:#0f766e}}button.record{{background:#ea580c}}button:disabled{{background:#475569;cursor:not-allowed;opacity:.55}}.badge{{display:inline-block;border-radius:999px;padding:8px 14px;font-weight:900;margin:8px 6px 0 0}}.badge.ok{{background:#064e3b;color:#bbf7d0;border:1px solid #22c55e}}.badge.idle{{background:#334155;color:#e2e8f0;border:1px solid #64748b}}.badge.rec{{background:#7c2d12;color:#fed7aa;border:1px solid #fb923c}}.warn{{background:#7f1d1d;border:1px solid #f87171;color:#fecaca;border-radius:16px;padding:13px;line-height:1.65;margin:12px 0}}.note{{background:#0b1220;border:1px solid #334155;border-radius:16px;padding:13px;color:#cbd5e1;line-height:1.65;margin:12px 0}}.good{{background:#052e2b;border:1px solid #14b8a6;border-radius:16px;padding:13px;color:#ccfbf1;line-height:1.65;margin:12px 0}}code,pre{{direction:ltr;text-align:left;white-space:pre-wrap}}pre{{background:#020617;border:1px solid #334155;color:#fde68a;border-radius:12px;padding:10px;overflow:auto}}.row{{display:flex;justify-content:space-between;gap:10px;border-bottom:1px solid #263548;padding:9px 0;color:#cbd5e1}}.row b{{color:white}}.file-row{{display:flex;justify-content:space-between;gap:12px;align-items:center;border-bottom:1px solid #263548;padding:10px 0;color:#cbd5e1}}.file-row span,.muted{{color:#94a3b8}}.mini{{background:#1d4ed8;color:white;border-radius:10px;padding:8px 10px;font-weight:900;white-space:nowrap}}label{{display:block;color:#e2e8f0;margin-top:8px;font-weight:800}}input[type=checkbox]{{transform:scale(1.25);margin-left:8px}}@media(max-width:850px){{.grid{{grid-template-columns:1fr}}body{{padding:13px}}h1{{font-size:28px}}.file-row{{align-items:flex-start}}}}
</style></head><body><div class="wrap"><div class="hero"><a href="/admin">⬅ חזרה לפאנל</a><h1>📷 מצלמת חדר</h1><p>v28 חוזר לברירת מחדל יציבה: וידאו דרך OpenCV/MJPEG, כי אצל EMEET SmartCam C965 הווידאו דרך FFmpeg DirectShow נכשל. האודיו מוזרם בנפרד דרך FFmpeg, וה־HLS המאוחד נשאר רק ככלי בדיקה מתקדם.</p><p><a class="btn gray" href="/camera/setup">סטטוס רכיבי מצלמה</a> <a class="btn gray" href="/admin/logs">לוגים</a></p><span class="badge {hls_cls}">שידור מאוחד: {hls_text}</span><span class="badge {audio_cls}">אודיו: {audio_text}</span><span class="badge {rec_cls}">הקלטה: {rec_text}</span></div>{allowed_block}{engine_block}
<div class="grid"><div><div class="card"><h2>שידור חי וידאו + אודיו</h2><div class="cam-box"><video id="hlsVideo" controls playsinline webkit-playsinline></video><img id="cameraView" alt="שידור מצלמה"><div id="placeholder" class="placeholder">השידור כבוי.<br>לחץ “הפעל וידאו + אודיו”. הווידאו יעבוד דרך OpenCV, והאודיו דרך FFmpeg.</div></div><audio id="audioPlayer" controls playsinline style="width:100%;margin-top:12px;display:none"></audio><div id="cameraMsg" class="note" style="display:none"></div><div><button onclick="startStableMjpegAudio()" {disabled_video}>הפעל וידאו + אודיו</button><button class="stop" onclick="stopStableStream()">כבה וידאו + אודיו</button><button class="snap" onclick="openSnapshot()" {disabled_video}>פתח צילום רגעי</button><button class="gray" onclick="resetCameraDevices()">שחרור/איפוס מצלמה</button><button class="gray" onclick="location.reload()">רענן סטטוס</button></div><div class="note">מתקדם בלבד: אם תרצה לבדוק שוב HLS מאוחד דרך FFmpeg, השתמש בכפתור הבא. אצל המצלמה שלך הוא כנראה נכשל ולכן לא ברירת מחדל.</div><button class="gray" onclick="startUnifiedStream(true)" {disabled_hls}>בדיקה מתקדמת: HLS מאוחד</button><button class="gray" onclick="startMjpegOnly()" {disabled_video}>וידאו בלבד</button></div>
<div class="card"><h2>⏺️ הקלטה למחשב</h2><p>הקלטות נשמרות מקומית בתיקייה: <code>{html_lib.escape(CAMERA_RECORDINGS_DIR)}</code></p><form method="post" action="/camera/record/start"><label><input type="checkbox" name="with_audio" value="1" checked>כלול אודיו בהקלטה</label><button class="record" type="submit" {disabled_av}>התחל הקלטה</button></form><form method="post" action="/camera/record/stop"><button class="stop" type="submit">עצור הקלטה</button></form><div class="good">משך הקלטה מקסימלי להגנה מדיסק מלא: {CAMERA_RECORD_MAX_SECONDS//60} דקות.</div></div>
<div class="card"><h2>🎞️ הקלטות אחרונות</h2>{rec_rows}<p><a class="btn gray" href="/camera/recordings">פתח רשימת הקלטות מלאה</a></p></div></div>
<div><div class="card"><h2>סטטוס טכני</h2><div class="row"><b>OpenCV</b><span>{'מותקן' if engine.get('opencv') else 'לא מותקן'}</span></div><div class="row"><b>FFmpeg</b><span>{'זמין' if av.get('ffmpeg') else 'לא נמצא'}</span></div><div class="row"><b>HLS פעיל</b><span>{hls_text}</span></div><div class="row"><b>HLS מצב</b><span>{html_lib.escape(hls_status.get('mode') or '')}</span></div><div class="row"><b>וידאו HLS</b><span>{html_lib.escape(hls_status.get('video_device') or av.get('video_device') or '')}</span></div><div class="row"><b>אודיו HLS</b><span>{html_lib.escape(hls_status.get('audio_device') or av.get('audio_device') or '')}</span></div><div class="row"><b>לוג HLS</b><span>{html_lib.escape(hls_status.get('log_path') or CAMERA_HLS_LOG_PATH)}</span></div><div class="row"><b>Camera index</b><span>{CAMERA_INDEX}</span></div><div class="row"><b>רזולוציה</b><span>{CAMERA_WIDTH}×{CAMERA_HEIGHT}</span></div><div class="row"><b>Frames MJPEG</b><span>{cam_status.get('frames')}</span></div><div class="row"><b>קובץ הקלטה פעיל</b><span>{html_lib.escape(rec_status.get('file_name') or 'אין')}</span></div><div class="row"><b>גודל הקלטה</b><span>{format_bytes(rec_status.get('file_size') or 0)}</span></div><div class="row"><b>שגיאה אחרונה</b><span>{html_lib.escape(hls_status.get('last_error') or cam_status.get('last_error') or rec_status.get('last_error') or 'אין')}</span></div></div>
<div class="card"><h2>התקנים שזוהו</h2><p><b>וידאו:</b><br>{html_lib.escape(video_devices)}</p><p><b>אודיו:</b><br>{html_lib.escape(audio_devices)}</p><p class="muted">אם נבחר התקן לא נכון, אפשר להגדיר במערכת: <code>TRIPSERVER_FFMPEG_VIDEO_DEVICE</code> / <code>TRIPSERVER_FFMPEG_AUDIO_DEVICE</code>.</p><p><a class="btn gray" href="/camera/audio-test.json" target="_blank">בדיקת אודיו טכנית</a><a class="btn gray" href="/camera/hls/status.json" target="_blank">JSON HLS</a></p></div>
<div class="note"><b>הערת שימוש:</b> זה כולל אודיו והקלטה. ודא שכל מי שנמצא בבית יודע שהמערכת קיימת ומתי היא פעילה.</div></div></div></div>
<script>
function setMsg(id, text, isError=false){{ const el=document.getElementById(id); if(!el)return; el.style.display=text?'block':'none'; el.textContent=text||''; el.className=isError?'warn':'note'; }}
async function startStableMjpegAudio(){{
setMsg('cameraMsg','מפעיל וידאו יציב דרך OpenCV ואודיו דרך FFmpeg...');
const audio=document.getElementById('audioPlayer');
const video=document.getElementById('hlsVideo');
const img=document.getElementById('cameraView');
const ph=document.getElementById('placeholder');
try{{ await fetch('/camera/hls/stop', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
video.pause(); video.removeAttribute('src'); video.load(); video.style.display='none';
// Start audio immediately from the user click, before async video prep can cause iPhone/Safari to block playback.
try{{
audio.style.display='block';
audio.src='/camera/audio-stream?restart=1&ts='+Date.now();
audio.load();
const p=audio.play();
if(p && p.catch) p.catch(e=>setMsg('cameraMsg','הווידאו יופעל. אם אין אודיו, לחץ Play בנגן האודיו. '+e,false));
}}catch(e){{ setMsg('cameraMsg','הווידאו יופעל, אך האודיו לא התחיל: '+e,false); }}
try{{ await fetch('/camera/prepare-video', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
img.onload=()=>{{ph.style.display='none'; img.style.display='block'; setMsg('cameraMsg','וידאו פעיל. האודיו בנגן הנפרד. אם אין קול, לחץ Play בנגן האודיו או פתח בדיקת אודיו טכנית.');}};
img.onerror=()=>setMsg('cameraMsg','שגיאת וידאו. לחץ שחרור/איפוס מצלמה ונסה שוב.',true);
img.src='/camera/stream?ts='+Date.now();
}}
async function stopStableStream(){{
try{{ await fetch('/camera/hls/stop', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
try{{ await fetch('/camera/audio-stop', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
try{{ await fetch('/camera/stop', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
const audio=document.getElementById('audioPlayer'); audio.pause(); audio.removeAttribute('src'); audio.load(); audio.style.display='none';
const video=document.getElementById('hlsVideo'); video.pause(); video.removeAttribute('src'); video.load(); video.style.display='none';
const img=document.getElementById('cameraView'); img.style.display='none'; img.removeAttribute('src');
document.getElementById('placeholder').style.display='block'; setMsg('cameraMsg','הווידאו והאודיו כובו.');
}}
async function startUnifiedStream(withAudio){{
setMsg('cameraMsg','מפעיל שידור מאוחד דרך FFmpeg...');
const video=document.getElementById('hlsVideo'); const img=document.getElementById('cameraView'); const ph=document.getElementById('placeholder');
try{{
img.style.display='none'; img.removeAttribute('src');
const res=await fetch('/camera/hls/start?audio='+(withAudio?'1':'0'), {{method:'POST', cache:'no-store'}});
const data=await res.json().catch(()=>({{ok:false,error:'תגובה לא קריאה מהשרת'}}));
if(!res.ok || !data.ok) throw new Error(data.error || ('HTTP '+res.status));
ph.style.display='none'; video.style.display='block';
video.src='/camera/hls/stream.m3u8?ts='+Date.now(); video.load();
try{{ await video.play(); setMsg('cameraMsg', data.with_audio ? 'שידור וידאו+אודיו פעיל.' : 'וידאו פעיל, אבל האודיו נכשל. בדוק לוג HLS.'); }}
catch(e){{ setMsg('cameraMsg','השידור הופעל. באייפון לחץ Play בנגן אם הוא לא התחיל לבד. '+e, false); }}
}}catch(e){{ setMsg('cameraMsg','שגיאת שידור מאוחד: '+e, true); }}
}}
async function stopUnifiedStream(){{
try{{ await fetch('/camera/hls/stop', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
const video=document.getElementById('hlsVideo'); video.pause(); video.removeAttribute('src'); video.load(); video.style.display='none';
const img=document.getElementById('cameraView'); img.style.display='none'; img.removeAttribute('src');
document.getElementById('placeholder').style.display='block'; setMsg('cameraMsg','השידור כובה.');
}}
async function startMjpegOnly(){{
setMsg('cameraMsg','מפעיל גיבוי וידאו בלבד...');
const video=document.getElementById('hlsVideo'); video.pause(); video.removeAttribute('src'); video.load(); video.style.display='none';
const img=document.getElementById('cameraView'); const ph=document.getElementById('placeholder');
try{{ await fetch('/camera/prepare-video', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
img.onload=()=>{{ph.style.display='none'; img.style.display='block'; setMsg('cameraMsg','וידאו בלבד פעיל.');}};
img.onerror=()=>setMsg('cameraMsg','שגיאת וידאו בלבד. לחץ שחרור/איפוס מצלמה ונסה שוב.',true);
img.src='/camera/stream?ts='+Date.now();
}}
function openSnapshot(){{ window.open('/camera/snapshot?ts='+Date.now(), '_blank'); }}
async function resetCameraDevices(){{
await stopUnifiedStream();
try{{ await fetch('/camera/audio-stop', {{method:'POST', cache:'no-store'}}); }}catch(e){{}}
try{{ const res=await fetch('/camera/reset', {{method:'POST', cache:'no-store'}}); const data=await res.json(); setMsg('cameraMsg', data.message || 'איפוס בוצע.'); }}catch(e){{ setMsg('cameraMsg','שגיאת איפוס: '+e,true); }}
}}
</script>
</body></html>
"""
def serve_app(folder):
file_name = find_html_file(FOLDERS[folder])
if not file_name:
return f"No HTML file found in {folder} folder", 404
return serve_file_no_cache(FOLDERS[folder], file_name)
@app.route("/")
def home():
return """
<!DOCTYPE html>
<html lang="he" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>מרכז ניהול</title>
<style>
body{margin:0;font-family:Arial;background:linear-gradient(135deg,#081229,#102447);color:white;min-height:100vh}
.hero{text-align:center;padding:50px 20px 20px}
.hero h1{font-size:42px;margin:0}
.hero p{color:#d8e6ff;font-size:18px}
.grid{width:min(1100px,92%);margin:40px auto;display:grid;grid-template-columns:repeat(auto-fit,minmax(240px,1fr));gap:22px}
.card{text-decoration:none;color:white;border-radius:28px;padding:28px;min-height:180px;box-shadow:0 10px 30px rgba(0,0,0,.25);transition:.2s transform,.2s box-shadow}
.card:hover{transform:translateY(-4px);box-shadow:0 16px 40px rgba(0,0,0,.35)}
.card h2{font-size:30px;margin:0 0 12px}
.card p{line-height:1.6;margin:0}
.icon{font-size:52px;margin-bottom:15px}
.admin{background:linear-gradient(135deg,#0f9b0f,#38ef7d)}
.money{background:linear-gradient(135deg,#5f2c82,#49a09d)}
.homebudget{background:linear-gradient(135deg,#11998e,#38ef7d)}
.japan{background:linear-gradient(135deg,#ff416c,#ff4b2b)}
.usa{background:linear-gradient(135deg,#0052d4,#4364f7,#6fb1fc)}
.footer{text-align:center;color:#b8c8e6;padding:25px}
.image-guide{margin:12px 0 16px;background:#0f172a;border:1px solid #334155;border-radius:14px;padding:12px;color:#e2e8f0}
.image-guide summary{cursor:pointer;font-weight:bold;color:#bfdbfe;margin-bottom:10px}
.image-guide-table{width:100%;border-collapse:collapse;margin-top:10px;font-size:14px}
.image-guide-table th,.image-guide-table td{border-bottom:1px solid #334155;padding:9px;text-align:right;vertical-align:top}
.image-guide-table th{color:#93c5fd;background:#111827}
.image-guide-table td:first-child{white-space:nowrap;color:#fde68a}
.image-upload-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:12px;margin-top:16px}
.image-upload-card{margin:0}
.image-file-input{position:absolute;opacity:0;width:1px;height:1px;pointer-events:none}
.image-upload-label{display:block;height:100%;cursor:pointer;background:#0f172a;border:1px solid #334155;border-radius:18px;padding:15px;transition:.18s transform,.18s border-color,.18s background;box-shadow:0 8px 22px rgba(0,0,0,.14)}
.image-upload-label:hover{transform:translateY(-2px);border-color:#60a5fa;background:#13213a}
.image-key{display:inline-flex;background:#1d4ed8;color:#dbeafe;border-radius:999px;padding:4px 9px;font-size:12px;font-weight:900;margin-bottom:8px}
.image-title{display:block;color:#fff;font-size:17px;font-weight:950;margin-bottom:7px;line-height:1.25}
.image-desc{display:block;color:#cbd5e1;font-size:14px;line-height:1.45;min-height:42px}
.upload-status{display:block;margin-top:12px;color:#fde68a;font-size:13px;font-weight:900;border-top:1px solid #334155;padding-top:10px}
@media(max-width:640px){.image-upload-grid{grid-template-columns:1fr}.image-title{font-size:16px}.image-desc{min-height:auto}}
.app-hero{background:radial-gradient(circle at top right,rgba(59,130,246,.35),transparent 35%),linear-gradient(135deg,#172554,#0f172a);border:1px solid #334155;border-radius:28px;padding:26px;margin-bottom:18px;box-shadow:0 20px 60px rgba(0,0,0,.25)}
.app-hero h1{text-align:right;margin:0 0 8px;font-size:36px;letter-spacing:-.8px}.app-hero p{margin:0;color:#cbd5e1;line-height:1.65}.top-actions{display:flex;gap:10px;flex-wrap:wrap;margin-top:16px}.pill-link{display:inline-flex;align-items:center;gap:8px;background:#1d4ed8;color:white!important;text-decoration:none;border-radius:999px;padding:10px 14px;font-weight:900;border:1px solid #60a5fa}.pill-link.secondary{background:#334155;border-color:#475569}.dashboard-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(210px,1fr));gap:14px;margin:18px 0}.dash-card{background:linear-gradient(180deg,#1e293b,#111827);border:1px solid #334155;border-radius:22px;padding:16px;box-shadow:0 10px 32px rgba(0,0,0,.18)}.dash-card b{display:block;font-size:18px;margin-bottom:6px}.dash-card span{color:#cbd5e1;font-size:14px;line-height:1.5}.box{box-shadow:0 16px 44px rgba(0,0,0,.20);border:1px solid #334155}.box h2{font-size:25px;letter-spacing:-.4px}.box h3{color:#bfdbfe;margin-top:20px}.split-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(260px,1fr));gap:14px}.mini-panel{background:#0f172a;border:1px solid #334155;border-radius:16px;padding:14px}.mini-panel h3{margin-top:0}.danger-zone{background:linear-gradient(135deg,#451a1a,#1e293b)!important;border-color:#7f1d1d!important}.safe-zone{background:linear-gradient(135deg,#132f25,#1e293b)!important;border-color:#166534!important}.field-label{display:block;color:#cbd5e1;font-size:13px;font-weight:900;margin:12px 0 6px}select,input[type=text]{width:100%;box-sizing:border-box;padding:13px;background:#0f172a;color:white;border-radius:12px;border:1px solid #334155}label.check{display:flex;align-items:center;gap:8px;margin-top:12px;color:#e2e8f0;font-weight:bold}.small-danger{background:#991b1b!important}.cancel{background:#475569!important}.muted{color:#94a3b8;font-size:13px;line-height:1.6}.success-dot{display:inline-block;width:10px;height:10px;background:#22c55e;border-radius:50%;box-shadow:0 0 14px #22c55e;margin-left:8px}.quick-nav{box-shadow:0 12px 32px rgba(0,0,0,.22)}@media(max-width:720px){.wrap{width:min(100%,94%);padding:18px 0}.app-hero{padding:20px}.app-hero h1{font-size:28px}.quick-nav{overflow:auto;flex-wrap:nowrap}.quick-nav a{white-space:nowrap}.dashboard-grid{grid-template-columns:1fr}}
</style>
</head>
<body>
<div class="hero">
<h1>מרכז ניהול</h1>
<p>מערכת מרכזית לניהול קבצים, תקציבים ולוחות זמנים</p>
</div>
<div class="grid">
<a class="card admin" href="/admin"><div class="icon">📁</div><h2>ניהול קבצים</h2><p>העלאת קבצים ישירות למחשב.</p></a>
<a class="card money" href="/Moneyapp/"><div class="icon">💰</div><h2>תקציב טיולים</h2><p>ניהול תקציב חופשות וסנכרון נתונים.</p></a>
<a class="card homebudget" href="/MoneyHome/"><div class="icon">🏠</div><h2>תקציב משפחתי</h2><p>ניהול תקציב חודשי, Limits, הוצאות בפועל וארכיון חודשים.</p></a>
<a class="card admin" href="/TripForm/"><div class="icon">🧾</div><h2>טופס פתיחת טיול</h2><p>טופס קליטת נתונים לטיול חדש.</p></a>
<a class="card admin" href="/admin/camera"><div class="icon">📷</div><h2>מצלמת חדר</h2><p>צפייה חיה במצלמת המחשב, אודיו והקלטות דרך הפאנל.</p></a>
<a class="card usa" href="/admin#trips"><div class="icon">🧳</div><h2>טיולים</h2><p>יפן קיץ 26, ארצות הברית קיץ 27 וכל טיול עתידי במקום אחד.</p></a>
</div>
<div class="footer">TripServer</div>
</body>
</html>
"""
USA_IMAGE_UI_ITEMS = [('intro', '🇺🇸 ארה״ב 2027 — אמריקה על פול ווליום', 'Road Trip • Orlando • Miami • Cruise / 36 ימים של נופים, פארקים, קניות, קרוז ורגעים שגורמים להגיד: “רגע… לשם אנחנו באמת הולכים?” / מערב ארה״ב: סן פרנסיסקו , כביש 1, LA , קניונים, וגאס ויוסמיטי'), ('roadtrip-map', '🗺️ מסלול הרוד טריפ — הסיפור הגדול, מלמעלה', 'סן פרנסיסקו • כביש 1 • LA • וגאס • יוסמיטי / מפה אחת שמסבירה את כל הרעיון: מתחילים באוקיינוס, מטפסים לקניונים, נוגעים במדבר וחוזרים לטבע. / המסלול נותן את כל המנה: חופים, כבישים יפים, פארקים לאומיים, וגאס ויוסמיטי — בלי לנסות לדחוס כאן את כל הלו״ז.'), ('day-01', 'יום 1 — ✈️ נחיתה ישירה ב סן פרנסיסקו', 'סן פרנסיסקו / נוחתים בעיר שיודעת לפתוח טיול עם סטייל. / נחיתה, התארגנות וטעימה ראשונה מהעיר'), ('day-02', 'יום 2 — 🌉 סן פרנסיסקו + Alcatraz', 'סן פרנסיסקו / Alcatraz / יום של אייקונים: מפרץ, גשרים, אלקטרז ואווירה של סרט. / אלקטרז, תצפיות והעיר הכי פוטוגנית בצפון קליפורניה'), ('day-03', 'יום 3 — 🌊 סן פרנסיסקו → מונטריי', 'מונטריי / 17-Mile Drive / עוברים למוד רוד־טריפ אמיתי: ים, כביש ונופים שמתחילים לעבוד עלינו חזק. / נסיעה דרומה, מונטריי וקטע חוף מהמם'), ('day-04', 'יום 4 — 🐋 Monterey Bay Whale Watch + Big Sur', 'מונטריי / Big Sur / היום שבו האוקיינוס נותן הופעה, ואם לוויתן קופץ — זה כבר בונוס קולנועי. / שייט לווייתנים ואז נופי Big Sur וכביש 1'), ('extra-big-sur', 'Road Trip Bonus — 🌉 Big Sur – Bixby Bridge + McWay Falls', 'כביש 1 / קליפורניה / כאן קליפורניה כבר לא רק יפה — היא מתחילה ממש להשוויץ. / גשר Bixby והחופים הדרמטיים של Big Sur הם מהשיאים המצולמים של כביש 1'), ('day-05', 'יום 5 — 🎡 סנטה מוניקה → לוס אנג׳לס', 'Santa Monica / Los Angeles / נכנסים ל־ LA דרך סנטה מוניקה : חוף, טיילת, גלגל ענק ואווירת קליפורניה קלאסית. / Santa Monica Pier, חוף, טיילת וכניסה לאווירת לוס אנג׳לס'), ('day-06', 'יום 6 — 🎥 Warner Bros + Beverly Hills + Griffith', 'לוס אנג׳לס / LA במצב קלאסי: אולפנים, בוורלי הילס ותצפית של סוף סרט. / Warner Bros , Beverly Hills ו־ Griffith'), ('day-07', 'יום 7 — 🏜️ לוס אנג׳לס → Zion', 'מעבר למדבר / Utah / עוזבים עיר ונכנסים למרחבים. זה יום מעבר, אבל עם סוף ששווה את זה. / נסיעה ארוכה אל אזור Zion'), ('extra-diner', 'Road Trip Bonus — 🍔 Peggy Sue’s 50’s Diner', 'עצירת דרך אמריקאית / מדבר / באמצע כל הנופים הגדולים, מגיע רגע קטן ומושלם של אמריקה קלאסית: ניאון, מילקשייק ואווירת רוד־טריפ אמיתית. / עצירת דיינר שנותנת לטיול את הטעם של אמריקה הישנה והכיפית של הכביש'), ('day-08', 'יום 8 — 🏞️ Zion → Bryce → Page', 'Zion / Bryce / Page / יום טבע חזק במיוחד: צוקים, צבעים ומעברים שנראים לא אמיתיים. / Zion , Bryce והגעה ל־ Page'), ('day-09', 'יום 9 — 🧡 Antelope Canyon + Horseshoe Bend', 'Page Arizona / זה היום של “וואו, לאן אתם הולכים?” — בדיוק זה. / Antelope Canyon ו־ Horseshoe Bend'), ('day-10', 'יום 10 — 🏜️ Page → Grand Canyon South Rim', 'Grand Canyon / היום שבו המילה “גדול” מקבלת קנה מידה חדש. / נסיעה אל South Rim ומפגש עם הגרנד קניון'), ('day-11', 'יום 11 — 🎰 Grand Canyon → Las Vegas', 'לאס וגאס / מהטבע הכי גדול — לעיר הכי מוגזמת. מעבר חד, אבל כיפי. / נסיעה לווגאס וכניסה לאורות של הסטריפ'), ('day-12', 'יום 12 — 🛍️ Vegas Outlets + Sphere', 'לאס וגאס / וגאס יודעת לשלב שופינג, שואו ואורות ברמה לא סבירה. / אאוטלטים, Sphere והסטריפ'), ('day-13', 'יום 13 — ✨ Vegas Extra Day + Strip', 'לאס וגאס / עוד יום בווגאס, כי כנראה יום אחד לא מספיק כדי להגזים כמו שצריך. / קניות, Strip , מלונות ותצוגות ערב'), ('day-14', 'יום 14 — 🏔️ Las Vegas → Mammoth Lakes', 'Mammoth Lakes / נפרדים מהניאון וחוזרים לאוויר הרים כמו אנשים אחראיים. בערך. / נסיעה צפונה אל Mammoth Lakes'), ('extra-death-valley', 'Road Trip Bonus — 🔥 Death Valley', 'בין וגאס ל־Mammoth / זה המעבר הכי חד במסלול: מווגאס המנצנצת אל מדבר עצום, שקט ובלתי נתפס. / עמק המוות מוסיף למסלול מרחבים אינסופיים, תצפיות דרמטיות ותחושת קצה של המערב'), ('extra-mono-lake', 'Road Trip Bonus — 🏜️ Mono Lake / Eastern Sierra', 'בדרך אל יוסמיטי / פה הרוד־טריפ משנה טון שוב: מדבר, הרים, אגם דרמטי ושמיים ענקיים — הכל בפריים אחד. / אזור Eastern Sierra מוסיף תחושת מרחב אדירה לפני הכניסה ליוסמיטי'), ('day-15', 'יום 15 — 🚗 Mammoth → Yosemite Valley', 'יוסמיטי / אחד מימי ה־וואו של הטיול. יוסמיטי נכנסת לפריים. / דרך נופית אל Yosemite Valley'), ('day-16', 'יום 16 — 🌲 Yosemite Highlights + Mariposa Grove', 'יוסמיטי / Mariposa / יום של מפלים, גרניט ועצי סקויה שגורמים לך להרגיש בגודל של סיכה. / יוסמיטי קלאסי + Mariposa Grove'), ('day-17', 'יום 17 — ✈️ Yosemite → SFO → Orlando', 'מעבר לאורלנדו / סוגרים פרק מערב ופותחים פרק פארקים. המזוודות עוד לא יודעות מה מחכה להן. / נסיעה לשדה וטיסה לאורלנדו'), ('day-18', 'יום 18 — 🛍️ התאוששות + Disney Springs', 'אורלנדו / יום רך לפני שמתחילים לצרוח בפארקים. / התאוששות, Disney Springs ואווירה קלילה'), ('day-19', 'יום 19 — 🎢 Islands of Adventure', 'Universal Orlando / כאן מתחיל פרק האדרנלין הרשמי. אקספרס ביד, חיוך בפנים. / צ׳ק־אין Premier ו־ Islands of Adventure'), ('day-20', 'יום 20 — 🎬 Universal Studios Florida + CityWalk', 'Universal Studios / יום Universal קלאסי: מתקנים, אווירה ו־ CityWalk לסיום. / Universal Studios Florida וערב CityWalk'), ('day-21', 'יום 21 — ⛳ מנוחה + Hollywood Drive-In Mini Golf', 'אורלנדו / יום רגוע יותר, כי גם למשפחה עם אנרגיה צריך טעינה. / מנוחה, בריכה/קניות קלות ומיני גולף'), ('day-22', 'יום 22 — 🌌 Epic Universe + Express', 'Epic Universe / אחד הימים הכי מסקרנים בטיול — פארק חדש, גדול ומלא הייפ. / Epic Universe עם Express'), ('day-23', 'יום 23 — 🧗 פארק חבלים + בריכה / I‑Drive', 'אורלנדו / קצת אקשן, קצת בריכה, קצת אורלנדו בלי לחץ. / פארק חבלים, בריכה או International Drive'), ('day-24', 'יום 24 — 🌊 Volcano Bay + Express', 'Volcano Bay / מים, מגלשות ויום רענון שנראה כמו פרסומת לחופשה. / Volcano Bay עם Express'), ('day-25', 'יום 25 — 🎃 רגוע + Halloween Horror Nights', 'Universal HHN / היום מתחיל רגוע ונגמר בפנים לבנות. / מנוחה ביום ו־HHN בערב'), ('day-26', 'יום 26 — 🚗 Orlando → Sawgrass Mills → Miami', 'Sawgrass / Miami / מחליפים פארקים בשופינג, ואז ממשיכים ל מיאמי . / Sawgrass Mills והמשך ל מיאמי'), ('day-27', 'יום 27 — 🌴 Miami Chill Before Cruise', 'מיאמי / מיאמי מורידה הילוך בסטייל לפני הקרוז. / חוף, אווירה, התארגנות ונשימה עמוקה'), ('day-28', 'יום 28 — 🚢 החזרת רכב ועלייה לקרוז', 'נמל מיאמי / הרכב חוזר, האונייה מתחילה. החופשה עוברת למצב צף. / עלייה לקרוז ותחילת פרק הים'), ('day-29', 'יום 29 — 🌊 קרוז – יום ים 1', 'בלב הים / יום ים ראשון: בריכה, אוכל, סיפון ונוף בלי סוף. / יום חופשי על האונייה'), ('day-30', 'יום 30 — 🌊 קרוז – יום ים 2', 'בלב הים / עוד יום ים, כי למה לעצור משהו שעובד? / מתקני האונייה, אוכל ומנוחה'), ('day-31', 'יום 31 — 🏝️ St. Maarten', 'St. Maarten / הקריביים נכנסים לתמונה בצבעים שלא נראים חוקיים. / יום עגינה ב־ St. Maarten'), ('day-32', 'יום 32 — 🐬 St. Thomas + Dolphin Splash', 'St. Thomas / יום שיכול להפוך לזיכרון משפחתי חזק במיוחד. / St. Thomas ואטרקציית דולפינים'), ('day-33', 'יום 33 — 🌅 קרוז – יום ים 3', 'בלב הים / עוד קצת ים כדי לא להיגמל מהר מדי. / יום ים, סיפון, אוכל ונוף'), ('day-34', 'יום 34 — 🏖️ Perfect Day at CocoCay', 'CocoCay / שם מחייב — ובמקרה הזה הוא לגמרי מתכוון לזה. / CocoCay , חוף, מים טורקיז ופארק מים'), ('day-35', 'יום 35 — 🏨 ירידה מהקרוז + לילה ליד השדה', 'מיאמי / החופשה מתחילה להתקפל, אבל לא שוברים בבת אחת. / ירידה מהקרוז, התארגנות ולילה ליד השדה'), ('day-36', 'יום 36 — 🛫 טיסה חזרה לישראל', 'הביתה / היום הפחות אהוב, אבל עם אלבום מטורף בזיכרון. / עלייה לטיסה חזרה לישראל'), ('budget', '💰 תקציב — המספרים, בלי דרמה', 'תמונת מצב פיננסית / זה לא טיול קטן. זה פרויקט משפחתי עם נוף, פארקים, ים, שופינג וקרוז. / עלות כללית מוערכת: $81,297 ≈ ₪243,890'), ('summary', '🥂 סיום — אז לאן אתם באמת הולכים?', 'טיול חלום משפחתי / אם מי שרואה את זה אומר “וואו” — הוא פשוט צודק. / מערב ארה״ב, אורלנדו, מיאמי וקרוז — בטיול אחד')]
def local_no_window_flags():
if os.name == "nt":
return getattr(subprocess, "CREATE_NO_WINDOW", 0)
return 0
def local_existing_dir(path):
try:
return bool(path and os.path.isdir(path))
except Exception:
return False
def local_norm_path(path):
return os.path.abspath(os.path.normpath(os.path.expandvars(os.path.expanduser(str(path or "")))))
def local_add_unique_path(items, label, path, key_hint, writable=True):
if not local_existing_dir(path):
return
norm = local_norm_path(path)
low = norm.lower()
if any(item["path"].lower() == low for item in items):
return
items.append({"label": label, "path": norm, "key_hint": key_hint, "readonly": False, "writable": bool(writable)})
def discover_download_folders():
items = []
bases = []
for env in ("USERPROFILE", "HOME"):
val = os.environ.get(env)
if val:
bases.append(val)
hd = os.environ.get("HOMEDRIVE")
hp = os.environ.get("HOMEPATH")
if hd and hp:
bases.append(hd + hp)
for env in ("OneDrive", "OneDriveConsumer", "OneDriveCommercial"):
val = os.environ.get(env)
if val:
bases.append(val)
if os.name == "nt":
# Scan Windows user profiles. This catches cases where the service runs under another user.
users_root = r"C:\Users"
try:
for name in os.listdir(users_root):
if name.lower() in {"public", "default", "default user", "all users"}:
continue
p = os.path.join(users_root, name)
if os.path.isdir(p):
bases.append(p)
bases.append(os.path.join(p, "OneDrive"))
bases.append(os.path.join(p, "OneDrive - Personal"))
except Exception:
pass
checked = set()
for base in bases:
if not base:
continue
base = local_norm_path(base)
if base.lower() in checked:
continue
checked.add(base.lower())
for sub in ("Downloads", "הורדות"):
local_add_unique_path(items, "הורדות במחשב", os.path.join(base, sub), "downloads")
# Some OneDrive setups store the localized display name but keep physical folder as Downloads.
local_add_unique_path(items, "הורדות OneDrive", os.path.join(base, "Downloads"), "downloads")
if os.name == "nt":
# v34.17: also detect Downloads folders placed directly on data drives,
# for example D:\הורדות as seen on the user's computer. This is shallow
# and safe: it only adds existing folders named Downloads / הורדות.
for letter in "CDEFGHIJKLMNOPQRSTUVWXYZ":
drive = f"{letter}:\\"
for sub in ("הורדות", "Downloads"):
candidate = os.path.join(drive, sub)
local_add_unique_path(items, f"הורדות בכונן {letter}:", candidate, "downloads")
try:
for name in os.listdir(drive):
clean = str(name or "").strip().lower()
if clean in {"downloads", "הורדות"}:
local_add_unique_path(items, f"הורדות בכונן {letter}:", os.path.join(drive, name), "downloads")
except Exception:
pass
return items
def tripserver_score(path):
score = 0
try:
names = set(os.listdir(path))
except Exception:
return 0
for name in ("admin_server.py", "control_server", "JAPAN", "USA", "Moneyapp", "MoneyHome", "TripForm"):
if name in names:
score += 1
return score
def discover_tripserver_folders():
items = []
candidates = []
# The running admin script location is the strongest signal.
try:
candidates.append(os.path.dirname(os.path.abspath(__file__)))
except Exception:
pass
candidates.append(BASE_DIR)
candidates.append(os.getcwd())
# Common locations on the server computer.
for env in ("USERPROFILE", "HOME", "OneDrive", "OneDriveConsumer", "OneDriveCommercial"):
base = os.environ.get(env)
if base:
candidates.extend([
os.path.join(base, "TripServer"),
os.path.join(base, "Desktop", "TripServer"),
os.path.join(base, "Documents", "TripServer"),
os.path.join(base, "Downloads", "TripServer"),
])
if os.name == "nt":
for drive in ("C:\\", "D:\\", "E:\\"):
candidates.append(os.path.join(drive, "TripServer"))
try:
# Shallow scan only; avoids hanging the server while still finding typical placements.
for name in os.listdir(drive):
if "tripserver" in name.lower() or name.lower() in {"server", "trip", "trips"}:
candidates.append(os.path.join(drive, name))
except Exception:
pass
try:
for user in os.listdir(r"C:\Users"):
up = os.path.join(r"C:\Users", user)
if os.path.isdir(up):
candidates.extend([
os.path.join(up, "Desktop", "TripServer"),
os.path.join(up, "Documents", "TripServer"),
os.path.join(up, "Downloads", "TripServer"),
])
except Exception:
pass
# Add parent folders around the running script if they look like TripServer.
try:
cur = os.path.dirname(os.path.abspath(__file__))
for _ in range(3):
candidates.append(cur)
cur = os.path.dirname(cur)
except Exception:
pass
seen = set()
scored = []
for c in candidates:
if not c:
continue
n = local_norm_path(c)
low = n.lower()
if low in seen or not local_existing_dir(n):
continue
seen.add(low)
score = tripserver_score(n)
if score >= 1 or os.path.basename(n).lower() == "tripserver":
scored.append((score, n))
scored.sort(key=lambda x: (-x[0], len(x[1])))
for idx, (score, path) in enumerate(scored[:8], start=1):
label = "TripServer" if idx == 1 else f"TripServer נוסף {idx}"
local_add_unique_path(items, label, path, "tripserver")
return items
def build_local_file_roots(force=False):
global LOCAL_FILE_ROOTS_CACHE, LOCAL_FILE_ROOTS_CACHE_TS
now = time.time()
if (not force) and LOCAL_FILE_ROOTS_CACHE and (now - LOCAL_FILE_ROOTS_CACHE_TS < 60):
return LOCAL_FILE_ROOTS_CACHE
roots = {}
all_items = []
all_items.extend(discover_download_folders())
all_items.extend(discover_tripserver_folders())
if not all_items:
# Safe fallback: expose only the folder where the admin is running.
try:
all_items.append({"label": "תיקיית האדמין", "path": os.path.dirname(os.path.abspath(__file__)), "key_hint": "tripserver", "readonly": False, "writable": True})
except Exception:
pass
counts = {"downloads": 0, "tripserver": 0, "root": 0}
for item in all_items:
hint = item.get("key_hint") or "root"
counts[hint] = counts.get(hint, 0) + 1
key = hint if counts[hint] == 1 else f"{hint}_{counts[hint]}"
roots[key] = {"label": item["label"], "path": item["path"], "readonly": False, "writable": True}
LOCAL_FILE_ROOTS_CACHE = roots
LOCAL_FILE_ROOTS_CACHE_TS = now
return roots
def normalize_local_root_key(root_key):
roots = build_local_file_roots()
root_key = (root_key or "").strip().lower()
if root_key in roots:
return root_key
if "downloads" in roots:
return "downloads"
if "tripserver" in roots:
return "tripserver"
return next(iter(roots.keys())) if roots else "downloads"
def local_root_path(root_key):
roots = build_local_file_roots()
root_key = normalize_local_root_key(root_key)
if root_key not in roots:
raise ValueError("לא נמצאה תיקיית בסיס מאושרת.")
return os.path.abspath(os.path.normpath(roots[root_key]["path"]))
def safe_local_path(root_key, rel_path=""):
root = local_root_path(root_key)
rel_path = (rel_path or "").replace("\\", os.sep).replace("/", os.sep).strip()
if rel_path and (os.path.isabs(rel_path) or re.match(r"^[A-Za-z]:", rel_path)):
raise ValueError("נתיב מוחלט נחסם. ניתן לגלוש רק בתוך התיקיות המאושרות.")
target = os.path.abspath(os.path.normpath(os.path.join(root, rel_path)))
try:
common = os.path.commonpath([root, target])
except Exception:
raise ValueError("נתיב לא תקין.")
if common != root:
raise ValueError("ניסיון יציאה מתיקיית הבסיס נחסם.")
return target
def rel_for_local(root_key, full_path):
root = local_root_path(root_key)
try:
rel = os.path.relpath(full_path, root)
return "" if rel == "." else rel.replace("\\", "/")
except Exception:
return ""
def local_file_kind(path):
if os.path.isdir(path):
return "dir"
ext = os.path.splitext(path)[1].lower()
if ext in {".jpg", ".jpeg", ".png", ".webp", ".gif", ".bmp"}:
return "image"
if ext in LOCAL_FILE_TEXT_EXTENSIONS:
return "text"
return "file"
def local_file_icon(kind):
return {"dir": "📁", "image": "🖼️", "text": "📄", "file": "📦"}.get(kind, "📦")
def local_safe_name(name):
name = os.path.basename((name or "").replace("\\", "/")).strip()
name = re.sub(r"[\x00-\x1f<>:\"/\\|?*]", "_", name)
name = name.strip(" .")
if name in {"", ".", ".."}:
raise ValueError("שם קובץ/תיקייה לא תקין.")
return name
def local_unique_destination(path):
if not os.path.exists(path):
return path
folder = os.path.dirname(path)
base, ext = os.path.splitext(os.path.basename(path))
for i in range(1, 1000):
candidate = os.path.join(folder, f"{base}_copy{i}{ext}")
if not os.path.exists(candidate):
return candidate
raise ValueError("לא ניתן ליצור שם יעד ייחודי.")
def local_copy_item(src, dest_dir):
target = local_unique_destination(os.path.join(dest_dir, os.path.basename(src)))
if os.path.isdir(src):
shutil.copytree(src, target)
else:
shutil.copy2(src, target)
return target
def local_move_item(src, dest_dir):
target = local_unique_destination(os.path.join(dest_dir, os.path.basename(src)))
shutil.move(src, target)
return target
def add_path_to_zip(zf, root_key, full_path, base_arc=""):
if os.path.isfile(full_path):
arc = base_arc or rel_for_local(root_key, full_path)
zf.write(full_path, arc)
return os.path.getsize(full_path), 1
total = 0
count = 0
for dirpath, dirnames, filenames in os.walk(full_path):
dirnames[:] = [d for d in dirnames if d not in {"$RECYCLE.BIN", "System Volume Information", "__pycache__"}]
for filename in filenames:
p = os.path.join(dirpath, filename)
try:
size = os.path.getsize(p)
rel = rel_for_local(root_key, p)
zf.write(p, rel)
total += size
count += 1
except Exception:
continue
return total, count
def render_admin_file_manager(root_key="downloads", rel_path=""):
root_key = normalize_local_root_key(root_key)
roots = build_local_file_roots()
root_info = roots[root_key]
try:
current = safe_local_path(root_key, rel_path)
except Exception as exc:
return admin_status_page("נתיב נחסם", "מנהל הקבצים לא פתח את הנתיב המבוקש.", ok=False, details=str(exc)), 400
if not os.path.exists(current):
return admin_status_page("תיקייה לא קיימת", "התיקייה המבוקשת לא קיימת במחשב השרת.", ok=False, details=current), 404
if not os.path.isdir(current):
return redirect(f"/admin/local-files/preview?root={html_lib.escape(root_key, quote=True)}&path={html_lib.escape(rel_for_local(root_key, current), quote=True)}")
rel_path = rel_for_local(root_key, current)
breadcrumbs = []
if rel_path and rel_path != ".":
parts = [p for p in rel_path.split("/") if p and p != "."]
acc = []
breadcrumbs.append(f'<a href="/admin/local-files?root={root_key}">שורש</a>')
for part in parts:
acc.append(part)
breadcrumbs.append(f'<a href="/admin/local-files?root={root_key}&path={html_lib.escape("/".join(acc), quote=True)}">{html_lib.escape(part)}</a>')
else:
breadcrumbs.append("שורש")
rows = []
parent_link = ""
if rel_path and rel_path not in (".", ""):
parent = os.path.dirname(rel_path.replace("/", os.sep)).replace("\\", "/")
parent_link = f'<tr><td></td><td><a class="file-name" href="/admin/local-files?root={root_key}&path={html_lib.escape(parent, quote=True)}">⬆️ תיקייה למעלה</a></td><td>תיקייה</td><td></td><td></td><td></td></tr>'
try:
entries = []
for name in os.listdir(current):
if name in {"$RECYCLE.BIN", "System Volume Information"}:
continue
path = os.path.join(current, name)
try:
is_dir = os.path.isdir(path)
st = os.stat(path)
entries.append((not is_dir, name.lower(), name, path, st))
except Exception:
continue
entries.sort()
limited = entries[:LOCAL_FILE_LIST_LIMIT]
for _not_dir, _sort, name, path, st in limited:
rel = rel_for_local(root_key, path)
kind = local_file_kind(path)
icon = local_file_icon(kind)
safe_name = html_lib.escape(name)
safe_rel = html_lib.escape(rel, quote=True)
modified = time.strftime("%Y-%m-%d %H:%M", time.localtime(st.st_mtime))
size = "—" if os.path.isdir(path) else format_bytes(st.st_size)
checkbox = f'<input type="checkbox" name="path" value="{safe_rel}">'
if os.path.isdir(path):
main = f'<a class="file-name" href="/admin/local-files?root={root_key}&path={safe_rel}">{icon} {safe_name}</a>'
actions = '<span class="muted">פתח</span>'
else:
main = f'<span class="file-name">{icon} {safe_name}</span>'
actions = f'<a href="/admin/local-files/download?root={root_key}&path={safe_rel}">הורד</a> '
if kind == "text" and st.st_size <= LOCAL_FILE_PREVIEW_MAX_BYTES:
actions += f' · <a href="/admin/local-files/preview?root={root_key}&path={safe_rel}">תצוגה</a> · <a href="/admin/local-files/edit?root={root_key}&path={safe_rel}">עריכה</a>'
rows.append(f'<tr><td class="select-cell">{checkbox}</td><td>{main}</td><td>{html_lib.escape(kind)}</td><td>{size}</td><td>{modified}</td><td>{actions}</td></tr>')
if len(entries) > LOCAL_FILE_LIST_LIMIT:
rows.append(f'<tr><td></td><td colspan="5" class="muted">מוצגים {LOCAL_FILE_LIST_LIMIT} פריטים ראשונים מתוך {len(entries)}. השתמש בתיקיות משנה כדי לצמצם.</td></tr>')
except Exception as exc:
rows.append(f'<tr><td></td><td colspan="5">שגיאה בקריאת תיקייה: {html_lib.escape(repr(exc))}</td></tr>')
root_buttons = []
for key, info in roots.items():
cls = "active-root" if key == root_key else ""
root_buttons.append(f'<a class="root-btn {cls}" href="/admin/local-files?root={key}" title="{html_lib.escape(info["path"], quote=True)}">{html_lib.escape(info["label"])}</a>')
dest_options = []
for key, info in roots.items():
selected = " selected" if key == root_key else ""
dest_options.append(f'<option value="{html_lib.escape(key, quote=True)}"{selected}>{html_lib.escape(info["label"])} — {html_lib.escape(info["path"])}</option>')
body_rows = parent_link + "\n".join(rows)
safe_current = html_lib.escape(current)
root_key_safe = html_lib.escape(root_key, quote=True)
rel_path_safe = html_lib.escape(rel_path if rel_path != "." else "", quote=True)
crumbs = " / ".join(breadcrumbs)
return f"""
<!DOCTYPE html><html lang="he" dir="rtl"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>קבצי מחשב</title>
<style>
:root{{--bg:#07111f;--panel:#101c31;--line:#253650;--text:#f8fafc;--muted:#a8b3c7;--brand:#3b82f6;--danger:#dc2626;--ok:#16a34a}}
*{{box-sizing:border-box}}body{{margin:0;font-family:Arial,'Segoe UI',sans-serif;background:linear-gradient(180deg,#07111f,#0f172a);color:var(--text)}}.wrap{{width:min(1220px,94%);margin:auto;padding:22px 0 50px}}a{{color:#bfdbfe}}.hero,.box{{background:rgba(16,28,49,.92);border:1px solid var(--line);border-radius:24px;padding:18px;margin:14px 0;box-shadow:0 18px 50px rgba(0,0,0,.24)}}h1{{margin:0 0 8px;font-size:34px}}p{{color:var(--muted);line-height:1.6}}.root-bar,.ops{{display:flex;gap:10px;flex-wrap:wrap;margin:14px 0}}.root-btn,.btn{{display:inline-flex;text-decoration:none;border:1px solid var(--line);background:#172033;border-radius:999px;padding:10px 13px;font-weight:900;color:#dbeafe}}.root-btn.active-root{{background:#2563eb;color:white;border-color:#60a5fa}}.path{{direction:ltr;text-align:left;background:#020617;border:1px solid #334155;border-radius:16px;padding:12px;color:#fde68a;overflow:auto}}table{{width:100%;border-collapse:collapse;margin-top:12px}}th,td{{border-bottom:1px solid #253650;padding:10px;text-align:right;vertical-align:middle}}th{{color:#cbd5e1}}.file-name{{font-weight:900;text-decoration:none;color:#fff}}.muted{{color:var(--muted)}}.select-cell{{width:44px;text-align:center}}button,input,select,textarea{{font-family:inherit}}button{{border:0;background:#2563eb;color:white;padding:11px 14px;border-radius:12px;font-weight:950;cursor:pointer}}button.danger{{background:#dc2626}}button.ok{{background:#16a34a}}button:hover{{filter:brightness(1.08)}}input[type=text],input[type=file],select,textarea{{background:#020617;color:#fff;border:1px solid #334155;border-radius:12px;padding:10px;max-width:100%}}.note{{background:#0f172a;border:1px solid #334155;border-radius:16px;padding:12px;color:#cbd5e1;line-height:1.6}}.mini-form{{display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin:8px 0}}.danger-note{{border-color:#7f1d1d;background:rgba(127,29,29,.25)}}@media(max-width:760px){{table,thead,tbody,tr,td,th{{display:block}}thead{{display:none}}tr{{border:1px solid #253650;border-radius:18px;margin:10px 0;padding:8px}}td{{border:0;padding:7px}}.select-cell{{width:auto;text-align:right}}}}
</style></head><body><div class="wrap">
<div class="hero"><a class="btn" href="/admin">⬅ חזרה לפאנל</a><h1>📁 קבצי מחשב — גישה מלאה לתיקיות מאושרות</h1><p>הפאנל סורק את מחשב השרת ומציג תיקיות הורדות ו־TripServer שנמצאו בפועל. בתוך התיקיות האלה אפשר לקרוא, להוריד, להעלות, למחוק, ליצור תיקייה, לערוך טקסט, להעתיק ולהעביר קבצים.</p><div class="root-bar">{''.join(root_buttons)}<a class="root-btn" href="/admin/local-files?refresh=1">🔄 סרוק מחדש</a></div><div class="path">{safe_current}</div><p class="muted">{crumbs}</p></div>
<div class="note"><b>חשוב:</b> זה עדיין לא נותן ל־ChatGPT גישה ישירה למחשב שלך. אבל זה כן נותן לך דרך לנהל קבצים במחשב השרת מתוך הפאנל, ליצור ZIP ולהעלות אליי לצ׳אט כשצריך ניתוח.</div>
<div class="box">
<h2>פעולות בתיקייה הנוכחית</h2>
<form class="mini-form" action="/admin/local-files/upload" method="post" enctype="multipart/form-data"><input type="hidden" name="root" value="{root_key_safe}"><input type="hidden" name="path" value="{rel_path_safe}"><input type="file" name="files" multiple><button class="ok" type="submit">⬆ העלה לכאן</button></form>
<form class="mini-form" action="/admin/local-files/mkdir" method="post"><input type="hidden" name="root" value="{root_key_safe}"><input type="hidden" name="path" value="{rel_path_safe}"><input type="text" name="folder_name" placeholder="שם תיקייה חדשה"><button type="submit">📁 צור תיקייה</button></form>
</div>
<div class="box"><form action="/admin/local-files/bulk" method="post"><input type="hidden" name="root" value="{root_key_safe}"><input type="hidden" name="base_path" value="{rel_path_safe}">
<div class="ops"><button name="action" value="zip" type="submit">⬇ ZIP למסומנים</button><button class="danger" name="action" value="delete" type="submit" onclick="return confirm('למחוק את כל הפריטים המסומנים?')">🗑 מחק מסומנים</button><button name="action" value="copy" type="submit">📋 העתק מסומנים אל היעד</button><button name="action" value="move" type="submit" onclick="return confirm('להעביר את הפריטים המסומנים?')">📦 העבר מסומנים אל היעד</button><button name="action" value="rename" type="submit">✏️ שנה שם למסומן אחד</button><input type="text" name="new_name" placeholder="שם חדש למסומן אחד"><select name="dest_root">{''.join(dest_options)}</select><input type="text" name="dest_path" placeholder="נתיב יעד יחסי. ריק = שורש התיקייה שנבחרה"></div>
<table><thead><tr><th></th><th>שם</th><th>סוג</th><th>גודל</th><th>עודכן</th><th>פעולות</th></tr></thead><tbody>{body_rows}</tbody></table></form></div>
</div></body></html>
"""
def render_usa_image_upload_cards():
cards = []
for image_key, title, desc in USA_IMAGE_UI_ITEMS:
safe_key = html_lib.escape(image_key, quote=True)
safe_title = html_lib.escape(title, quote=True)
safe_desc = html_lib.escape(desc, quote=True)
search_text = html_lib.escape((image_key + " " + title + " " + desc).lower(), quote=True)
cards.append("""
<form class="image-upload-card" data-search="%s" action="/upload_usa_image" method="post" enctype="multipart/form-data">
<input type="hidden" name="image_key" value="%s">
<input class="image-file-input" id="image-file-%s" type="file" name="file" accept=".jpg,.jpeg,.png,.webp,.bmp,.tif,.tiff,image/*" onchange="if(this.files && this.files.length){this.closest('form').querySelector('.upload-status').textContent='מעלה תמונה...';this.closest('form').submit();}">
<label for="image-file-%s" class="image-upload-label">
<span class="image-key">%s</span>
<span class="image-title">%s</span>
<span class="image-desc">%s</span>
<span class="upload-status">לחץ כאן ובחר תמונה מהטלפון</span>
</label>
</form>
""" % (search_text, safe_key, safe_key, safe_key, safe_key, safe_title, safe_desc))
return "\n".join(cards)
@app.route("/admin")
@app.route("/admin/")
def admin():
return """
<!DOCTYPE html>
<html lang="he" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TripServer Admin</title>
<style>
:root{--bg:#07111f;--panel:#101c31;--panel2:#14233c;--line:#253650;--text:#f8fafc;--muted:#a8b3c7;--brand:#3b82f6;--brand2:#60a5fa;--good:#16a34a;--warn:#f59e0b;--bad:#dc2626;--glass:rgba(16,28,49,.74)}
*{box-sizing:border-box}body{margin:0;font-family:Arial,'Segoe UI',sans-serif;background:radial-gradient(circle at 85% -10%,rgba(59,130,246,.35),transparent 28%),radial-gradient(circle at 10% 10%,rgba(20,184,166,.18),transparent 24%),linear-gradient(180deg,#07111f,#0f172a 52%,#07111f);color:var(--text);min-height:100vh}a{color:inherit}.wrap{width:min(1180px,94%);margin:auto;padding:22px 0 48px}.app-shell{min-height:calc(100vh - 44px)}
.hero{display:grid;grid-template-columns:1fr auto;gap:16px;align-items:center;background:linear-grad