Merge branch 'dev' into hugo-shipping-notice

This commit is contained in:
hugo committed 2026-07-14 21:18:10 +08:00
commit bc86720744
49 files changed
+2950 -458

No files matched your search

+30 -1
View File
@@ -3,7 +3,7 @@ backend/app/api/admin/reports.py
用途:管理后台报表 API(薄路由层)。
依赖:admin.blueprint、services/admin/report_service。
逻辑:/reports/* 路由。
逻辑:/reports/usage、/reports/applications、feedback 与 general-feedbacks 路由。
"""
from flask import jsonify, request
@@ -38,6 +38,35 @@ def usage_report():
)
@admin_bp.route('/reports/applications', methods=['GET'])
@require_admin
def applications_report():
"""
应用运营报表:按应用聚合使用 / 权限 / 评分。
Query: range, status, usage, q, sort, order, page, page_size。
"""
try:
page = int(request.args.get('page', 1))
except (TypeError, ValueError):
page = 1
try:
page_size = int(request.args.get('page_size', 20))
except (TypeError, ValueError):
page_size = 20
return jsonify(
report_service.build_application_report(
range_key=request.args.get('range'),
status=request.args.get('status'),
usage=request.args.get('usage'),
q=request.args.get('q'),
sort=request.args.get('sort'),
order=request.args.get('order'),
page=page,
page_size=page_size,
)
)
@admin_bp.route('/reports/feedback', methods=['GET'])
@require_admin
def feedback_report():
+47 -80
View File
@@ -8,6 +8,8 @@ backend/app/api/chat.py
import io
import ipaddress
import mimetypes
import ntpath
import posixpath
import tempfile
import os
import subprocess
@@ -226,26 +228,60 @@ def _update_chat_session(
return session
def _normalize_windows_share_path(path_value: str) -> str:
normalized = str(path_value or '').strip().strip('"').replace('/', '\\')
if normalized.startswith('\\\\'):
return '\\\\' + normalized[2:].rstrip('\\')
return normalized.rstrip('\\')
def _normalize_allowed_share_path(path_value: str) -> tuple[str, str]:
"""
统一归一化 Windows UNC / Windows 本地盘符 / Linux 挂载目录路径。
Returns:
(path_kind, normalized_path)
- path_kind: 'windows' | 'posix' | ''
- normalized_path: 归一化后的绝对路径文本
"""
raw_value = str(path_value or '').strip().strip('"')
if not raw_value:
return ('', '')
if raw_value.startswith('\\\\'):
normalized = raw_value.replace('/', '\\')
normalized = '\\\\' + normalized[2:].rstrip('\\')
return ('windows', normalized)
if len(raw_value) >= 3 and raw_value[1] == ':' and raw_value[2] in ('\\', '/'):
normalized = ntpath.normpath(raw_value.replace('/', '\\')).rstrip('\\')
return ('windows', normalized)
if raw_value.startswith('/'):
normalized = posixpath.normpath(raw_value.replace('\\', '/'))
return ('posix', normalized.rstrip('/'))
return ('', '')
def _is_path_within_allowed_root(target_path: str, root_path: str) -> bool:
target_kind, normalized_target = _normalize_allowed_share_path(target_path)
root_kind, normalized_root = _normalize_allowed_share_path(root_path)
if not normalized_target or not normalized_root or target_kind != root_kind:
return False
if target_kind == 'windows':
target_lower = normalized_target.lower()
root_lower = normalized_root.lower()
return target_lower == root_lower or target_lower.startswith(root_lower.rstrip('\\') + '\\')
return normalized_target == normalized_root or normalized_target.startswith(normalized_root.rstrip('/') + '/')
def _is_allowed_share_path(target_url: str) -> bool:
normalized_target = _normalize_windows_share_path(target_url)
if not normalized_target.startswith('\\\\'):
target_kind, normalized_target = _normalize_allowed_share_path(target_url)
if not normalized_target:
return False
history_share_root = ''
history_local_root = ''
if has_request_context():
history_share_root = str(current_app.config.get('FIXED_ASSET_HISTORY_SHARE_ROOT') or '').strip()
history_local_root = str(current_app.config.get('FIXED_ASSET_HISTORY_LOCAL_ROOT') or '').strip()
if not history_share_root:
history_share_root = str(Config.FIXED_ASSET_HISTORY_SHARE_ROOT or '').strip()
if not history_local_root:
history_local_root = str(Config.FIXED_ASSET_HISTORY_LOCAL_ROOT or '').strip()
roots = [
_normalize_windows_share_path(PLM_PREVIEW_SHARE_ROOT),
_normalize_windows_share_path(history_share_root),
PLM_PREVIEW_SHARE_ROOT,
history_share_root,
history_local_root,
]
return any(root and normalized_target.lower().startswith(root.lower()) for root in roots)
return any(root and _is_path_within_allowed_root(normalized_target, root) for root in roots)
def _is_allowed_preview_url(target_url: str) -> bool:
"""
@@ -300,7 +336,7 @@ def _fetch_preview_source_bytes(file_url: str, max_size: int | None=15 * 1024 *
import sqlalchemy as sa
from urllib.parse import parse_qs
if _is_allowed_share_path(file_url):
normalized_path = _normalize_windows_share_path(file_url)
_path_kind, normalized_path = _normalize_allowed_share_path(file_url)
try:
if not os.path.isfile(normalized_path):
return (None, None, ('source file not found', 404))
@@ -1166,31 +1202,6 @@ def search_asset_history_files():
asset_name = (request.args.get('asset_name') or '').strip()
limit = request.args.get('limit', type=int) or 20
limit = max(1, min(limit, 50))
# #region debug-point C:asset-history-api-entry
import json as _debug_json
import urllib.request as _debug_urllib_request
try:
_debug_payload = _debug_json.dumps({
'sessionId': 'asset-history-click',
'runId': 'pre-fix',
'hypothesisId': 'C',
'location': 'backend/chat.py:search_asset_history_files',
'msg': '[DEBUG] asset history api entry',
'data': {
'asset_id': asset_id,
'asset_name': asset_name,
'limit': limit,
},
}).encode('utf-8')
_debug_urllib_request.urlopen(_debug_urllib_request.Request(
'http://127.0.0.1:7777/event',
data=_debug_payload,
headers={'Content-Type': 'application/json'},
method='POST',
), timeout=2).read()
except Exception:
pass
# #endregion
if not asset_id and not asset_name:
raise ValidationError('asset_id or asset_name is required')
@@ -1198,27 +1209,6 @@ def search_asset_history_files():
from app.services.fixed_asset_history import FixedAssetHistoryService
if not FixedAssetHistoryService.is_enabled():
# #region debug-point C:asset-history-api-disabled
try:
_debug_payload = _debug_json.dumps({
'sessionId': 'asset-history-click',
'runId': 'pre-fix',
'hypothesisId': 'C',
'location': 'backend/chat.py:search_asset_history_files',
'msg': '[DEBUG] asset history api disabled',
'data': {
'share_root': str(Config.FIXED_ASSET_HISTORY_SHARE_ROOT or ''),
},
}).encode('utf-8')
_debug_urllib_request.urlopen(_debug_urllib_request.Request(
'http://127.0.0.1:7777/event',
data=_debug_payload,
headers={'Content-Type': 'application/json'},
method='POST',
), timeout=2).read()
except Exception:
pass
# #endregion
raise ServiceUnavailableError('asset history share is not configured')
items = FixedAssetHistoryService.search_history_files(
@@ -1226,29 +1216,6 @@ def search_asset_history_files():
asset_name=asset_name,
limit=limit,
)
# #region debug-point C:asset-history-api-success
try:
_debug_payload = _debug_json.dumps({
'sessionId': 'asset-history-click',
'runId': 'pre-fix',
'hypothesisId': 'C',
'location': 'backend/chat.py:search_asset_history_files',
'msg': '[DEBUG] asset history api success',
'data': {
'asset_id': asset_id,
'asset_name': asset_name,
'item_count': len(items),
},
}).encode('utf-8')
_debug_urllib_request.urlopen(_debug_urllib_request.Request(
'http://127.0.0.1:7777/event',
data=_debug_payload,
headers={'Content-Type': 'application/json'},
method='POST',
), timeout=2).read()
except Exception:
pass
# #endregion
return jsonify({
'asset_id': asset_id,
'asset_name': asset_name,